gpt4 book ai didi

java - 如何基于分区键和排序键查询DynamoDB [Java]?

转载 作者:行者123 更新时间:2023-12-04 17:17:57 25 4
gpt4 key购买 nike

我是 DynamoDB 的新手,想知道如何使用 hashKey 和 sortKey 查询 DynamoDB 中的表。

我有一个名为 Items 的表。它的架构是

1. Product (Partition Key of type String)
2. ID (Sort Key of type int)
3. Date ( attribute of type String)

我获取所有具有 product = 10 的项目的查询是
Items it = new Items();
it.setProduct("apple");

DynamoDBQueryExpression<Items> queryExpression = new DynamoDBQueryExpression<Items>()
.withHashKeyValues(it);


List<Items> itemList = mapper.query(Items.class, queryExpression);

但是,现在我想获取所有具有 Product = "apple"ID = 100 的项目。

我可以在 Java 中为 DynamoDB 编写一个查询。

最佳答案

为了使用分区键和排序键从 DynamoDB 获取数据。您可以使用 load 类中的 DynamoDBMapper 方法。

DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient);
String product = "ball";
Integer id = 1;
Item itemObj = mapper.load(Items.class, product, id);

模型类,即在您的情况下项目类:-

您应该使用 Hash 和 Range 键的正确注释定义 Item 类。
@DynamoDBTable(tableName = "Items")
public class Item {

private String product;
private Integer id;

@DynamoDBHashKey(attributeName = "Product")
public String getProduct() {
return autoID;
}
@DynamoDBRangeKey(attributeName = "ID")
public String getId() {
return id;
}
}

关于java - 如何基于分区键和排序键查询DynamoDB [Java]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43257068/

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