使用AWS DMS从CSV(S3)到DynamoDB的数据迁移

发布于 2025-02-11 20:45:34 字数 2346 浏览 2 评论 0原文

当我迁移一张 json 的表格时,我正面临一个问题,该表应将其插入为 dyanmodb 地图。源数据采用以下格式

<table border="1"><tr><th>userId</th><th>brandId</th><th>createdBy</th><th>modifiedBy</th><th>preferences</th><th>version</th></tr><tr><td>TU0001</td><td>TEST BRAND</td><td>{"channel":{"S":"website"},"date":{"S": "2022-06-13T08:16:26.300Z"},"userId":{"S":"TU0001"}}</td><td>{"channel":{"S":"website"},"date":{"S": "2022-06-13T015:26:10.200Z"},"userId":{"S":"TU0001"}}</td><td>{"Colour": {"S": "Red" },"Size":{"S": "XL" }}</td><td>1</td></tr></table>

DynamoDB中的表位于SAM结构中,只是将JSON值存储为MAP(DynamoDB-MAP)。当我们使用DMS迁移时,它将JSON作为字符串值插入,而不是按预期映射。

我已经定义了转换规则如下:

"attribute-mappings": [
      {
        "target-attribute-name": "userId",
        "attribute-type": "scalar",
        "attribute-sub-type": "string",
        "value": "${userId}"
      },
      {
        "target-attribute-name": "brandId",
        "attribute-type": "scalar",
        "attribute-sub-type": "string",
        "value": "${brandId}"
      },
      {
        "target-attribute-name": "createdBy",
        "attribute-type": "document",
        "attribute-sub-type": "dynamodb-map",
        "value": {
          "M": {
            "S": "${createdBy}"
          }
        }
      },
      {
        "target-attribute-name": "modifiedBy",
        "attribute-type": "document",
        "attribute-sub-type": "dynamodb-map",
        "value": {
          "M": {
            "S": "${modifiedBy}"
          }
        }
      },
      {
        "target-attribute-name": "preferences",
        "attribute-type": "document",
        "attribute-sub-type": "dynamodb-map",
        "value": {
          "M": {
            "S": "${preferences}"
          }
        }
      },
      {
        "target-attribute-name": "version",
        "attribute-type": "scalar",
        "attribute-sub-type": "number",
        "value": "${version}"
      }
    ]

我还尝试在下面添加地图,并在Dyanmodb中添加一个空的地图。

"value": {
          "M": "${preferences}"
        }

希望有人可以帮忙。

I am facing an issue when I am migrating a table consisting a JSON in a column which should be inserted in as a DyanmoDB Map. The source data is in the following format

<table border="1"><tr><th>userId</th><th>brandId</th><th>createdBy</th><th>modifiedBy</th><th>preferences</th><th>version</th></tr><tr><td>TU0001</td><td>TEST BRAND</td><td>{"channel":{"S":"website"},"date":{"S": "2022-06-13T08:16:26.300Z"},"userId":{"S":"TU0001"}}</td><td>{"channel":{"S":"website"},"date":{"S": "2022-06-13T015:26:10.200Z"},"userId":{"S":"TU0001"}}</td><td>{"Colour": {"S": "Red" },"Size":{"S": "XL" }}</td><td>1</td></tr></table>

The table in DynamoDB is in the sam structure except that the JSON values are stored as MAP (dynamodb-map). When we migrate using DMS, it inserts the JSON as string value and not MAP as expected.

I have defined the transformation rule as follows:

"attribute-mappings": [
      {
        "target-attribute-name": "userId",
        "attribute-type": "scalar",
        "attribute-sub-type": "string",
        "value": "${userId}"
      },
      {
        "target-attribute-name": "brandId",
        "attribute-type": "scalar",
        "attribute-sub-type": "string",
        "value": "${brandId}"
      },
      {
        "target-attribute-name": "createdBy",
        "attribute-type": "document",
        "attribute-sub-type": "dynamodb-map",
        "value": {
          "M": {
            "S": "${createdBy}"
          }
        }
      },
      {
        "target-attribute-name": "modifiedBy",
        "attribute-type": "document",
        "attribute-sub-type": "dynamodb-map",
        "value": {
          "M": {
            "S": "${modifiedBy}"
          }
        }
      },
      {
        "target-attribute-name": "preferences",
        "attribute-type": "document",
        "attribute-sub-type": "dynamodb-map",
        "value": {
          "M": {
            "S": "${preferences}"
          }
        }
      },
      {
        "target-attribute-name": "version",
        "attribute-type": "scalar",
        "attribute-sub-type": "number",
        "value": "${version}"
      }
    ]

I also tried adding the map as below, and it adds an empty map in DyanmoDB.

"value": {
          "M": "${preferences}"
        }

Hope someone can help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄风 2025-02-18 20:45:34

我认为DMS不支持将JSON字符串直接转换为发电机图。在这里,DMS将JSON数据插入字符串,而不是转换为DynamoDB地图或列表类型。但是,您可以在迁移后之前处理数据,以确保将JSON数据正确解析到Dyanamodb映射中。

迁移之前:

  • 源数据库中的过程数据将JSON字符串转换为一种格式,该格式
    DM可以处理。
  • 编写脚本(JS/Python)以读取您的数据
    源,将JSON转换为DynamoDB映射,然后写入DynamoDB。

迁移后:

  • 读取来自dynamodb的数据,然后将JSON字符串放入映射,然后
    然后写回DynamoDB。
  • 编写一个脚本,该脚本每次在dynamodb表中每次都在
    检索字符串数据,将其转换为地图,然后更新项目
    在DynamoDB中。

以下是一个node.js,具有AWS SDK的“后处理”示例,可以在迁移后完成,您可以做:

const AWS = require('aws-sdk');

AWS.config.update({
  region: "your_region",
  // Add your credentials
});

const dynamodb = new AWS.DynamoDB.DocumentClient();
const tableName = 'your_table'; // replace with your table name

const scanAndProcessItems = async (startKey) => {
  try {
    const params = {
      TableName: tableName,
      ExclusiveStartKey: startKey
    };
    
    const data = await dynamodb.scan(params).promise();
    
    if (data.Items) {
      for (let item of data.Items) {
        // To parse JSON strings into DynamoDB Maps
        item.createdBy = JSON.parse(item.createdBy);
        item.modifiedBy = JSON.parse(item.modifiedBy);
        item.preferences = JSON.parse(item.preferences);
    
        // To write the item back to the table
        await dynamodb.put({ TableName: tableName, Item: item }).promise();
      }
    }
    
    // If there's more data to scan, it will recursively call this function
    if (data.LastEvaluatedKey) {
      await scanAndProcessItems(data.LastEvaluatedKey);
    }
    
  } catch (err) {
    console.error("An error occurred", err);
  }
};

scanAndProcessItems();

希望这对您有帮助。

I think DMS does not support direct conversion of JSON strings to Dynamo maps. Here DMS inserting JSON data as strings instead of converting into DynamoDB Map or List type. But you can process the data before or after migration to make sure that JSON data is correctly parsed into DyanamoDB Map.

Before Migration:

  • Process data in source db to convert JSON strings to a format that
    DMS can handle.
  • Write a script (JS/Python) to read data from your
    source, transform JSON into DynamoDB map and then write to DynamoDB.

OR

After Migration:

  • Read the data from DynamoDB, and then parse JSON string into Map, and
    then write back to DynamoDB.
  • Write a script which itereate over every time in the DynamoDB table,
    retrieve the string data, convert it into a Map, and then update item
    in the DynamoDB.

Below is an Node.js with AWS SDK example of 'Post Processing' that can be done after migration, you can do:

const AWS = require('aws-sdk');

AWS.config.update({
  region: "your_region",
  // Add your credentials
});

const dynamodb = new AWS.DynamoDB.DocumentClient();
const tableName = 'your_table'; // replace with your table name

const scanAndProcessItems = async (startKey) => {
  try {
    const params = {
      TableName: tableName,
      ExclusiveStartKey: startKey
    };
    
    const data = await dynamodb.scan(params).promise();
    
    if (data.Items) {
      for (let item of data.Items) {
        // To parse JSON strings into DynamoDB Maps
        item.createdBy = JSON.parse(item.createdBy);
        item.modifiedBy = JSON.parse(item.modifiedBy);
        item.preferences = JSON.parse(item.preferences);
    
        // To write the item back to the table
        await dynamodb.put({ TableName: tableName, Item: item }).promise();
      }
    }
    
    // If there's more data to scan, it will recursively call this function
    if (data.LastEvaluatedKey) {
      await scanAndProcessItems(data.LastEvaluatedKey);
    }
    
  } catch (err) {
    console.error("An error occurred", err);
  }
};

scanAndProcessItems();

Hope this will help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文