如何在Terraform中为Google Datastream传入多表列表?
如何用Terraform配置Datastream的MySQL多表同步到BigQuery
我正尝试使用Terraform搭建从MySQL到BigQuery的Datastream连接,官方文档展示的示例仅支持配置单个要同步的表,但我需要指定多个包含的表。文档说明mysql_databases块支持传入mysql_tables,但未展示多表写法,想了解实现方式。
解决方案
在同一个mysql_databases块下,重复定义多个mysql_tables嵌套块即可实现多表配置。每个mysql_tables块对应一个要同步的表,你可以为每个表单独指定需要同步的列,也可以省略列配置直接同步全表。
完整配置示例
resource "google_datastream_stream" "default" { depends_on = [ google_kms_crypto_key_iam_member.key_user ] stream_id = "my-stream" desired_state = "NOT_STARTED" location = "us-central1" display_name = "my stream" labels = { key = "value" } source_config { source_connection_profile = google_datastream_connection_profile.source_connection_profile.id mysql_source_config { include_objects { mysql_databases { database = "my-database" # 第一个要同步的表:指定具体列 mysql_tables { table = "includedTable1" mysql_columns { column = "includedColumn1" data_type = "VARCHAR" collation = "utf8mb4" primary_key = false nullable = false ordinal_position = 0 } mysql_columns { column = "includedColumn2" data_type = "INT" primary_key = true nullable = false ordinal_position = 1 } } # 第二个要同步的表:同步全表(省略列配置) mysql_tables { table = "includedTable2" } # 第三个要同步的表:自定义列配置 mysql_tables { table = "includedTable3" mysql_columns { column = "id" data_type = "BIGINT" primary_key = true nullable = false ordinal_position = 0 } } } } } } # 补充BigQuery目标端配置(示例省略部分需自行完善) destination_config { bigquery_destination_config { # 你的BigQuery配置内容 } } }
关键说明
- Terraform中,这类嵌套配置块(如
mysql_tables、mysql_columns)通过重复定义块来表示多个元素,这是Terraform处理嵌套集合的标准方式,无需使用列表/数组语法。 - 若不需要精确指定列,仅保留
table字段即可,Datastream会自动同步该表所有列。 - 每个
mysql_tables块内可定义多个mysql_columns块,用于精准控制要同步的列及其属性。
内容的提问来源于stack exchange,提问作者CClarke
相关产品推荐
相关产品推荐

