1
1

More than 3 years have passed since last update.

BigQueryの取り込み時間パーティションテーブルのカラム削除

Posted at

背景

取り込み時間パーティションテーブルのカラム削除を行いたい。
なかなかいい方法がなくて困りました。
クエリを使った削除や、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?

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