Alexander Vysotski
Alexander Vysotski

Reputation: 51

How I can get items from DynamoDBIndexHashKey?

I would like to specify. May I receive elements only from DynamoDBIndexHashKey, not use DynamoDBHashKey?

I have a table with fields

@DynamoDBIndexHashKey (attributeName = "count", globalSecondaryIndexName = "count-index")
@DynamoDBHashKey(attributeName="cluster_output_Id)"
@DynamoDBRangeKey(attributeName="last_fetch)"  

I have no @DynamoDBIndexRangeKey

It's code:

 MyEntity myEntity = new MyEntity();
    myEntity.setCount(1);    // Integer
    DynamoDBQueryExpression<NewsDynamoDb> queryExpression = new DynamoDBQueryExpression<NewsDynamoDb>()
            .withHashKeyValues(myEntity)
            .withIndexName("count-index");
    queryExpression.setConsistentRead(false);
    List<MyEntity> myCollection = mapper.query(MyEntity.class, queryExpression);

Error:

AmazonServiceException: Status Code: 400, AWS Service: AmazonDynamoDBv2, AWS Request ID: I97S04LDGO6FSF56OCJ8S3K167VV4KQNSO5AEMVJF66Q9ASUAAJG, AWS Error Code: ValidationException, AWS Error Message: One or more parameter values were invalid: Invalid number of argument(s) for the EQ ComparisonOperator

How I can get items from DynamoDBIndexHashKey?

P.s. Scan - work but not interesting to me, because in a further I want a sorting Query with DynamoDBHashKey work. I have problems with DynamoDBIndexHashKey

same example

Upvotes: 2

Views: 5640

Answers (3)

Alexander Vysotski
Alexander Vysotski

Reputation: 51

It is the answer to my question

entity:

 @DynamoDBHashKey(attributeName="cluster_output_Id")
public Integer getCluster_output_Id() {
    return cluster_output_Id;
}

@DynamoDBIndexHashKey(attributeName = "count", globalSecondaryIndexName = "count-index")
public Integer getCount() {
    return count;
}

@DynamoDBRangeKey(attributeName="last_fetch")
@DynamoDBIndexRangeKey(attributeName = "last_fetch", globalSecondaryIndexName = "count-index")
public Date getLast_fetch() {
    return last_fetch;
}

code:

dynamoDBMapper = new DynamoDBMapper(amazonDynamoDBClient);
MyClass myClass= new MyClass();
DynamoDBQueryExpression<MyClass > queryExpression = new DynamoDBQueryExpression<MyClass >();
        myClass.setCount(1);
queryExpression.setHashKeyValues(myClass);
queryExpression.withIndexName("count-index");  // it's not necessarily
Condition rangeKeyCondition = new Condition();
rangeKeyCondition.withComparisonOperator(ComparisonOperator.NE)
        .withAttributeValueList(new AttributeValue().withS(""));
queryExpression.setConsistentRead(false);  
List entities = dynamoDBMapper.query(MyClass.class, queryExpression);

Thank you!

Upvotes: 2

Dasharath
Dasharath

Reputation: 579

You cannot use Query to find items based on sort/range key only.

You can read more here.

In a Query operation, you use the KeyConditionExpression parameter to determine the items to be read from the table or index. You must specify the partition key name and value as an equality condition. You can optionally provide a second condition for the sort key (if present).

In this case your options are:

  1. Scan operation with last_fetch as filter.
  2. Redesign your database to have a GSI with last_fetch as partition key

Upvotes: 0

kuhajeyan
kuhajeyan

Reputation: 11017

like explained here

 Table table = dynamoDB.getTable("tableName");
 Index index = table.getIndex("count-index");
 ItemCollection<QueryOutcome> items = null;
 QuerySpec querySpec = new QuerySpec();

querySpec.withKeyConditionExpression("count= :v_count > 0 ")
                .withValueMap(new ValueMap()                  .withString(":v_count","1");
 items = index.query(querySpec);

 while (iterator.hasNext()) {
   //.......
 }

Upvotes: 0

Related Questions