如何在Terraform中创建结构字段

问题描述 投票:0回答:1

我正在编写一个Terraform来创建bigquery表。我在terraform资源中有架构。我无法弄清楚如何在架构中创建struct字段?

这是我到目前为止所拥有的。

resource "google_bigquery_table" "my-table" {
  dataset_id = google_bigquery_dataset.my-dataset
  table_id   = "name"

  time_partitioning {
    type = "DAY"
  }

  schema = <<EOF
  [
    {
      "name": "id",
      "type": "STRING",
      "mode": "NULLABLE"
    },
    {
      "name": "struct_field_to_contain_2_values",
      "type": "STRUCT(FLOAT64)",
      "mode": "NULLABLE"
    },
  ]
  EOF
}
google-cloud-platform google-bigquery terraform
1个回答
0
投票

模式可能像这样:

[
    {
        "name": "id",
        "type": "STRING"
    },
    {
        "name": "struct_field_to_contain_2_values",
        "type": "RECORD",
        "fields": [
            {
                "name": "value",
                "type": "FLOAT"
            }
        ]
    }
]

考虑到结构的数据类型定义为RECORD,并且您需要为此结构指定值,在示例中,您可以看到struct_field_to_contain_2_values已定义了嵌套字段(FLOAT64)的[value。

您可以使用Bigquery UI创建架构,然后使用选项“以文本形式编辑”以JSON结构形式获取架构。而且,在Terraform documentation中有一个关于使用模式字段的注释:

由于此字段需要JSON字符串,因此对该字符串的任何更改即使JSON本身未更改,也会创建一个差异。如果API对于相同的架构,返回不同的值,例如它切换了值的顺序或将STRUCT字段类型替换为RECORD字段类型,我们目前无法消除这种情况引起的重复差异。作为一个解决方法,我们建议使用API​​返回的架构。

© www.soinside.com 2019 - 2024. All rights reserved.