做任务可以给钱的网站,网站开发规划书,wordpress site-name,网站开发毕设开题报告dynamodb java在上一篇文章中#xff0c;我们有机会发布了一些基本的DynamoDB查询操作。 但是#xff0c;除了基本操作之外#xff0c;DynamoDB api还为我们提供了一些额外的功能。 投影是具有类似选择功能的功能。 您选择应从DynamoDB项中提取哪些属性。 请记住#xf… dynamodb java 在上一篇文章中我们有机会发布了一些基本的DynamoDB查询操作。 但是除了基本操作之外DynamoDB api还为我们提供了一些额外的功能。 投影是具有类似选择功能的功能。 您选择应从DynamoDB项中提取哪些属性。 请记住使用投影不会对您的查询帐单产生任何影响。 public MapString,AttributeValue getRegisterDate(String email) {MapString,String expressionAttributesNames new HashMap();expressionAttributesNames.put(#email,email);MapString,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).withProjectionExpression(registerDate);QueryResult queryResult amazonDynamoDB.query(queryRequest);ListMapString,AttributeValue attributeValues queryResult.getItems();if(attributeValues.size()0) {return attributeValues.get(0);} else {return null;}} 除了选择属性我们还可以根据我们的范围键指定顺序。 我们将使用scanIndexForward以降序查询登录表。 public ListMapString,AttributeValue fetchLoginsDesc(String email) {ListMapString,AttributeValue items new ArrayList();MapString,String expressionAttributesNames new HashMap();expressionAttributesNames.put(#email,email);MapString,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).withScanIndexForward(false);MapString,AttributeValue lastKey null;do {QueryResult queryResult amazonDynamoDB.query(queryRequest);ListMapString,AttributeValue results queryResult.getItems();items.addAll(results);lastKey queryResult.getLastEvaluatedKey();} while (lastKey!null);return items;} 数据库的常见功能是对集合中保留的项目进行计数。 在我们的情况下我们要计算特定用户的登录次数。 但是请特别注意因为计数功能只不过是对已获取的项目总数进行计数因此这将使您好像在获取项目一样花费很多。 public Integer countLogins(String email) {ListMapString,AttributeValue items new ArrayList();MapString,String expressionAttributesNames new HashMap();expressionAttributesNames.put(#email,email);MapString,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).withSelect(Select.COUNT);MapString,AttributeValue lastKey null;QueryResult queryResult amazonDynamoDB.query(queryRequest);ListMapString,AttributeValue results queryResult.getItems();return queryResult.getCount();} DynamoDB的另一个功能是批量获取项目即使它们属于不同的表也是如此。 在属于特定上下文的数据通过不同的表分布的情况下这确实很有用。 每个获取项都作为DynamoDB读取操作进行处理和收费。 如果是批处理获取项目则应指定所有表键因为BatchGetItem的每个查询的目的都是获取单个项目。 public MapString,ListMapString,AttributeValue getMultipleInformation(String email,String name) {MapString,KeysAndAttributes requestItems new HashMap();ListMapString,AttributeValue userKeys new ArrayList();MapString,AttributeValue userAttributes new HashMap();userAttributes.put(email,new AttributeValue().withS(email));userKeys.add(userAttributes);requestItems.put(UserRepository.TABLE_NAME,new KeysAndAttributes().withKeys(userKeys));ListMapString,AttributeValue supervisorKeys new ArrayList();MapString,AttributeValue supervisorAttributes new HashMap();supervisorAttributes.put(name,new AttributeValue().withS(name));supervisorKeys.add(supervisorAttributes);requestItems.put(SupervisorRepository.TABLE_NAME,new KeysAndAttributes().withKeys(supervisorKeys));BatchGetItemRequest batchGetItemRequest new BatchGetItemRequest();batchGetItemRequest.setRequestItems(requestItems);BatchGetItemResult batchGetItemResult amazonDynamoDB.batchGetItem(batchGetItemRequest);MapString,ListMapString,AttributeValue responses batchGetItemResult.getResponses();return responses;} 您可以在github上找到源代码 翻译自: https://www.javacodegeeks.com/2016/07/query-dynamodb-items-java-part-2.htmldynamodb java