LoginSignup
3
3

More than 3 years have passed since last update.

GCSのファイルをBigQueryにロードするジョブをPythonで実装したメモ

Last updated at Posted at 2020-01-21

サンプルコード


# sevice account の設定しないと認証エラー出ます
# export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credential

from google.cloud import bigquery
client = bigquery.Client('project')

table_ref = client.dataset('dataset').table('table$20200101') # partition指定する

job_config = bigquery.LoadJobConfig()
job_config.write_disposition = bigquery.WriteDisposition.WRITE_TRUNCATE # 上書き
job_config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON # JSON
# job_config.ignore_unknown_values = True #unknowsな値を許容するならコメント外す

uri = 'gs://bucket/path/*.gz' # ディレクトリ以下全て指定
load_job = client.load_table_from_uri(
        uri, table_ref, job_config=job_config
        )     
load_job.result()
print("Job finished.")

参考

CSV データをテーブルに読み込む

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3