欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > java >内容正文

java

dynamodb java_使用Java扫描DynamoDB项目

发布时间:2023/12/3 java 56 豆豆
生活随笔 收集整理的这篇文章主要介绍了 dynamodb java_使用Java扫描DynamoDB项目 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

dynamodb java

在之前的文章中,我们介绍了如何查询DynamoDB数据库
查询DynamoDB第1部分
查询DynamoDB第2部分 。

除了发出查询之外,DynamoDB还提供扫描功能。 扫描所做的是获取您在DynamoDB表上可能拥有的所有项目。 因此,扫描不需要任何基于我们的分区键或您的全局/本地二级索引的规则。 扫描提供的功能是基于已获取的项目进行过滤,并从已获取的项目中返回特定属性。

下面的代码段通过添加过滤并仅选择电子邮件字段来对“登录名”表进行扫描。

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;}

在对应用程序使用扫描之前,我们必须考虑到扫描会获取所有表项。 因此,它在费用和性能上都有很高的成本。 此外,它可能会消耗您的配置容量。
最好坚持查询并避免扫描。

您可以在github上找到源代码。

翻译自: https://www.javacodegeeks.com/2016/08/scan-dynamodb-items-java.html

dynamodb java

总结

以上是生活随笔为你收集整理的dynamodb java_使用Java扫描DynamoDB项目的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。