微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

DynamoDB ItemCollection<QueryOutcome> 到 java 对象

如何解决DynamoDB ItemCollection<QueryOutcome> 到 java 对象

我想将 ItemCollection 转换为我的 java pojo 对象。我希望目前通过以下方式这样做。这样做的最佳方法是什么?

  QuerySpec spec = new QuerySpec()
                .withKeyConditionExpression("txn_id = :txnId")
                .withFilterExpression("rrn = :rrn")
                .withValueMap(new ValueMap()
                        .withString(":txnId",txnId)
                        .withString(":rrn",rrn))
                .withConsistentRead(true);
        ItemCollection<QueryOutcome> items = table.query(spec);

这里我有 ItemCollection 。现在我将把它转换成我的 java pojo :-

        Iterator<Item> iter = items.iterator();
        while (iter.hasNext()) {
            String txn = iter.next().toJSON();
            Gson gson = new Gson();
            Test t = gson.fromJson(txn,Test.class);
            return t;
          }

有没有办法像我们在 MysqL 中那样直接将 dynamodb 获取值(ItemCollection)转换为 java pojo?为什么我总是得到 json 然后转换为 pojo。我们不会在其他数据库(如 MysqL 或 oracle DB)中这样做.

文本.java

public class Test implements Serializable {


@DynamoDBAttribute(attributeName = "txn_id")
@JsonProperty("txn_id")
@DynamoDBHashKey
private String txnId;

private String umn;
private String note;

@DynamoDBAttribute(attributeName = "rrn")
private String rrn;

@DynamoDBAttribute(attributeName = "expire_on")
private Date expireOn;



@DynamoDBAttribute(attributeName = "expire_after")
private Integer expireAfter;

@DynamoDBAttribute(attributeName = "created_on")
@DynamoDBAutoGeneratedTimestamp(strategy= DynamoDBAutoGenerateStrategy.CREATE)
private Date createdOn;

}

解决方法

是的,看看DynamoDBMapper。使用相同的 pojo 注释...

CatalogItem partitionKey = new CatalogItem();

partitionKey.setId(102);
DynamoDBQueryExpression<CatalogItem> queryExpression = new DynamoDBQueryExpression<CatalogItem>()
    .withHashKeyValues(partitionKey);

List<CatalogItem> itemList = mapper.query(CatalogItem.class,queryExpression);

for (int i = 0; i < itemList.size(); i++) {
    System.out.println(itemList.get(i).getTitle());
    System.out.println(itemList.get(i).getBookAuthors());
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。