public void addUser(String id, Date date) {
Map<String,AttributeValue> attributeValues = new HashMap<>();
attributeValues.put("id",new AttributeValue().withS(id));
attributeValues.put("timestamp",new AttributeValue().withN(Long.toString(date.getTime())));
PutItemRequest putItemRequest = new PutItemRequest()
.withTableName(TABLE_NAME)
.withItem(attributeValues);
PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
}
public Integer countUsers(String id) {
List<Map<String,AttributeValue>> items = new ArrayList<>();
Map<String,String> expressionAttributesNames = new HashMap<>();
expressionAttributesNames.put("#id","id");
Map<String,AttributeValue> expressionAttributeValues = new HashMap<>();
expressionAttributeValues.put(":idValue",new AttributeValue().withS(id));
QueryRequest queryRequest = new QueryRequest()
.withTableName(TABLE_NAME)
.withKeyConditionExpression("#id = :idValue")
.withExpressionAttributeNames(expressionAttributesNames)
.withExpressionAttributeValues(expressionAttributeValues)
.withSelect(Select.COUNT);
Map<String,AttributeValue> lastKey = null;
QueryResult queryResult = dynamoDB.query(queryRequest);
List<Map<String,AttributeValue>> results = queryResult.getItems();
return queryResult.getCount();
}
public List<Map<String,AttributeValue>> fetchUsersDesc(String id) {
List<Map<String,AttributeValue>> items = new ArrayList<>();
Map<String,String> expressionAttributesNames = new HashMap<>();
expressionAttributesNames.put("#id","id");
Map<String,AttributeValue> expressionAttributeValues = new HashMap<>();
expressionAttributeValues.put(":idValue",new AttributeValue().withS(id));
QueryRequest queryRequest = new QueryRequest()
.withTableName(TABLE_NAME)
.withKeyConditionExpression("#id = :idValue")
.withExpressionAttributeNames(expressionAttributesNames)
.withExpressionAttributeValues(expressionAttributeValues)
.withScanIndexForward(false);
Map<String,AttributeValue> lastKey = null;
do {
QueryResult queryResult = dynamoDB.query(queryRequest);
List<Map<String,AttributeValue>> results = queryResult.getItems();
items.addAll(results);
lastKey = queryResult.getLastEvaluatedKey();
queryRequest.withExclusiveStartKey(lastKey);
} while (lastKey!=null);
return items;
}
public List<Map<String ,AttributeValue>> queryUsersBetween(String id, Date from, Date to) {
List<Map<String,AttributeValue>> items = new ArrayList<>();
Map<String,String> expressionAttributesNames = new HashMap<>();
expressionAttributesNames.put("#id","id");
expressionAttributesNames.put("#timestamp","timestamp");
Map<String,AttributeValue> expressionAttributeValues = new HashMap<>();
expressionAttributeValues.put(":idValue",new AttributeValue().withS(id));
expressionAttributeValues.put(":from",new AttributeValue().withN(Long.toString(from.getTime())));
expressionAttributeValues.put(":to",new AttributeValue().withN(Long.toString(to.getTime())));
QueryRequest queryRequest = new QueryRequest()
.withTableName(TABLE_NAME)
.withKeyConditionExpression("#id = :idValue and #timestamp BETWEEN :from AND :to ")
.withExpressionAttributeNames(expressionAttributesNames)
.withExpressionAttributeValues(expressionAttributeValues);
Map<String,AttributeValue> lastKey = null;
do {
QueryResult queryResult = dynamoDB.query(queryRequest);
List<Map<String,AttributeValue>> results = queryResult.getItems();
items.addAll(results);
lastKey = queryResult.getLastEvaluatedKey();
queryRequest.setExclusiveStartKey(lastKey);
} while (lastKey!=null);
return items;
}
public List<String> scanUsers(Date date) {
List<String> ids = new ArrayList<>();
Map<String, String> attributeNames = new HashMap<String, String >();
attributeNames.put("#timestamp", "timestamp");
Map<String, AttributeValue> attributeValues = new HashMap<String, AttributeValue>();
attributeValues.put(":from", new AttributeValue().withN(Long.toString(date.getTime())));
ScanRequest scanRequest = new ScanRequest()
.withTableName(TABLE_NAME)
.withFilterExpression("#timestamp < :from")
.withExpressionAttributeNames(attributeNames)
.withExpressionAttributeValues(attributeValues)
.withProjectionExpression("id");
Map<String,AttributeValue> lastKey = null;
do {
ScanResult scanResult = dynamoDB.scan(scanRequest);
List<Map<String,AttributeValue>> results = scanResult.getItems();
results.forEach(r->ids.add(r.get("id").getS()));
lastKey = scanResult.getLastEvaluatedKey();
scanRequest.setExclusiveStartKey(lastKey);
} while (lastKey!=null);
return ids;
}
dynamodb QueryRequest & ScanRequest
猜你喜欢
转载自blog.csdn.net/zhouyan8603/article/details/82287458
今日推荐
周排行