【问题标题】:Is there any way to create multiple tables in BigQuery at once?有没有办法一次在 BigQuery 中创建多个表?
【发布时间】:2021-06-10 09:43:35
【问题描述】:

我在 GCS 的存储桶中有很多 json 文件,我需要为每个文件创建一个表。

通常,我在 BigQuery 中手动执行:选择格式 (json),为其命名并使用自动检测到的架构。

有没有办法使用 GCS 中的数据一次创建多个表?

【问题讨论】:

    标签: json datatable google-bigquery


    【解决方案1】:

    免责声明:我在https://medium.com/p/54228d166a7d 有一篇关于此主题的博文

    基本上,您可以利用 Cloud Workflows 来自动化此过程。

    一个示例工作流程是:

    ProcessItem:
      params: [project, gcsPath]
      steps:
        - initialize:
            assign:
              - dataset: wf_samples
              - input: ${gcsPath}
    # omitted parts for simplicity 
        - runLoadJob:
            call: BQJobsInsertLoadJob_FromGCS
            args:
              project: ${project}
              configuration:
                jobType: LOAD
                load:
                  sourceUris: ${gcsPath}
                  schema:
                    fields:
                      - name: "mydate"
                        type: "TIMESTAMP"
                      - name: "col1"
                        type: "FLOAT"
                      - name: "col2"
                        type: "FLOAT"
                  destinationTable:
                    projectId: ${project}
                    datasetId: ${dataset}
                    tableId: ${"table_"+output.index}
            result: loadJobResult
        - final:
            return: ${loadJobResult}
    BQJobsInsertLoadJob_FromGCS:
      params: [project, configuration]
      steps:
        - runJob:
            call: http.post
            args:
              url: ${"https://bigquery.googleapis.com/bigquery/v2/projects/"+project+"/jobs"}
              auth:
                type: OAuth2
              body:
                configuration: ${configuration}
            result: queryResult
            next: queryCompleted
        - queryCompleted:
            return: ${queryResult.body}
    

    【讨论】:

      【解决方案2】:

      this answer 中,您有一个解决方案可以递归地遍历您的存储桶并将 csv 文件加载到 BQ。例如,您可以修改此代码:

      gsutil ls gs://mybucket/**.json | \
          xargs -I{} echo {} | \
          awk '{n=split($1,A,"/"); q=split(A[n],B,"."); print "mydataset."B[1]" "$0}' | \
          xargs -I{} sh -c 'bq --location=YOUR_LOCATION load --replace=false --autodetect --source_format=NEWLINE_DELIMITED_JSON {}'
      

      如果您想手动并行运行加载作业。

      如果您想添加自动化,您可以使用@Pentium10 推荐的工作流,或者将 Bash 命令插入到 Cloud Run 实例中并与调度程序结合使用(您可以查看this repo 以获得灵感)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-06
        • 2022-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多