如何使用 Terraform 在 AWS DMS 上加入多个 JSON 文件以进行表映射? [英] How do I join multiple JSON files for table mapping on AWS DMS using Terraform?

查看:12
本文介绍了如何使用 Terraform 在 AWS DMS 上加入多个 JSON 文件以进行表映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了创建 DMS 的复制任务,我使用了这个资源:

In order to create a replication task of DMS, I use this resource:

resource "aws_dms_replication_task" "test" {
  migration_type            = "full-load"
  replication_instance_arn  = aws_dms_replication_instance.test-dms-replication-instance-tf.replication_instance_arn
  replication_task_id       = "test-dms-replication-task-tf"
  source_endpoint_arn       = aws_dms_endpoint.test-dms-source-endpoint-tf.endpoint_arn
  table_mappings           = file("${path.module}/db1/test1.json")

  tags = {
    Name = "test"
  }

  target_endpoint_arn = aws_dms_endpoint.test-dms-target-endpoint-tf.endpoint_arn
}

schema生成的JSON文件有很多,比如:

There are many JSON files generated by the schema, such as:

./db1/test1.json

{
  "rules": [
    {
      "rule-type": "transformation",
      "rule-id": "1",
      "rule-name": "Rule name 1",
      "rule-action": "rename",
      "rule-target": "schema",
      "object-locator": {
        "schema-name": "SCHEMA_1",
        "table-name": "TABLE_1"
      },
      "value": "main"
    }
  ]
}

./db1/test2.json

{
  "rules": [
    {
      "rule-type": "transformation",
      "rule-id": "2",
      "rule-name": "Rule name 2",
      "rule-action": "rename",
      "rule-target": "schema",
      "object-locator": {
        "schema-name": "SCHEMA_2",
        "table-name": "TABLE_2"
      },
      "value": "main"
    }
  ]
}

最后,我想得到一个完整的 rules JSON 输出,比如

Finally, I want to get a full rules JSON output like

{
  "rules": [
    {
      "rule-type": "transformation",
      "rule-id": "1",
      "rule-name": "Rule name 1",
      "rule-action": "rename",
      "rule-target": "schema",
      "object-locator": {
        "schema-name": "SCHEMA_1",
        "table-name": "TABLE_1"
      },
      "value": "main"
    },
    {
      "rule-type": "transformation",
      "rule-id": "2",
      "rule-name": "Rule name 2",
      "rule-action": "rename",
      "rule-target": "schema",
      "object-locator": {
        "schema-name": "SCHEMA_2",
        "table-name": "TABLE_2"
      },
      "value": "main"
    }
  ]
}

看来我需要合并子元素.

It seems that I need to merge the child elements.

如何在不使用 Terraform 手动将它们放入 1 个 JSON 文件的情况下将这些文件连接在一起?

How do I join these files together without putting them manually inside 1 JSON file using Terraform?

推荐答案

可能有几种方法可以做到这一点.一种是:

There are probably several ways of doing this. One would be as follows:

locals {

    rule_files = ["${path.module}/test1.json", "${path.module}/test2.json"]

    rules_joined = {
                    rules = flatten([for fname in local.rule_files: jsondecode(file(fname))["rules"]])
                   }
}

local.rules_joined 设为:

{
  "rules" = [
    {
      "object-locator" = {
        "schema-name" = "SCHEMA_1"
        "table-name" = "TABLE_1"
      }
      "rule-action" = "rename"
      "rule-id" = "1"
      "rule-name" = "Rule name 1"
      "rule-target" = "schema"
      "rule-type" = "transformation"
      "value" = "main"
    },
    {
      "object-locator" = {
        "schema-name" = "SCHEMA_2"
        "table-name" = "TABLE_2"
      }
      "rule-action" = "rename"
      "rule-id" = "2"
      "rule-name" = "Rule name 2"
      "rule-target" = "schema"
      "rule-type" = "transformation"
      "value" = "main"
    },
  ]
}

要将其变成字符串,请使用 jsonencode(local.rules_joined).

To make it into a string, use jsonencode(local.rules_joined).

这篇关于如何使用 Terraform 在 AWS DMS 上加入多个 JSON 文件以进行表映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆