【问题标题】:Amazon AWS DynamoDB converting List of Map to ArrayList of objectsAmazon AWS DynamoDB 将 Map 列表转换为对象 ArrayList
【发布时间】:2017-07-28 04:05:56
【问题描述】:

我正在尝试从 Amazon AWS 学习 DynamoDB,并且已经能够成功检索数据,但是我很难将其转换为可用的形式。

我的目标是将结果转换为我的 Data 数据类型的 ArrayList,这是一个具有属性、getter 和 setter 的 ValueObject 类。

谢谢!

Map<String,String> expressionAttributesNames = new HashMap<>();
expressionAttributesNames.put("#network_asset_code","network_asset_code");
expressionAttributesNames.put("#temperature","temperature");

Map<String,AttributeValue> expressionAttributeValues = new HashMap<>();
expressionAttributeValues.put(":network_asset_codeValue", new AttributeValue().withS("17AB05"));
expressionAttributeValues.put(":temperature", new AttributeValue().withN("21"));

ScanRequest scanRequest = new ScanRequest()
   .withTableName("things")
   .withFilterExpression("#network_asset_code = :network_asset_codeValue and #temperature = :temperature")
   .withExpressionAttributeNames(expressionAttributesNames)
   .withExpressionAttributeValues(expressionAttributeValues);


ScanResult scanResult = client.scan(scanRequest);

List<Map<String,AttributeValue>> attributeValues = scanResult.getItems();
ArrayList<Data> dataArray = new ArrayList<>();

for (Map map: attributeValues) {
     Data d = map.values();
     dataArray.add(d);
}

【问题讨论】:

    标签: list amazon-web-services dictionary arraylist amazon-dynamodb


    【解决方案1】:

    您可以使用DynamoDBMapper 使用annotations 自动将 DynamoDB 项目转换为 Java 对象 (POJO)。

    【讨论】:

      【解决方案2】:

      过了一会儿,我就搞定了:

      for (Map map: attributeValues) {
      
                  AttributeValue attr = (AttributeValue) map.get("network_asset_code");
      
                  Data d = new Data();
                  d.network_asset_code = attr.getS();
      
                  dataArray.add(d);
              }
      

      【讨论】:

        猜你喜欢
        • 2020-03-16
        • 2021-11-17
        • 2014-05-20
        • 2020-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-16
        • 1970-01-01
        相关资源
        最近更新 更多