How to add data to a RECORD type column using python in bigquery

google-bigquery, python

Solution

Suppose your schema was defined as:

full_name = SchemaField('full_name', 'STRING', mode='REQUIRED')
area_code = SchemaField('area_code', 'STRING', 'REQUIRED')
local_number = SchemaField('local_number', 'STRING', 'REQUIRED')
rank = SchemaField('rank', 'INTEGER', 'REQUIRED')
phone = SchemaField('phone', 'RECORD', mode='NULLABLE',
                    fields=[area_code, local_number, rank])

Then your data to insert would look like:

rows_to_insert = [
        ('Phred Phlyntstone', {'area_code': '800',
                               'local_number': '555-1212',
                               'rank': 1}),
        ('Bharney Rhubble', {'area_code': '877',
                             'local_number': '768-5309',
                             'rank': 2}),
        ('Wylma Phlyntstone', None),
    ]

Problem

I'm using python and want to write to a table in bigquery. The schema of the table looks like: ``` test RECORD REPEATED test.foo STRING NULLABLE test.bar STRING NULLABLE ``` I want to run a command like: ``` table = dataset.table(name='test_table') table.insert_data(rows_to_insert) ``` What does the input rows_to_insert look like. I keep getting the error: Repeated value added outside of an array.

Original source