使用Java扫描DynamoDB项目
生活随笔
收集整理的這篇文章主要介紹了
使用Java扫描DynamoDB项目
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在之前的文章中,我們介紹了如何查詢DynamoDB數(shù)據(jù)庫
查詢DynamoDB第1部分
查詢DynamoDB第2部分 。
除了發(fā)出查詢之外,DynamoDB還提供掃描功能。 掃描所做的是獲取您在DynamoDB表上可能擁有的所有項(xiàng)目。 因此,掃描不需要任何基于我們的分區(qū)鍵或您的全局/本地二級(jí)索引的規(guī)則。 掃描提供的功能是基于已獲取的項(xiàng)目進(jìn)行過濾,并從已獲取的項(xiàng)目中返回特定屬性。
下面的代碼段通過添加過濾并僅選擇電子郵件字段來對(duì)“登錄名”表進(jìn)行掃描。
public List<String> scanLogins(Date date) {List<String> emails = 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("email");Map<String,AttributeValue> lastKey = null;do {ScanResult scanResult = amazonDynamoDB.scan(scanRequest);List<Map<String,AttributeValue>> results = scanResult.getItems();results.forEach(r->emails.add(r.get("email").getS()));lastKey = scanResult.getLastEvaluatedKey();scanRequest.setExclusiveStartKey(lastKey);} while (lastKey!=null);return emails;} 在對(duì)應(yīng)用程序使用掃描之前,我們必須考慮到掃描會(huì)獲取所有表項(xiàng)。 因此,它在費(fèi)用和性能上都有很高的成本。 此外,它可能會(huì)消耗您的配置容量。
最好堅(jiān)持查詢并避免掃描。
您可以在github上找到源代碼。
翻譯自: https://www.javacodegeeks.com/2016/08/scan-dynamodb-items-java.html
總結(jié)
以上是生活随笔為你收集整理的使用Java扫描DynamoDB项目的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓悬浮窗口怎么打开(安卓悬浮窗口)
- 下一篇: Java数组排序解码