dynamo DB:QuerySpec与QueryRequest

时间:2016-04-02 18:22:10

标签: amazon-web-services amazon amazon-dynamodb

QueryRequest和QuerySpec之间存在什么差异?

QuerySpec spec = new QuerySpec()
        .withKeyConditionExpression("#n_channel = :v_channel")
        .withFilterExpression("#n_type = :v_type")
        .withNameMap( new NameMap()
            .with( "#n_type", DATABASE_CONTENT_TYPE_NAME )
            .with( "#n_channel", PRIMARY_KEY_NAME ))
        .withValueMap(new ValueMap()
            .withString(":v_type", type)
            .withString(":v_channel",channelId))
        .withConsistentRead(true);

使用QuerySpec - 工作

keyConditions.put( PRIMARY_KEY_NAME, new Condition().withComparisonOperator( ComparisonOperator.EQ ).withAttributeValueList( new AttributeValue().withS( channelId ) ) );
keyConditions.put( RANGE_KEY_NAME, new Condition().withComparisonOperator( ComparisonOperator.NOT_NULL ) );//default value
typeFilterExpression = "#n_type = :v_type";
nameMap.with( "#n_type", DATABASE_CONTENT_TYPE_NAME );
values.put( ":v_type", new AttributeValue().withS( type ) );

//
    QueryRequest request = new QueryRequest().withTableName( tableName )
        .withKeyConditions( keyConditions ).withLimit( QUERY_LIMIT )
        .withReturnConsumedCapacity( ReturnConsumedCapacity.TOTAL ).withConsistentRead( false );
    if( StringUtils.isNotBlank( typeFilterExpression ) ) {
      request.withFilterExpression( typeFilterExpression );
    }
    if( !MapUtils.isEmpty( nameMap ) ) {
      request.withExpressionAttributeNames( nameMap );
    }
    if( !MapUtils.isEmpty( values ) ) {
      request.withExpressionAttributeValues( values );
    }

使用相同的QueryRequest - 不起作用。

com.amazonaws.AmazonServiceException: Attempted conditional constraint is not an indexable operation

亚马逊版: 编译' com.amazonaws:aws-java-sdk:1.10.65'

谢谢!

1 个答案:

答案 0 :(得分:0)

对于QueryRequest,需要使用过滤器map:

filters.put( TYPE, new Condition() //
        .withComparisonOperator( ComparisonOperator.EQ ) //
        .withAttributeValueList( //
            new AttributeValue() //
                .withS( type.name() ) //
        ) //
    );
request.withQueryFilter( filters );

一切正常!