gpt4 book ai didi

Java:使用 Mapper 查询 DynamoDB 表中 Range 键的所有值

转载 作者:行者123 更新时间:2023-12-01 18:02:24 27 4
gpt4 key购买 nike

我有一个 DynamoDb 表。它的哈希键为“ID”,范围键为“Category”。

我正在尝试查找“类别”的所有条目,例如喜剧。

这是我的映射类:

@DynamoDBTable(tableName = "Videos")
public class VideoDynamoMappingAdapter {

private String videoID;
private String videoCategory;
private String videoArtist;
private String videoTitle;

@DynamoDBHashKey(attributeName = "ID")
public String getVideoID() {
return videoID;
}

public void setVideoID(String videoID) {
this.videoID = videoID;
}
@DynamoDBRangeKey(attributeName = "Category")
public String getVideoCategory() {
return videoCategory;
}

public void setVideoCategory(String videoCategory) {
this.videoCategory = videoCategory;
}

@DynamoDBAttribute(attributeName = "ArtistName")
public String getVideoArtist() {
return videoArtist;
}

public void setVideoArtist(String videoArtist) {
this.videoArtist = videoArtist;
}

@DynamoDBAttribute(attributeName = "VideoTitle")
public String getVideoTitle() {
return videoTitle;
}

public void setVideoTitle(String videoTitle) {
this.videoTitle = videoTitle;
}

}

我一直在尝试查询这样的内容:

    DynamoDBQueryExpression<VideoDynamoMappingAdapter> queryExpression = new DynamoDBQueryExpression<VideoDynamoMappingAdapter>()
.withRangeKeyCondition()


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

我一直在查看指南,但我无法理解它,我本以为这将是一个一直在执行的简单查询,所以也许我遗漏了一些东西:

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryingJavaDocumentAPI.html#DocumentAPIJavaQueryExample

预先感谢您的帮助

最佳答案

Notionquest 的回答是正确的,我无法在给定范围条件下查询哈希键的所有值。我通过在类别属性上创建全局二级索引解决了这个问题。

这是我的映射器类:

@ DynamoDBTable(tableName = "AlarmTable")
public class VideoDynamoMappingAdapter {

private String videoID;
private String videoCategory;
private String videoArtist;
private String videoTitle;

@DynamoDBHashKey(attributeName = "ID")
public String getVideoID() {
return videoID;
}

public void setVideoID(String videoID) {
this.videoID = videoID;
}

@DynamoDBIndexHashKey( globalSecondaryIndexName = "CategoryIndex", attributeName = "Category")
public String getVideoCategory() {
return videoCategory;
}

public void setVideoCategory(String videoCategory) {
this.videoCategory = videoCategory;
}

@DynamoDBAttribute(attributeName = "VideoArtist")
public String getVideoArtist() {
return videoArtist;
}

public void setVideoArtist(String videoArtist) {
this.videoArtist = videoArtist;
}

@DynamoDBAttribute(attributeName = "VideoTitle")
public String getVideoTitle() {
return videoTitle;
}

public void setVideoTitle(String videoTitle) {
this.videoTitle = videoTitle;
}

}

请注意类别上的注释。

然后我的查询是这样的:

VideoDynamoMappingAdapter videosToFind = new VideoDynamoMappingAdapter();
videosToFind.setVideoCategory("Other");

DynamoDBQueryExpression<VideoDynamoMappingAdapter> queryExpression = new DynamoDBQueryExpression<VideoDynamoMappingAdapter>()
.withIndexName("CategoryIndex")
.withHashKeyValues(videosToFind)
.withConsistentRead(false);

List<VideoDynamoMappingAdapter> itemList = mapper.query(VideoDynamoMappingAdapter.class, queryExpression);
System.out.println("test" + itemList.size());

我希望它对某人有所帮助。

关于Java:使用 Mapper 查询 DynamoDB 表中 Range 键的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39875972/

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