我如何从DynamoDBIndexHashKey获取项目?

时间:2016-08-11 06:44:35

标签: java amazon-dynamodb

我想说明一下。我是否只能从DynamoDBIndexHashKey收到元素,而不是DynamoDBHashKey

我有一个包含字段的表

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

我没有@DynamoDBIndexRangeKey

这是代码:

 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);

错误:

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

如何从DynamoDBIndexHashKey获取商品?

P.S。扫描 - 工作,但对我来说并不感兴趣,因为我想要进一步排序 使用DynamoDBHashKey查询工作。我有DynamoDBIndexHashKey

的问题

same example

3 个答案:

答案 0 :(得分:1)

这是我的问题的答案

实体:

 @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;
}

代码:

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);

谢谢!

答案 1 :(得分:0)

解释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()) {
   //.......
 }

答案 2 :(得分:0)

您不能使用Query仅根据排序/范围键查找项目。

您可以阅读更多here

  

在Query操作中,使用KeyConditionExpression参数确定要从表或索引中读取的项。 您必须将分区键名称和值指定为相等条件。您可以选择为排序键(如果存在)提供第二个条件。

在这种情况下,您的选项是:

  1. 使用last_fetch作为过滤器扫描操作。
  2. 重新设计数据库,使其具有带有last_fetch作为分区键的GSI
相关问题