I have a big query table with 4 columns : name (string) , age(int) , score(float) , dob (datetime).
#!/usr/bin/env python
import json
from google.cloud import bigquery
def stream_data(json_data):
bigquery_client = bigquery.Client("myproject")
dataset = bigquery_client.dataset("ComputedData")
table = dataset.table("test")
data = json.loads(json_data)
table.reload()
rows = [data]
errors = table.insert_data(rows)
if not errors:
print('Loaded 1 row ')
else:
print('Errors: {}'.format(errors))
if __name__ == '__main__':
mynam = 'mike'
mage = 212
mydob='1983-09-01 00:00:00'
mydob=None
ds=str(mydob) if mydob else None
myscore = 0;
stream_data('["' + str(mynam) + '",' + str(mage) + ',"' + ds + '",'+ str(myscore) +']')
The above is a sample to test whether I can insert null values . For eg. I actually compute the score and date of birth (assume) and insert it. But if the computation does not work I want to insert null into the big query table as datetime supports null.
By default , null is None in python. However I cannot insert this into the as I cannot concat Nonetype as string . If I try to stringify the null statement as :
ds=str(mydob) if mydob else 'null'
I get 'Invalid datetime string "null"'
I am not sure if I am generating my json the wrong way. enter image description here
I do get nulls but for that I have to leave out the field in the json.
stream_data('["' + str(mynam) + '",' + str(mage) + ']')
I cannot do this as if I want to enter score but leave dob as null , if I insert
stream_data('["' + str(mynam) + '",' + str(mage) + ',' + str(myscore)+']')
This inserts or tries to insert score in the datetime column. So I have to insert a value for every column in the json. but want to be able to specify nulls while inserting.

name,12,,123(not the two commas). Ideally BQ should default to the default date. If it does not, some alternatives: - push the default date yourself (1970) - put the field as string, and write null instead (depends what you do with the date afterwards).