BigQuery 将导出的数据拆分为多个文件(如果是)大于1GB。但您可以将这些文件与gsutil工具, check 这个官方文档了解如何使用 gsutil 执行对象组合。
当 BigQuery 导出具有相同前缀的文件时,您可以使用通配符*
将它们合并为一个复合对象:
gsutil compose gs://example-bucket/component-obj-* gs://example-bucket/composite-object
请注意,单个操作中可以组成的组件数量有限制(当前为 32 个)。
此选项的缺点是每个的标题行.csv
文件将被添加到复合对象中。但你可以通过修改来避免这种情况jobConfig设置print_header范围 to False
.
这是一个Python示例代码,但你可以使用任何其他 BigQuery 客户端库:
from google.cloud import bigquery
client = bigquery.Client()
bucket_name = 'yourBucket'
project = 'bigquery-public-data'
dataset_id = 'libraries_io'
table_id = 'dependencies'
destination_uri = 'gs://{}/{}'.format(bucket_name, 'file-*.csv')
dataset_ref = client.dataset(dataset_id, project=project)
table_ref = dataset_ref.table(table_id)
job_config = bigquery.job.ExtractJobConfig(print_header=False)
extract_job = client.extract_table(
table_ref,
destination_uri,
# Location must match that of the source table.
location='US',
job_config=job_config) # API request
extract_job.result() # Waits for job to complete.
print('Exported {}:{}.{} to {}'.format(
project, dataset_id, table_id, destination_uri))
最后记得写一个空的.csv
仅包含标题行。