我有一个DynamoDB表" Music"。在它上面它有一个带分区键的GSI"类别"并排序键" UserRating"。
我可以轻松地查询作为"类别"中的歌曲的示例。 =" Rap"和" UserRating" = 1
我想做的是查询,然后回到所有"类别"。因为这是一个GSI和分区密钥,我听说你可以做到,但我不确定如何。
是否有可能或者我必须在"类别"上创建单独的GSI。没有排序键。
感谢您的帮助。
答案 0 :(得分:1)
如果您不想按键过滤。您可能需要扫描索引。以下解决方案是扫描索引以获取所有类别(并非所有不同类别)。
请在下面找到Java代码以获取GSI的所有类别。相应地替换下面代码中的二级索引名称。
List<String> categoryList = new ArrayList<>();
DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);
Table table = dynamoDB.getTable("Music");
Index index = table.getIndex("Secondary Index Name");
ItemCollection<ScanOutcome> items = null;
ScanSpec scanSpec = new ScanSpec().withSelect(Select.SPECIFIC_ATTRIBUTES).withAttributesToGet("Category");
items = index.scan(scanSpec);
Iterator<Item> pageIterator = items.iterator();
while (pageIterator.hasNext() ) {
categoryList.add(pageIterator.next().getString("Category"));
}