2

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

Community
  • 1
  • 1

3 Answers3

2

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!

0

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()) {
   //.......
 }
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
  • Thank you. But if I use QuerySpec I have an error `Exception in thread "rresqTaskExecutor-1" java.lang.NoSuchMethodError: com.amazonaws.RequestClientOptions.appendUserAgent (Ljava/lang/String;)` maybe you know why? I change dependencies – Alexander Vysotski Aug 12 '16 at 06:34
  • @AlexanderVysotski I guess you may be missing AWSJavaSDK, http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/RequestClientOptions.html – kuhajeyan Aug 12 '16 at 09:58
0

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
Dasharath
  • 549
  • 4
  • 15
  • Can I use DynamoDBIndexHashKey + DynamoDBRangeKey на last_fetch? And can you write an example.. Because if I write DynamoDBRangeKey(attributeName="last_fetch) and DynamoDBIndexRangeKey(attributeName = "last_fetch", localSecondaryIndexName = "count-index") - I have an error the last_fetch not consist in "count-index" – Alexander Vysotski Aug 12 '16 at 06:19
  • You haven't specified the index name in DynamoDBRangeKey. Try this - DynamoDBRangeKey(attributeName="last_fetch", globalSecondaryIndexName="last_fetch_index"). Don't forget to create last_fetch_index GSI first – Dasharath Aug 12 '16 at 12:51
  • sorry `DynamoDBRangeKey` and `DynamoDBIndexRangeKey (localSecondaryIndexName = "count-index")` together on`last_fetch` . - I have an error the last_fetch not consist in "count-index" – Alexander Vysotski Aug 15 '16 at 06:20
  • Could you post the code changes you have made? What is the complete error? When do you get the error? – Dasharath Aug 15 '16 at 10:33
  • It is not actual any more, I have found the solution of this problem. The problem was in dependencies which I used with S3 earlier. and also http.client . (It is my mistake) Now I use DynamoDB in full. Thank you. – Alexander Vysotski Aug 19 '16 at 13:26