如何将GG Bigquery中存储的数据导出到GZ文件中。

时间:2018-10-16 03:51:08

标签: python-3.x google-bigquery export

我使用以下代码将数据导出到csv文件中,并且可以正常工作:

project_id = 'project_id'
client = bigquery.Client()
dataset_id = 'dataset_id'
bucket_name = 'bucket_name'
table_id = 'table_id'

destination_uri = 'gs://{}/{}'.format(bucket_name, 'file.csv')
dataset_ref = client.dataset(dataset_id, project=project_id)
table_ref = dataset_ref.table(table_id)

extract_job = client.extract_table(
    table_ref,
    destination_uri) 
extract_job.result() 

但是我更喜欢GZ文件,因为我的表最大为700M。谁能帮我将数据导出到GZ文件中?

1 个答案:

答案 0 :(得分:2)

您需要像这样添加jobConfig

job_config = bigquery.job.ExtractJobConfig()
job_config.compression = 'GZIP'

完整代码:

from google.cloud import bigquery
client = bigquery.Client()

project_id = 'fh-bigquery'
dataset_id = 'public_dump'
table_id = 'afinn_en_165'


bucket_name = 'your_bucket'

destination_uri = 'gs://{}/{}'.format(bucket_name, 'file.csv.gz')

dataset_ref = client.dataset(dataset_id, project=project_id)
table_ref = dataset_ref.table(table_id)

job_config = bigquery.job.ExtractJobConfig()
job_config.compression = 'GZIP'
extract_job = client.extract_table(
    table_ref,
    destination_uri,
    job_config = job_config
) 
extract_job.result()