2
2

More than 3 years have passed since last update.

pythonでbigqueryのデータセットとテーブル一覧を抜き出してCSV出力する

Posted at

A列にデータセット名、B列にテーブル名が記載されたCSVを出力します。
対象のprojectはターミナルで以下を打ち込んだ際に表示されるプロジェクトとなります。

gcloud config list

コード

import datetime
import pandas as pd
from google.cloud import bigquery

bigquery_data = pd.DataFrame(
    index   = [] ,
    columns = ['dataset','table']
)

client = bigquery.Client()
datasets = list(client.list_datasets())
project = client.project

for dataset in datasets:
    dataset_id = '{}.{}'.format(project , dataset.dataset_id)
    dataset = client.get_dataset(dataset_id)
    tables = list(client.list_tables(dataset))
    for table in tables:
        dataset_table = pd.Series({'dataset' : dataset.dataset_id ,'table' : table.table_id})
        bigquery_data = bigquery_data.append(dataset_table , ignore_index = True)
make_day = datetime.datetime.today().strftime('%Y-%m-%d')
bigquery_data.to_csv('bigquery_data_{}.csv'.format(make_day) , index = False)

2
2
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
2