【问题标题】:Search a dynamoDB table attribute as a sort key搜索 dynamoDB 表属性作为排序键
【发布时间】: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 的值 ownername 它返回正确的记录。

我的查询的排序键有什么问题?

【问题讨论】:

    标签: java amazon-dynamodb


    【解决方案1】:

    问题:

    没有返回数据是我在查询更改数据之前运行其他测试代码的问题。修复数据和代码后,两个选项都有效。然而,第一个查询 sn-p 不按排序键搜索,而仅按哈希键列搜索,在这种情况下 itemOwner 并返回所有值。

    正确查询代码:

    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);
    

    正确的注释:

    @DynamoDBAttribute
    @DynameDBIndexHashKey(globalSecondaryIndexName = "name_index")
    @DynameDBIndexRangeKey(globalSecondaryIndexName = "owner_index")
    private String itemName;
    
    
    @DynamoDBAttribute
    @DynameDBIndexHashKey(globalSecondaryIndexName = "owner_index")
    private String itemOwner
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 2018-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多