// 连接DB
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2"))
.build();
// 设置查询条件
Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
expressionAttributeValues.put(":val1", new AttributeValue().withS(Constant.STATUS));
expressionAttributeValues.put(":val2", new AttributeValue().withS(Constant.DATE));
Map<String, AttributeValue> lastKeyEvaluated = null;
Integer i = 0;
// 循环查询数据
do {
ScanRequest scanRequest = new ScanRequest()
// 表名
.withTableName(properties.get(Constant.TABLENAME))
// 上一页中最后一个项目的主键值
.withExclusiveStartKey(lastKeyEvaluated)
// where条件
.withFilterExpression("status = :val1 and date = :val2")
// 只查出id和data字段
.withProjectionExpression("id,data")
.withExpressionAttributeValues(expressionAttributeValues);
// 获取返回结果
ScanResult result = client.scan(scanRequest);
for (Map<String, AttributeValue> item : result.getItems()) {
dataList.add(
String.valueOf(item.get("id").getS()) + "," + String.valueOf(item.get("data").getS()));
}
lastKeyEvaluated = result.getLastEvaluatedKey();
i++;
} while (lastKeyEvaluated != null);
参考文档:https://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/Introduction.html