7

EDIT: I was actually incorrect. I was querying the table when I meant to query an index which explains my error. Vikdor's solution is a valid one though.

ORIGINAL: I have a table with a Hash-Range key schema in DynamoDB. I need to be able to get all items associated with a specific hash key but it seems to require a range key condition. My issue is I want EVERY range key but there is no wildcard option. As of right now my range key is a string and the only way I could think to do this is by querying all range keys greater or equal to the smallest ascii characters I can use since the documentation says it sorts based on ascii character values.

I looked into scanning but it appears that simply will read the entire table which is NOT an option.

Is there any better way to query for all values of a hash key or can anyone confirm that using the method with the ascii character will work?

mnewton
  • 444
  • 1
  • 5
  • 12

2 Answers2

8

but it seems to require a range key condition.

This doesn't sound to be true.

I use DynamoDBMapper and use DynamoDBQueryExpression to query all the records with a given HashKey as follows:

DynamoDBQueryExpression<DomainObject> query = 
    new DynamoDBQueryExpression<DomainObject>();
DomainObject hashKeyValues = new DomainObject();
hashKeyValues.setHashKey(hashKeyValue);
query.setHashKeyValues(hashKeyValues);
// getMapper() returns a DynamoDBMapper object with the appropriate 
// AmazonDynamoDBClient object.
List<DomainObject> results = getMapper().query(query);

HTH.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
  • I was trying to use your approach for querying the Table on the basis of HashKey only but it is not working for me. Let me know if you can help. Thanks – Keshav Sharma Aug 18 '16 at 07:08
  • Could you please elaborate on "not working"? Is it not returning any rows with that HashKey? or is it throwing some exception? Are you sure there are records with the HashKey you were trying to search for? @KeshavSharma – Vikdor Aug 18 '16 at 14:14
  • [my question](http://stackoverflow.com/questions/39013566/query-dynamodb-using-only-hashkey) In this question, nothing happens after mapper.query() is called. I have created records in the previous steps using mapper.save(). Thanks for the help. – Keshav Sharma Aug 18 '16 at 16:25
3

You can use DynamoDB's query API, which allows you to query the database based conditional expressions using the hash/range keys. You can see examples of the API here. Here is a relevant example:

ItemCollection<QueryOutcome> items = table.query("theHashFieldName", "theHashFieldToQuery");

You can also query using more complex expressions. E.g.:

DynamoDB dynamoDB = new DynamoDB(
    new AmazonDynamoDBClient(new ProfileCredentialsProvider()));

Table table = dynamoDB.getTable("TableName");

QuerySpec spec = new QuerySpec()
    .withKeyConditionExpression("Id = :v_id")
    .withValueMap(new ValueMap()
        .withString(":v_id", "TheId"));

ItemCollection<QueryOutcome> items = table.query(spec);

Iterator<Item> iterator = items.iterator();
Item item = null;
while (iterator.hasNext()) {
    item = iterator.next();
    System.out.println(item.toJSONPretty());
}
Max
  • 15,157
  • 17
  • 82
  • 127
  • Does it require to create an Index? – Emil Dec 12 '16 at 23:35
  • If you are querying the table's hash/range key this will work out of the box. If you are querying an GSI, then you will need an index. – Max Dec 12 '16 at 23:46