【问题标题】:Google Cloud Storage Joining multiple csv files谷歌云存储加入多个 csv 文件
【发布时间】:2019-08-02 12:10:12
【问题描述】:

鉴于 BigQuery 将文件导出为 99 个 csv 文件的文件大小,我将数据集从 Google BigQuery 导出到 Google Cloud Storage。

但是现在我想连接到我的 GCP Bucket 并使用 Spark 执行一些分析,但我需要将所有 99 个文件合并到一个大型 csv 文件中才能运行我的分析。

如何做到这一点?

【问题讨论】:

    标签: google-bigquery google-cloud-storage


    【解决方案1】:

    如果是larger than 1GB,BigQuery 会将导出的数据拆分为多个文件。但是您可以将这些文件与gsutil tool 合并,查看this official documentation 以了解如何使用 gsutil 执行对象组合。

    由于 BigQuery 导出具有相同前缀的文件,您可以使用通配符 * 将它们合并为一个复合对象:

    gsutil compose gs://example-bucket/component-obj-* gs://example-bucket/composite-object

    请注意,可以在单个操作中组合的组件数量存在限制(当前为 32 个)。

    此选项的缺点是每个.csv 文件的标题行将添加到复合对象中。但是您可以通过修改jobConfigprint_header parameter 设置为False 来避免这种情况。

    这是一个 Python 示例代码,但你可以使用any other BigQuery Client library

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-02
      • 2015-06-02
      • 1970-01-01
      • 2018-04-21
      • 1970-01-01
      • 2021-06-13
      相关资源
      最近更新 更多