从表上的DynamoDB列中获取一些整数并计算它们的总和

时间:2016-11-18 08:30:33

标签: android amazon-dynamodb

在我的DynamoDB表中,我有两列:一列是字符串,名为' id',另一列是整数,名为' price'。

在我的应用中,我有ID,保存在String[]中。我想得到每个ID的价格并计算它们的总和。

我已经有了DynamoDB Mapper类:

@DynamoDBTable(tableName = "Products")
public class Product {

    private String id;
    private String price;

    @DynamoDBAttribute(attributeName = "id")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @DynamoDBAttribute(attributeName = "price")
    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }
}

我需要知道如何才能获得数组中ID的价格,并计算它们的总和。你能帮我吗?

1 个答案:

答案 0 :(得分:0)

DynamoDB无法为您进行求和,因此您需要在应用层处理它。

伪代码(可能无法按原样编译):

List<Product> itemsToGet;
for (int i = 0; i < idArray.length; i++) {
    Product p = new Product();
    p.setId(idArray[i]);

    itemsToGet.add(p);
}
Map<String, List<Object>> items = dynamoDBMapper.batchLoad(itemsToGet);
Map.Entry<String, List<Object>> firstEntry = items.entrySet().iterator().next();  // the Map will contain only one entry, because you are querying for only one table.
List<Product> productsFromDynamoDB = firstEntry.getValue();

productsFromDynamoDB开始,您可以为所有price获取Product并对其进行总结。