2016-11-18 133 views
1

在我的DynamoDB表中,我有2列:一个带有字符串,名为'id',另一个带有整数,名为'price'。从表中的DynamoDB列获取一些整数并计算它们的总和

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

我已经有DynamoDB映射器类:

@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的价格,并计算它们的总和。你能帮我么?

回答

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,可以为所有Product小号取price,总结起来。

相关问题