背景
取り込み時間パーティションテーブルのカラム削除を行いたい。
なかなかいい方法がなくて困りました。
クエリを使った削除や、GCSを経由するLoadジョブだとパーティション情報は失われてしまう。。。
テーブル スキーマからの列の削除
今回はINSERT(..., _PARTITIONTIME) を使うとパーティションに区切れることを利用したやり方でやってみました。
他にいい方法知っている方は教えてください。
やること
対象テーブルのスキーマを取得
bq show --schema --format=prettyjson dataset.sample
スキーマから削除したいカラムのスキーマ情報を削除して、textに保存
元スキーマだとすると...
[{
"type": "TIMESTAMP",
"name": "time",
"mode": "NULLABLE"
},
{
"type": "STRING",
"name": "string1",
"mode": "NULLABLE"
},
{
"type": "STRING",
"name": "string2",
"mode": "NULLABLE"
}
]
deleted_schema.json(使う方)
[{
"type": "TIMESTAMP",
"name": "time",
"mode": "NULLABLE"
},
{
"type": "STRING",
"name": "string1",
"mode": "NULLABLE"
}
]
カラムを削除したスキーマで新テーブル(destination_table)を作成する
bq mk --time_partitioning_type=DAY --table dataset.destination_table ./deleted_schema.json
新テーブルにINSERTする
INSERT INTO `dataset.destination_table` (time, string1, _PARTITIONTIME)
SELECT time, string1, _PARTITIONTIME FROM dataset.sample
参照
Is it possible to remove a column from a partitioned table in Google BigQuery?