0
votes

I have a DynamoDB table that is defined like:

UUId,itemName,itemOwner
1,item1,owner1
2,item1,owner2

I have 2 global secondary indexes on this table name_index -> itemName(PartitionKey) owner_index -> itemOwner(PartitionKey), ItemName(SortKey)

I have annotate the dynamoDB Item class like this:

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


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

However when I attempt to query the table by the item owner it fails to return anything.

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

I then tried to change the query to set range key:

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

This also returns nothing. If I do a a query through the console however with owner_index with values owner and name it returns the correct record.

What am I doing wrong with the sort key for my query?

1

1 Answers

0
votes

The Issue:

The no data being returned was an issue of me running other test code before querying that was altering the data. After fixing the data and code, both options worked. The first query snippet however does not search by sort key, but only by the hash key column, itemOwner in this case and was returning all values.

Correct query code:

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

Correct Annotation:

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


@DynamoDBAttribute
@DynameDBIndexHashKey(globalSecondaryIndexName = "owner_index")
private String itemOwner