简体   繁体   中英

DynamoDB ItemCollection<QueryOutcome> to java object

I want to convert ItemCollection into my java pojo object.I want currently doing so by following way.What is the best way to do so?

  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);

here i got ItemCollection.Now i will be converting it to my 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;
          }

Is there a way to convert dynamodb fetch value( ItemCollection) to java pojo directly like we do in mysql?Why i will always get json then covert to pojo.We don't do so in other DB like mysql or oracle DB.

Text.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;

}

Yes, take a look at DynamoDBMapper . Uses the same pojo annotations...

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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM