【发布时间】:2016-06-16 06:37:56
【问题描述】:
我有一个 DynamoDB 表,其定义如下:
UUId,itemName,itemOwner
1,item1,owner1
2,item1,owner2
我在这张表上有 2 个全局二级索引 name_index -> itemName(PartitionKey) owner_index -> itemOwner(PartitionKey), ItemName(SortKey)
我已经像这样注释 dynamoDB Item 类:
@DynamoDBAttribute
@DynameDBIndexHashKey(globalSecondaryIndexName = "name_index")
@DynameDBIndexRangeKey(globalSecondaryIndexName = "owner_index")
private String itemName;
@DynamoDBAttribute
@DynameDBIndexHashKey(globalSecondaryIndexName = "owner_index")
private String itemOwner;
但是,当我尝试按项目所有者查询表时,它无法返回任何内容。
Item itemKey = new ItemName();
itemKey.setItemOwner("owner1");
itemKey.setItemName("item1");
DynamoDBQueryExpression<T> expr = DynamoDBQueryExpression<T>().withIndexName("owner_index").withHashKeyValues(itemKey).withConsistentRead(false);
return mapper.query(ItemKey.class, expr);
然后我尝试更改查询以设置范围键:
Item itemKey = new ItemName();
itemKey.setItemOwner("owner1");
List<AttributeValue> valueList = new LinkedList<>();
AttributeValue nameVal = new AttributeValue();
nameVal.setS("item1");
valueList.add(nameVal);
Condition con = new condition();
con.setComparisonOperator(ComparisonOperator.EQ);
con.setAttributeValueList(valueList);
DynamoDBQueryExpression<T> expr = DynamoDBQueryExpression<T>().withIndexName("owner_index").withHashKeyValues(itemKey).withRangeKeyCondtion("itemName", condition).withConsistentRead(false);
return mapper.query(ItemKey.class, expr);
这也不会返回任何内容。如果我通过控制台进行查询,但是使用 owner_index 的值 owner 和 name 它返回正确的记录。
我的查询的排序键有什么问题?
【问题讨论】:
标签: java amazon-dynamodb