Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to automatize the bigquery' schema generation using python?

I have a Mysql database on google cloud, I want to create automatic schemas to insert data into Bigquery, I need to create the following line automatically:

schema= [bigquery.SchemaField('EmployeeID', 'STRING', mode='NULLABLE')
bigquery.SchemaField('LastName', 'STRING', mode='NULLABLE')
bigquery.SchemaField('FirstName', 'STRING', mode='NULLABLE')
bigquery.SchemaField('Title', 'STRING', mode='NULLABLE')
bigquery.SchemaField('TitleOfCourtesy', 'STRING', mode='NULLABLE')
bigquery.SchemaField('BirthDate', 'STRING', mode='NULLABLE')
bigquery.SchemaField('HireDate', 'STRING', mode='NULLABLE')
bigquery.SchemaField('Address', 'STRING', mode='NULLABLE')
bigquery.SchemaField('City', 'STRING', mode='NULLABLE')
bigquery.SchemaField('Region', 'STRING', mode='NULLABLE')
bigquery.SchemaField('PostalCode', 'STRING', mode='NULLABLE')
bigquery.SchemaField('Country', 'STRING', mode='NULLABLE')
bigquery.SchemaField('HomePhone', 'STRING', mode='NULLABLE')
bigquery.SchemaField('Extension', 'STRING', mode='NULLABLE')
bigquery.SchemaField('Photo', 'STRING', mode='NULLABLE')
bigquery.SchemaField('Notes', 'STRING', mode='NULLABLE')
bigquery.SchemaField('ReportsTo', 'STRING', mode='NULLABLE')
bigquery.SchemaField('PhotoPath', 'STRING', mode='NULLABLE')]

So in order to achieve this I tried: first, I get the name of the columns with a function this is my output:

print(table_schema_name_column)
['EmployeeID', 'LastName', 'FirstName', 'Title', 'TitleOfCourtesy', 'BirthDate', 'HireDate', 'Address', 'City', 'Region', 'PostalCode', 'Country', 'HomePhone', 'Extension', 'Photo', 'Notes', 'ReportsTo', 'PhotoPath']

then I tried:

schema2=[]
for element in table_schema_name_column:
    base2="bigquery.SchemaField("+'\''+element+"\', \'STRING\', mode=\'NULLABLE\')"
    tmp=base2
    #print(base2)
    schema2.append(base2)

print(schema2)

this is the corresponding output:

["bigquery.SchemaField('EmployeeID', 'STRING', mode='NULLABLE')", 
"bigquery.SchemaField('LastName', 'STRING', mode='NULLABLE')", 
"bigquery.SchemaField('FirstName', 'STRING', mode='NULLABLE')", "bigquery.SchemaField('Title', 'STRING', mode='NULLABLE')", 
"bigquery.SchemaField('TitleOfCourtesy', 'STRING', mode='NULLABLE')", "bigquery.SchemaField('BirthDate', 'STRING', mode='NULLABLE')",
"bigquery.SchemaField('HireDate', 'STRING', mode='NULLABLE')", "bigquery.SchemaField('Address', 'STRING', mode='NULLABLE')", 
"bigquery.SchemaField('City', 'STRING', mode='NULLABLE')", "bigquery.SchemaField('Region', 'STRING', mode='NULLABLE')",
 "bigquery.SchemaField('PostalCode', 'STRING', mode='NULLABLE')", "bigquery.SchemaField('Country', 'STRING', mode='NULLABLE')", 
 "bigquery.SchemaField('HomePhone', 'STRING', mode='NULLABLE')", "bigquery.SchemaField('Extension', 'STRING', mode='NULLABLE')",
  "bigquery.SchemaField('Photo', 'STRING', mode='NULLABLE')", "bigquery.SchemaField('Notes', 'STRING', mode='NULLABLE')", 
  "bigquery.SchemaField('ReportsTo', 'STRING', mode='NULLABLE')", "bigquery.SchemaField('PhotoPath', 'STRING', mode='NULLABLE')"]

the problem with this schema2 is that when I try to use it to create a table I got the following error:

table_ref = dataset_ref.table("my_table_aut")
table = bigquery.Table(table_ref, schema=schema2)
table = client.create_table(table)  # API request

assert table.table_id == "my_table_aut"

error output:

ValueError                                Traceback (most recent call last)
<ipython-input-13-ce1fc2c637fe> in <module>
      4 ]
      5 table_ref = dataset_ref.table("my_table_aut")
----> 6 table = bigquery.Table(table_ref, schema=schema2)
      7 table = client.create_table(table)  # API request
      8 

~/.local/lib/python3.6/site-packages/google/cloud/bigquery/table.py in __init__(self, table_ref, schema)
    371         # Let the @property do validation.
    372         if schema is not None:
--> 373             self.schema = schema
    374 
    375     @property

~/.local/lib/python3.6/site-packages/google/cloud/bigquery/table.py in schema(self, value)
    420             self._properties["schema"] = None
    421         elif not all(isinstance(field, SchemaField) for field in value):
--> 422             raise ValueError("Schema items must be fields")
    423         else:
    424             self._properties["schema"] = {"fields": _build_schema_resource(value)}

ValueError: Schema items must be fields

So I would like to appreciate the support to overcome this task

like image 880
neo33 Avatar asked Jun 16 '26 09:06

neo33


1 Answers

This should work:

schema2=[]
for element in table_schema_name_column:
    schema2.append(bigquery.SchemaField(element, 'STRING', mode='NULLABLE'))

table_ref = dataset_ref.table("my_table_aut")
table = bigquery.Table(table_ref, schema=schema2)
table = client.create_table(table)
like image 176
vinoaj Avatar answered Jun 18 '26 00:06

vinoaj