我想将表格数据从BigQuery导出到Google Cloud Storage。
问题是,我需要从date1到date2的数据,而不是整个表的数据。

extract_job = client.extract_table(
    table_ref,
    destination_uri,
    # Location must match that of the source table.
    location='US')  # API request
extract_job.result()

这就是我在Google云端帮助中找到的内容。
没有空间使用where子句添加查询或限制数据。

最佳答案

不幸的是,这将是两步过程。
首先,您需要建立结果表,然后再导出结果。
从成本的角度来看,影响应该是最小的-您将为临时表使用的存储空间支付结果,但成本为每月每GB 0.02 USD-因此,如果您设法在1小时内完成任务-成本为0.000027 USD/GB

job_config = bigquery.QueryJobConfig()
gcs_filename = 'file_*.gzip'

table_ref = client.dataset(dataset_id).table('my_temp_table')
job_config.destination = table_ref

job_config.write_disposition = bigquery.WriteDisposition.WRITE_TRUNCATE

# Start the query, passing in the extra configuration.
query_job = client.query(
    """#standardSql
    select * from `project.dataset.table` where <your_condition> ;""",
    location='US',
    job_config=job_config)

while not query_job.done():
    time.sleep(1)

#check if table successfully written
print("query completed")
job_config = bigquery.ExtractJobConfig()
job_config.compression = bigquery.Compression.GZIP
job_config.destination_format = (
    bigquery.DestinationFormat.CSV)
job_config.print_header = False

destination_uri = 'gs://{}/{}'.format(bucket_name, gcs_filename)

extract_job = client.extract_table(
    table_ref,
    destination_uri,
    job_config=job_config,
    location='US')  # API request
extract_job.result()
print("extract completed")

关于python - 使用python将BigQuery表数据导出到具有where子句的Google Cloud Storage,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50794270/

10-11 03:51
查看更多