created: 2019-08-10T07:24:49.000Z

bigqueryのクエリ結果をgcsに出力するスクリプト

こんな段取りで行う

  • テンポラリなテーブルを作って、そこにクエリの結果を入れる
  • そのテンポラリなテーブルを、データエクスポート機能を使ってgcsに送る
import os
import datetime
from google.cloud import bigquery


DATASET_ID = os.getenv("BQ_DATASET_ID")

def export_to_gcs(query: str, destination_uri: str):
    client = bigquery.Client()
    temp_table_name = 'temp_to_gcs_' + datetime.datetime.now().strftime("%Y%m%d_%H%M")
    temp_table_ref = client.dataset(DATASET_ID).table(temp_table_name)

    # query configuration
    job_config = bigquery.QueryJobConfig()
    job_config.destination = temp_table_ref
    job_config.write_disposition = 'WRITE_TRUNCATE'
    query_job = client.query(query, job_config=job_config)

    try:
        query_job.result()  # waiting
        # transport to gcs
        client.extract_table(temp_table_ref, destination_uri)
    finally:
        client.delete_table(temp_table_ref)


if __name__ == '__main__':
    query = "SELECT * FROM testds20180123.testable"
    export_to_gcs(query, 'gs://xxxx-bq-output/public/test/test2.csv')
みんなのデータ構造
[ad] みんなのデータ構造
Pat Morin, 堀江 慧 (単行本(ソフトカバー))