1

I've seen this page about how to query with partition keys only. However, my case is using DynamoDBMapper class to make the query, what seemed to work there does not apply.

Here's a part of my code:

private final DynamoDBMapper mapper; 
List<QueryResult> queryResult = mapper.query(QueryResult.class, queryExpression);

The table I query has a primary partition key id and primary sort key timestamp.

I wanted to query all the rows with designatedid, eav looks like:

{:id={S: 0123456,}}

but if the id has duplicates (which makes sense cause it's partition key), it always gives me

"The provided key element does not match the schema"

Not sure how to resolve this. Due to sharing code with other tables, DynamoDBMapper class is a must.

Any help appreciated! Thanks.

Community
  • 1
  • 1
Kei
  • 611
  • 2
  • 11
  • 24
  • what's in `queryExpression` – Charles Nov 26 '19 at 18:22
  • @Charles Thanks for replying! I fixed the queryExpression issue but now it's still not letting me query with partition key only. I edited the question above. – Kei Nov 27 '19 at 18:45
  • @Kei Was this resolved? I am also having the same use case, but the documentation states that the querying is possible only using the hash and range keys together. Can you share the solution? – Aditya Jun 03 '21 at 05:25

2 Answers2

0

Does the below work?

final DynamoDBQueryExpression<QueryResult> queryExpression = new DynamoDBQueryExpression<>();
        expression.setKeyConditionExpression("id = :id");
        expression.withExpressionAttributeValues(ImmutableMap.of(":id", new AttributeValue("0123456")));
Aliaksandr Kazlou
  • 3,221
  • 6
  • 28
  • 34
0

Here is a working example:

final MyItem hashKeyValues = MyItem.builder()
                             .hashKeyField("abc")
                             .build();

final DynamoDBQueryExpression<MyItem> queryExpression = new DynamoDBQueryExpression<>();
queryExpression.withHashKeyValues(hashKeyValues);
queryExpression.setConsistentRead(false); //or true

final PaginatedQueryList<MyItem> response = dynamoDBMapper.query(MyItem.class, queryExpression);
AppleCiderGuy
  • 1,249
  • 1
  • 9
  • 16