1
votes

I'm using the python BigQuery client (https://github.com/tylertreat/BigQuery-Python) to upload data from google cloud storage to a table. I want to overwrite the data so I need to set the writeDisposition to WRITE_TRUNCATE. Where should I import the WRITE_TRUNCATE from?

job = client.import_data_from_uris( gs_file_path,
                               'dataset_name',
                               'table_name', 
                               schema, 
                               source_format=JOB_SOURCE_FORMAT_CSV,
                               writeDisposition=WRITE_TRUNCATE,
                               field_delimiter='\t')

Here is the current error - NameError: name 'WRITE_TRUNCATE' is not defined.

Also, I would be interested to know importing of other similar name constants.

1

1 Answers

1
votes

Maybe if before anything you run:

from bigquery.client import JOB_WRITE_TRUNCATE

and then run:

job = client.import_data_from_uris( gs_file_path,
                                   'dataset_name',
                                   'table_name', 
                                   schema, 
                                   source_format=JOB_SOURCE_FORMAT_CSV,
                                   writeDisposition=JOB_WRITE_TRUNCATE,
                                   field_d

elimiter='\t')

It might already work for you. Still, I think that defining yourself this variable and using it as you wish might work the same way and be a bit simpler, like so:

write_disposition = 'WRITE_TRUNCATE'
job = client.import_data_from_uris( gs_file_path,
                               'dataset_name',
                               'table_name', 
                               schema, 
                               source_format=JOB_SOURCE_FORMAT_CSV,
                               writeDisposition=write_disposition,
                               field_delimiter='\t')

if you wanted some appending operation would you just change the value to write_disposition='WRITE_APPEND' and so on.