如何在java dynamodb查询中从AttributeValue映射中获取多个值?
我想从dynamo db表中获取所有项目。我用java编写了一个查询,如下所示,它可以正常工作。但问题是它没有将所有列添加到AttributeValue映射中。它只有第一列(键)。所以我在这里做的是按循环中的键搜索每个项。因此,当我搜索循环中的每一项时,如果您的表有数百万个数据,则效率低下。我能做些什么来避免这种情况? 注意:“名称”是表中的第一列。(主键) 我尝试获取“count”,就像我喜欢“name”一样,但它没有为“count”返回任何内容,而是为null如何在java dynamodb查询中从AttributeValue映射中获取多个值?,java,amazon-web-services,Java,Amazon Web Services,我想从dynamo db表中获取所有项目。我用java编写了一个查询,如下所示,它可以正常工作。但问题是它没有将所有列添加到AttributeValue映射中。它只有第一列(键)。所以我在这里做的是按循环中的键搜索每个项。因此,当我搜索循环中的每一项时,如果您的表有数百万个数据,则效率低下。我能做些什么来避免这种情况? 注意:“名称”是表中的第一列。(主键) 我尝试获取“count”,就像我喜欢“name”一样,但它没有为“count”返回任何内容,而是为null Table table
Table table = dynamoDb.getTable(DYNAMODB_TABLE_NAME);
ScanRequest scanRequest = new ScanRequest(DYNAMODB_TABLE_NAME);
ArrayList<DeviceResponse>deviceResponses=new ArrayList<>();
Map<String, AttributeValue> exclusiveStartKey = null;
do {
final ScanResult scanResult = client.scan(scanRequest);
List<Map<String, AttributeValue>> items = scanResult.getItems();
for (int i = 0; i < items.size(); i++) {
Map<String, AttributeValue> map = items.get(i);
AttributeValue value = map.get("name");
String name = value.getS();
Item item = table.getItem("name", name); //Searching item
int count = item.getInt("count"); //getting count from item
DeviceResponse deviceResponse = new DeviceResponse();
deviceResponse.setName(name);
deviceResponse.setCount(count);
deviceResponses.add(deviceResponse);
}
exclusiveStartKey = scanResult.getLastEvaluatedKey();
// Reusing same request object, just setting the start key
scanRequest.setExclusiveStartKey(exclusiveStartKey);
} while(exclusiveStartKey != null);
return deviceResponses;
Table Table=dynamoDb.getTable(dynamoDb\u Table\u NAME);
ScanRequest ScanRequest=新的ScanRequest(DYNAMODB_表_名称);
ArrayList DeviceResponses=新的ArrayList();
Map exclusiveStartKey=null;
做{
最终扫描结果ScanResult=client.scan(扫描请求);
List items=scanResult.getItems();
对于(int i=0;i
我试图得到“计数”,因为我喜欢“名字”
不,你没有。此代码可从中获取名称
:
AttributeValue value = map.get("name");
String name = value.getS();
与您尝试获取计数的代码不同:
Item item = table.getItem("name", name); //Searching item
int count = item.getInt("count"); //getting count from item
您正在获取name
字段,然后出于某种原因对数据库执行另一个查询。所有属性都在Map
对象中。如果希望以与获得名为name
的属性相同的方式获得名为count
的属性,则代码如下:
AttributeValue value = map.get("count");
int count = value.getN();
或者简化一下:
int count = map.get("count").getN();