# update_schema.py
from google.cloud import bigquery
from google.oauth2 import service_account
# サービスアカウントのJSONキーファイル呼び出し
credentials = service_account.Credentials.from_service_account_file(
filename= './key/eparkdb-*******.json',
scopes=['https://www.googleapis.com/auth/cloud-platform']
)
# プロジェクト情報
project_id = 'eparkdb'
client = bigquery.Client(
credentials=credentials,
project=credentials.project_id,
)
# テーブル情報
table_id = 'opendata.post_office'
table = client.get_table(table_id)
# スキーマ情報
print( 'schema: {}'.format(table.schema) )
schema = [
bigquery.SchemaField("JIS_code_of_the_location_of_large_offices", "INTEGER", mode="NULLABLE", description='大口事業所の所在地のJISコード'),
bigquery.SchemaField("Large_office_name_Kana", "STRING", mode="NULLABLE", description='大口事業所名(カナ)'),
bigquery.SchemaField("Large_office_name_Kanji", "STRING", mode="NULLABLE", description='大口事業所名(漢字)'),
bigquery.SchemaField("Prefecture_name_Kanji", "STRING", mode="NULLABLE", description='都道府県名(漢字)'),
bigquery.SchemaField("City_name_Kanji", "STRING", mode="NULLABLE", description='市区町村名(漢字)'),
bigquery.SchemaField("Town_area_name_Kanji", "STRING", mode="NULLABLE", description='町域名(漢字)'),
bigquery.SchemaField("Koaza_name_chome_street_address_etc_Kanji", "STRING", mode="NULLABLE", description='小字名、丁目、番地等(漢字)'),
bigquery.SchemaField("Large_office_individual_number", "INTEGER", mode="NULLABLE", description='大口事業所個別番号'),
bigquery.SchemaField("Old_zip_code", "INTEGER", mode="NULLABLE", description='旧郵便番号'),
bigquery.SchemaField("Handling_Bureau_Kanji", "STRING", mode="NULLABLE", description='取扱局(漢字)'),
bigquery.SchemaField("Display_of_individual_number_type", "INTEGER", mode="NULLABLE", description='個別番号の種別の表示'),
bigquery.SchemaField("Presence_or_absence_of_multiple_number", "INTEGER", mode="NULLABLE", description='複数番号の有無'),
bigquery.SchemaField("Modification_code", "INTEGER", mode="NULLABLE", description='修正コード'),
]
table.schema = schema
# テーブル更新
table = client.update_table( table, ['schema'] )
# (最新の)テーブル・スキーマ情報
table = client.get_table(table_id)
print( 'schema: {}'.format(table.schema) )
Home/BQのメタデータ(スキーマ定義etc)をPythonで更新する方法(update_table)