gpt4 book ai didi

java - 如何使用Java从dynamodb读取表?

转载 作者:行者123 更新时间:2023-11-30 02:35:03 25 4
gpt4 key购买 nike

我在 Amazon dynamodb 中创建了一个主键 Issue(String) 的表,其中存储了数据。我想从表中读取值。我正在使用以下代码..

@DynamoDBTable(tableName="Incident")
AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient();
String tableName = "Incident";
Table table = dynamoDBClient.getTable("Incident");
Item getItem=dynamoDBClient.getItem();

我在调用 getTable 方法时遇到错误......它是像 createTable() 一样的预定义方法还是我们需要编写自己的方法..如果是这样怎么办?还有应该使用什么方法来读取表中的所有项目..?我使用此链接编写了一些代码... http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaDocumentAPIItemCRUD.html#JavaDocumentAPIGetItem

我是 Java 新手,请帮忙..

最佳答案

扫描API可用于获取表中的所有项目。

应该进行扫描,直到 LastEvaluatedKey 不为 null,这对于获取所有项目非常重要。否则,如果表中有很多项目,您将无法获取所有项目,即 API 每次扫描将返回 1 MB 的数据。

A Scan operation performs eventually consistent reads by default, and it can return up to 1 MB (one page) of data.

Scan API

Map<String, AttributeValue> lastKeyEvaluated = null;
do {
ScanRequest scanRequest = new ScanRequest()
.withTableName("ProductCatalog")
.withLimit(10)
.withExclusiveStartKey(lastKeyEvaluated);

ScanResult result = client.scan(scanRequest);
for (Map<String, AttributeValue> item : result.getItems()){
printItem(item);
}
lastKeyEvaluated = result.getLastEvaluatedKey();
} while (lastKeyEvaluated != null);

关于java - 如何使用Java从dynamodb读取表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43318511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com