使用Java第2部分查询DynamoDB项
生活随笔
收集整理的這篇文章主要介紹了
使用Java第2部分查询DynamoDB项
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在上一篇文章中,我們有機會發布了一些基本的DynamoDB查詢操作。
但是,除了基本操作之外,DynamoDB api還為我們提供了一些額外的功能。
投影是具有類似選擇功能的功能。
您選擇應從DynamoDB項中提取哪些屬性。 請記住,使用投影不會對您的查詢帳單產生任何影響。
除了選擇屬性,我們還可以根據我們的范圍鍵指定順序。 我們將使用scanIndexForward以降序查詢登錄表。
public List<Map<String,AttributeValue>> fetchLoginsDesc(String email) {List<Map<String,AttributeValue>> items = new ArrayList<>();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).withScanIndexForward(false);Map<String,AttributeValue> lastKey = null;do {QueryResult queryResult = amazonDynamoDB.query(queryRequest);List<Map<String,AttributeValue>> results = queryResult.getItems();items.addAll(results);lastKey = queryResult.getLastEvaluatedKey();} while (lastKey!=null);return items;}數據庫的常見功能是對集合中保留的項目進行計數。 在我們的情況下,我們要計算特定用戶的登錄次數。 但是,請特別注意,因為計數功能只不過是對已提取的項目總數進行計數,因此,這將使您像在提取項目一樣花費很多。
public Integer countLogins(String email) {List<Map<String,AttributeValue>> items = new ArrayList<>();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).withSelect(Select.COUNT);Map<String,AttributeValue> lastKey = null;QueryResult queryResult = amazonDynamoDB.query(queryRequest);List<Map<String,AttributeValue>> results = queryResult.getItems();return queryResult.getCount();}DynamoDB的另一個功能是批量獲取項目,即使它們屬于不同的表也是如此。 在屬于特定上下文的數據通過不同的表分布的情況下,這確實很有用。 每個獲取項都作為DynamoDB讀取操作進行處理和收費。 如果是批量獲取項,則應指定所有表鍵,因為BatchGetItem的每個查詢的目的都是獲取單個項。
public Map<String,List<Map<String,AttributeValue>>> getMultipleInformation(String email,String name) {Map<String,KeysAndAttributes> requestItems = new HashMap<>();List<Map<String,AttributeValue>> userKeys = new ArrayList<>();Map<String,AttributeValue> userAttributes = new HashMap<>();userAttributes.put("email",new AttributeValue().withS(email));userKeys.add(userAttributes);requestItems.put(UserRepository.TABLE_NAME,new KeysAndAttributes().withKeys(userKeys));List<Map<String,AttributeValue>> supervisorKeys = new ArrayList<>();Map<String,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);Map<String,List<Map<String,AttributeValue>>> responses = batchGetItemResult.getResponses();return responses;}您可以在github上找到源代碼
翻譯自: https://www.javacodegeeks.com/2016/07/query-dynamodb-items-java-part-2.html
總結
以上是生活随笔為你收集整理的使用Java第2部分查询DynamoDB项的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jbehave_使用JBehave,Gr
- 下一篇: JavaFX实际应用程序:SkedPal