Yu Zou
Yu Zou

Reputation: 51

No hash key condition is found in the query

I am trying to query my DynamoDB table for an individual record using a unique partition key:

DynamoDBQueryExpression queryExpression=new DynamoDBQueryExpression()
            .withHashKeyValues("parkurself")
            .withFilterExpression("Latitude>25")
            .withFilterExpression("Latitude<40")
            .withFilterExpression("Longitude>25")
            .withFilterExpression("Longitude<40")
            .withConsistentRead(false);
PaginatedQueryList<ParkingInfo> result=mapper.query(ParkingInfo.class, queryExpression);

However, I have received an error "No hash key condition is found in the query" The Logcat is being attached.

Caused by: java.lang.IllegalArgumentException: Illegal query expression: No hash key condition is found in the query
        at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.processKeyConditions(DynamoDBMapper.java:2420)
        at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.createQueryRequestFromExpression(DynamoDBMapper.java:2382)
        at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.query(DynamoDBMapper.java:2166)
        at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.query(DynamoDBMapper.java:2127)
        at monster.com.parkurself.QueryParkingInfo.doInBackground(QueryParkingInfo.java:63)
        at monster.com.parkurself.QueryParkingInfo.doInBackground(QueryParkingInfo.java:18)
        at android.os.AsyncTask$2.call(AsyncTask.java:295)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)

         

I don't know how to fix it. Can anyone give me some help? Thank you in advance.

Upvotes: 0

Views: 6524

Answers (2)

JellyJay
JellyJay

Reputation: 401

As Mircea said, you need to create a class that encapsulates the entry first which it looks like you have done. The issue here is you have passed in the String class which has no mapping attributes.

Create an instance of the mapper class and set the hash key to the one you want to query. Pass that class into the query and it should work.

// Create object which maps to the query object.
ParkingInfo parkingInfo = new ParkingInfo();
parkingInfo.setID("parkurself");

DynamoDBQueryExpression<ParkingInfo> queryExpression = new DynamoDBQueryExpression<ParkingInfo>()
        .withHashKeyValues(parkingInfo)
        .withFilterExpression("Latitude>25")
        .withFilterExpression("Latitude<40")
        .withFilterExpression("Longitude>25")
        .withFilterExpression("Longitude<40")
        .withConsistentRead(false);

PaginatedQueryList<ParkingInfo> result = mapper.query(ParkingInfo.class, queryExpression);

Upvotes: 2

Mircea
Mircea

Reputation: 10566

you are not going through the dynamo db mapper (http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/datamodeling/DynamoDBMapper.html and http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaSDKHighLevel.html) in order to use the DynamoDBQueryExpression you have to go through the mapper. what you pass into withHashKeyValues need to be an object instantiated from a class that has the right annotations on it. See example here:

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/datamodeling/DynamoDBMapper.html

you will need to create a class that has Parkurself, Longitude, Latitude as members and correctly tag them via attributes.

More about how to access DDB here: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/AboutJava.html

If you don't want/need to use the mapper (i.e. high level api) you could go through the low level api.

Upvotes: 1

Related Questions