2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SQLファイルを読みBigQueryを実行しcsvを保存してくれるPythonスクリプト

Posted at

python 3.6.1 にて動作。

bq_runnner.py
import argparse
import pandas as pd


def run_query(sql_file):
    with open(sql_file, 'r') as f:
        query = f.read()
    
    project_id = 'YOUR_PROJECT_ID'
    df = pd.read_gbq(query, project_id=project_id, dialect='standard')
    return df


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('sql_file')
    parser.add_argument('--output_csv', default='output.csv')

    args = parser.parse_args()

    df = run_query(args.sql_file)
    df.to_csv(args.output_csv, index=False)

使い方

$ python bq_runnner.py YOUR_QUERY.sql

csvファイル名はお好みで。

必要ライブラリ

requirements.txt
pandas==0.25.3
pandas-gbq==0.13.0
2
5
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
2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?