【问题标题】:How to convert a POJO to Map<String, AttributeValue> for dynamo db?如何将 POJO 转换为 dynamo db 的 Map<String, AttributeValue>?
【发布时间】:2019-09-05 19:21:02
【问题描述】:

我正在为 dynamodb 使用事务。并且事务 Put 请求将 com.amazonaws.services.dynamodbv2.document.Item 作为输入参数。所以,我需要将 POJO 转换为 Map

到目前为止,我已经尝试使用 Jackson 将对象转换为字符串,然后将字符串转换为项目。 以下是我尝试过的代码。

        ObjectMapper objectMapper = new ObjectMapper();
        String jsonStr = null;
        try {
            jsonStr = objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        Item item = new Item().withJSON("document", jsonStr);
        Map<String,AttributeValue> attributes = ItemUtils.toAttributeValues(item);
    return attributes.get("document").getM();

问题是,“Set”类型的字段转换后返回“List”。 有什么建议可以解决这个问题吗?

【问题讨论】:

标签: java amazon-dynamodb


【解决方案1】:

以下代码应该可以解决您的转换问题:

Map<String, AttributeValue> valueMap = ItemUtils.toAttributeValues(item);
CustomEntity entity = dynamoDBMapper.marshallIntoObject(CustomEntity.class, valueMap);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    相关资源
    最近更新 更多