日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

使用Java第2部分查询DynamoDB项

發布時間:2023/12/3 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Java第2部分查询DynamoDB项 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在上一篇文章中,我們有機會發布了一些基本的DynamoDB查詢操作。

但是,除了基本操作之外,DynamoDB api還為我們提供了一些額外的功能。

投影是具有類似選擇功能的功能。
您選擇應從DynamoDB項中提取哪些屬性。 請記住,使用投影不會對您的查詢帳單產生任何影響。

public Map<String,AttributeValue> getRegisterDate(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).withProjectionExpression("registerDate");QueryResult queryResult = amazonDynamoDB.query(queryRequest);List<Map<String,AttributeValue>> attributeValues = queryResult.getItems();if(attributeValues.size()>0) {return attributeValues.get(0);} else {return null;}}

除了選擇屬性,我們還可以根據我們的范圍鍵指定順序。 我們將使用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项的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。