LoginSignup
7
1

More than 5 years have passed since last update.

[メモ]BigQueryの配列型、構造体型

Posted at

下記のような形式のオブジェクトをBigQueryに入れたい。(オブジェクトの配列を含んでいる)

{
  "id": "1",
  "params": [
    {
      "key": "key1",
      "values": [
        "val1",
        "val2"
      ]
    },
    {
      "key": "key2",
      "values": [
        "val1",
        "val2"
      ]
    }
  ]
}

フィールドの定義はどのようにすればよいか。

フィールド定義

GCPのWebコンソール上での設定を想定している。

Name Type Mode
id STRING REQUIRED
params RECORD REPEATED
params.key STRING REQUIRED
params.values STRING REPEATED

params.*をWebコンソールから定義する際は、paramsを定義した箇所のRECORDの右側の+から追加する。

RECORD型

「1つ以上の他のフィールドのコレクション」(https://cloud.google.com/bigquery/data-types?hl=ja)

標準SQLでは、「構造体型」になっている。
https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types?hl=ja#struct-type

REPEATED

繰り返しフィールド。(https://cloud.google.com/bigquery/docs/legacy-nested-repeated?hl=ja)

標準SQLでは、「配列型(Array)」になっている。
https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types?hl=ja#array-type

繰り返しフィールドを二つ以上もつ場合、そのままSELECT * FROM table_nameなどとすることはできず、フラット化などを行う必要がある。

下記のエラーが発生する。

Error: Cannot output multiple independently repeated fields at the same time.

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