使用DynamoDBMapper扫描DynamoDB项目
之前,我們介紹了如何使用DynamoDBMapper或底層Java api查詢DynamoDB數(shù)據(jù)庫。
除了發(fā)出查詢之外,DynamoDB還提供掃描功能。
掃描的目的是獲取您在DynamoDB表上可能擁有的所有項目。
因此,掃描不需要任何基于我們的分區(qū)鍵或您的全局/本地二級索引的規(guī)則。 掃描提供的功能是基于已獲取的項目進行過濾,并從已獲取的項目中返回特定屬性。
下面的代碼段通過過濾具有較低日期的項目來對“登錄名”表進行掃描。
public List<Login> scanLogins(Long date) {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(date.toString()));DynamoDBScanExpression dynamoDBScanExpression = new DynamoDBScanExpression().withFilterExpression("#timestamp < :from").withExpressionAttributeNames(attributeNames).withExpressionAttributeValues(attributeValues);List<Login> logins = dynamoDBMapper.scan(Login.class, dynamoDBScanExpression);return logins;} DynamoDBMapper的另一個重要功能是并行掃描。 并行掃描將掃描任務(wù)劃分為多個工作程序,每個邏輯段一個。 工作人員并行處理數(shù)據(jù)并返回結(jié)果。
通常,掃描請求的性能在很大程度上取決于DynamoDB表中存儲的項目數(shù)。 因此,并行掃描可能會解除掃描請求的某些性能問題,因為您必須處理大量數(shù)據(jù)。
在對我們的應(yīng)用程序使用掃描之前,我們必須考慮到掃描會獲取所有表項。 因此,它在費用和性能上都有很高的成本。 此外,它可能會消耗您的配置容量。
通常,最好堅持查詢并避免掃描。
您可以在github上找到帶有單元測試的完整源代碼。
翻譯自: https://www.javacodegeeks.com/2016/10/scan-dynamodb-items-dynamodbmapper.html
總結(jié)
以上是生活随笔為你收集整理的使用DynamoDBMapper扫描DynamoDB项目的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 继承Javadoc方法注释
- 下一篇: 苏轼的父亲和兄弟叫什么名字 苏轼的父亲和