LoginSignup
14
15

More than 5 years have passed since last update.

BigQuery で既存のテーブルにカラムを追加する

Posted at

bq コマンドを使って行う。

はじめに bq コマンドで、対象のテーブルの現在のスキーマを取得する。

$ bq --format=prettyjson show project_id_x:dataset_id_y.table_id_z > table.json
$ cat table.json 
{
  "creationTime": "xxx", 
  "etag": "xxx", 
  "id": "project_id_x:dataset_id_y.table_id_z", 
  "kind": "bigquery#table", 
  "lastModifiedTime": "xxx", 
  "schema": {
    "fields": [
      {
        "name": "time", 
        "type": "INTEGER"
      }, 
-------------
略
-------------
      {
        "name": "hoge", 
        "type": "STRING"
      }
    ]
  }, 
  "selfLink": "xxx", 
  "tableReference": {
    "datasetId": "dataset_id_y", 
    "projectId": "project_id_x", 
    "tableId": "table_id_z"
  }, 
  "type": "TABLE"
}

取得したスキーマを fields の部分だけ取り出した json に変更する。

$ cat table.json 
[
      {
        "name": "time", 
        "type": "INTEGER"
      }, 
-------------
略
-------------
      {
        "name": "hoge", 
        "type": "STRING"
      }
]

変更したスキーマに 追加したいカラムを足す。

$ cat table.json 
[
      {
        "name": "time", 
        "type": "INTEGER"
      }, 
-------------
略
-------------
      {
        "name": "hoge", 
        "type": "STRING"
      },
      {
        "name": "new_column_dayo", 
        "type": "STRING"
      }
]

新しいスキーマを適用する。

% bq update project_id_x:dataset_id_y.table_id_z table.json
Table 'project_id_x:dataset_id_y.table_id_z' successfully updated.
14
15
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
14
15