使用java api:QueryRequest查询dynamodb时,无论什么情况下,必须得set hashkey
1.简单查询(hashkey或者hashkey+rangekey或者hashkey+attribute):
public Map<String,AttributeValue> getOne(String email) {
Map<String,String> expressionAttributesNames = new HashMap<>();
expressionAttributesNames.put("#email","email");
Map<String,AttributeValue> expressionAttributeValues = new HashMap<>();
expressionAttributeValues.put(":emailValue",new AttributeValue().withS(email));
QueryRequest queryRequest = new QueryRequest()
.withTableName(TABLE_NAME)
.withKeyConditionExpression("#email = :emailValue")
.withExpressionAttributeNames(expressionAttributesNames)
.withExpressionAttributeValues(expressionAttributeValues);
QueryResult queryResult = amazonDynamoDB.query(queryRequest);
List<Map<String,AttributeValue>> attributeValues = queryResult.getItems();
if(attributeValues.size()>0) {
return attributeValues.get(0);
} else {
return null;
}
}
2.复杂查询(hashkey+非hashkey的属性条件)
public List<Map<String ,AttributeValue>> queryBetween(String email, Date from, Date to) {
List<Map<String,AttributeValue>> items = new ArrayList<>();
val queryRequest = new QueryRequest()
val expressionAttributesNames = new util.HashMap[String, String]()
expressionAttributesNames.put("#userId", "userId")
expressionAttributesNames.put("#appName", "appName")
val expressionAttributeValues = new util.HashMap[String, AttributeValue]()
expressionAttributeValues.put(":v_userId", new AttributeValue().withS(uid))
expressionAttributeValues.put(":v_appName", new AttributeValue().withS(appName))
queryRequest.withTableName(tableName)
.withConsistentRead(false)
.withKeyConditionExpression("#userId = :v_userId")
.withFilterExpression("#appName = :v_appName")
.withExpressionAttributeNames(expressionAttributesNames)
.withExpressionAttributeValues(expressionAttributeValues)
Map<String,AttributeValue> lastKey = null;
do {
QueryResult queryResult = amazonDynamoDB.query(queryRequest);
List<Map<String,AttributeValue>> results = queryResult.getItems();
items.addAll(results);
lastKey = queryResult.getLastEvaluatedKey();
queryRequest.setExclusiveStartKey(lastKey);
} while (lastKey!=null);
return items;
}
需要注意的是:dynamodb查询的时候是按照page进行返回的,如果查询结果有多页,则返回的时候需要last evaluated key 做判断处理
3.SCAN
public void downloadAllRecords() throws InterruptedException, IOException {
final Object[] FILE_HEADER = {"id", "userId", "createdDate"};
CSVFormat csvFormat = CSVFormat.DEFAULT.withRecordSeparator("\n");
CSVPrinter csvPrinter = new CSVPrinter(new FileWriter(tableName + ".csv"), csvFormat);
csvPrinter.printRecord(FILE_HEADER);
ScanRequest scanRequest = new ScanRequest()
.withTableName(tableName)
.withConsistentRead(false)
.withLimit(100)
.withAttributesToGet("id", "userId", "createdDate");
int counter = 0;
do {
ScanResult result = client.scan(scanRequest);
Map<String, AttributeValue> lastEvaluatedKey = result.getLastEvaluatedKey();
for (Map<String, AttributeValue> item : result.getItems()) {
List record = new ArrayList();
System.out.println(item);
AttributeValue userIdAttribute = item.getOrDefault("userId", new AttributeValue());
AttributeValue idAttribute = item.getOrDefault("id", new AttributeValue());
AttributeValue createdDateAttribute = item.getOrDefault("createdDate", new AttributeValue());
record.add(idAttribute.getS());
record.add(userIdAttribute.getS());
record.add(createdDateAttribute.getS());
csvPrinter.printRecord(record);
TimeUnit.MILLISECONDS.sleep(50);
}
scanRequest.setExclusiveStartKey(lastEvaluatedKey);
} while (scanRequest.getExclusiveStartKey() != null);
csvPrinter.flush();
csvPrinter.close();
System.out.println("CSV file generated successfully.");
}