dhusar200
dhusar200

Reputation: 21

Android Studio, DynamoDB Query using also sort key

I am having an issue where I am trying to get the data from the DynamoDB directly to my android studio. It works totally fine if I want to query with PK and filter with random column, however, no matter what I do I am unable to use also the sort key. Expression cannot be used as its part of partion key, but it is also not possible to put two primitive types into the query.

I also tried using QueryFilter, however, I am getting has protected access on the code bellow, even if I extend the class.

QueryFilter queryFilter = new QueryFilter();

This is the code which works for query with only Partion Key without Sort Key.

final Expression expression = new Expression();
expression.setExpressionStatement("#t < :time");
//expression.setExpressionStatement("begins_with (#t, :time)");
expression.withExpressionAttibuteValues(":time", new Primitive(timeNow.format(format)));
expression.addExpressionAttributeNames("#t", "time");

Search searchResult = dbTable.query(new Primitive("Location1"), expression);

Upvotes: 1

Views: 78

Answers (1)

dhusar200
dhusar200

Reputation: 21

After trying for a bit longer I have found a solution with QueryOperationConfig. As someone may find this usefull, I will be posting the solution which worked for me.

    final Expression expression2 = new Expression();
    expression2.setExpressionStatement("#p = :PK AND begins_with(#s, :SK)");
    expression2.withExpressionAttibuteValues(":PK", new Primitive("Location1"));
    expression2.withExpressionAttibuteValues(":SK", new Primitive("2021-04-11 22:08"));
    expression2.addExpressionAttributeNames("#p", "location");
    expression2.addExpressionAttributeNames("#s", "device # MAC # UUID");

    QueryOperationConfig queryOperationConfig = new QueryOperationConfig();
    queryOperationConfig.withKeyExpression(expression2);
    Search searchResult = dbTable.query(queryOperationConfig);

Upvotes: 1

Related Questions