如何通过Terraform创建指定Bigtable选项的BigQuery外部表?
问题
我已通过官方文档中的SQL语句创建了指向Bigtable的BigQuery外部表,语句如下:
CREATE EXTERNAL TABLE mydataset.BigtableTable OPTIONS ( format = 'CLOUD_BIGTABLE', uris = ['https://googleapis.com/bigtable/projects/myproject/instances/myBigtableInstance/tables/table1'], bigtable_options = """ { bigtableColumnFamilies: [ { "familyId": "familyId1", "type": "INTEGER", "encoding": "BINARY" } ], readRowkeyAsString: true } """ );
请问是否可以通过Terraform实现该操作?若可以,配置代码是什么样的?我在Terraform Google Provider文档中未找到指定Bigtable选项的方式,但至少需要配置readRowkeyAsString: true。
回答
可以通过Terraform实现该操作,使用google_bigquery_table资源,通过external_data_configuration块配置Bigtable外部表参数,其中bigtable_options支持HCL结构化配置或JSON字符串两种形式。
方式一:HCL结构化配置(推荐)
这种方式更贴合Terraform语法风格,便于维护:
resource "google_bigquery_table" "bigtable_external_table" { dataset_id = "mydataset" table_id = "BigtableTable" external_data_configuration { source_format = "CLOUD_BIGTABLE" uris = ["https://googleapis.com/bigtable/projects/myproject/instances/myBigtableInstance/tables/table1"] bigtable_options { read_rowkey_as_string = true column_families { family_id = "familyId1" type = "INTEGER" encoding = "BINARY" } } } }
方式二:JSON字符串配置
如果需要复用原有SQL中的JSON配置结构,可使用bigtable_options_json参数:
resource "google_bigquery_table" "bigtable_external_table" { dataset_id = "mydataset" table_id = "BigtableTable" external_data_configuration { source_format = "CLOUD_BIGTABLE" uris = ["https://googleapis.com/bigtable/projects/myproject/instances/myBigtableInstance/tables/table1"] bigtable_options_json = jsonencode({ bigtableColumnFamilies = [ { familyId = "familyId1" type = "INTEGER" encoding = "BINARY" } ], readRowkeyAsString = true }) } }
关键配置对应说明
source_format对应SQL中的format = 'CLOUD_BIGTABLE'uris直接复用Bigtable资源路径read_rowkey_as_string(HCL方式)或readRowkeyAsString(JSON方式)就是你需要的核心配置项
内容的提问来源于stack exchange,提问作者dave-edison
相关产品推荐
相关产品推荐

