如何在我的 BigQuery terraform 文件中创建类型为 INTEGER 的记录

How to create Type RECORD of INTEGER in my terraform file for BigQuery

我正在尝试为我的 BigQuery 创建 Terraform 架构 table,我需要一个 RECORD 类型的列,它将由 INTEGER 填充。 有问题的字段将采用括号格式,其中整数可以是一个或多个,用逗号分隔:[1]

我试过这样写:

resource "google_bigquery_table" "categories" {
  project = "abcd-data-ods-${terraform.workspace}"
  dataset_id = google_bigquery_dataset.bq_dataset_op.dataset_id
  table_id = "categories"

schema = <<EOF
    [
        {"type":"STRING","name":"a","mode":"NULLABLE"},
        {"type":"RECORD[INTEGER]","name":"b","mode":"NULLABLE"}
    ]
EOF

}

像这样:

resource "google_bigquery_table" "categories" {
  project = "abcd-data-ods-${terraform.workspace}"
  dataset_id = google_bigquery_dataset.bq_dataset_op.dataset_id
  table_id = "categories"

schema = <<EOF
    [
        {"type":"STRING","name":"a","mode":"NULLABLE"},
        {"type":"RECORD","name":"b","mode":"NULLABLE"}
    ]
EOF

}

但它没有用,因为我在 gitlab 上的 CI/CD 中不断收到错误消息 第一次尝试的错误:

Error: googleapi: Error 400: Invalid value for type: RECORD[INTEGER] is not a valid value, invalid

第二次尝试的错误:

Error: googleapi: Error 400: Field b is type RECORD but has no schema, invalid

我认为第二个实现是最接近错误的解决方案,但它仍然缺少一些东西 有没有人知道正确的申报方式

正如第二个错误所述:

Error: googleapi: Error 400: Field b is type RECORD but has no schema, invalid

您必须为 RECORD 类型提供架构 (you can read more on the docs)。例如,一个有效的例子可以是:

resource "google_bigquery_table" "categories" {
  project    = "abcd-data-ods-${terraform.workspace}"
  dataset_id = google_bigquery_dataset.bq_dataset_op.dataset_id
  table_id   = "categories"

  schema = <<EOF
      [
          {
            "type":"STRING",
            "name":"a",
            "mode":"NULLABLE"
          },
          {
            "type":"RECORD",
            "name":"b",
            "mode":"NULLABLE",
            "fields": [{
                "name": "c",
                "type": "INTEGER",
                "mode": "NULLABLE"
            }]
          }
      ]
  EOF
}

希望能帮到你。