非法查询表达式:在AWS Query中的查询中未找到任何哈希键条件

时间:2017-09-06 06:46:46

标签: amazon-web-services amazon-dynamodb

我在AWS移动集线器中有表,我正在使用以下模型

public class UserstopcoreDO {
    private String _userId;
    private String _usertoplevel;
    private String _usertopscore;
    private String _username;

    @DynamoDBHashKey(attributeName = "userId")
    @DynamoDBAttribute(attributeName = "userId")
    public String getUserId() {
        return _userId;
    }

    public void setUserId(final String _userId) {
        this._userId = _userId;
    }

    @DynamoDBAttribute(attributeName = "usertoplevel")
    public String getUsertoplevel() {
        return _usertoplevel;
    }

    @DynamoDBAttribute(attributeName = "username")
    public String getUsername() {
        return _username;
    }

    public void setUsername(final String _username) {
        this._username = _username;
    }

    public void setUsertoplevel(final String _usertoplevel) {
        this._usertoplevel = _usertoplevel;
    }

    @DynamoDBIndexHashKey(attributeName = "usertopscore", globalSecondaryIndexName = "usertopscore")
    public String getUsertopscore() {
        return _usertopscore;
    }

    public void setUsertopscore(final String _usertopscore) {
        this._usertopscore = _usertopscore;
    }

}

在表格中,我有1500多条记录,现在我想从中获取前10条记录,所以我写下面的查询

final DynamoDBQueryExpression<UserstopcoreDO> queryExpression = new DynamoDBQueryExpression<>();
            queryExpression.withLimit(10);
            queryExpression.setScanIndexForward(false);
            final PaginatedQueryList<UserstopcoreDO> results = mapper.query(UserstopcoreDO.class, queryExpression);
            Iterator<UserstopcoreDO> resultsIterator = results.iterator();
            if (resultsIterator.hasNext()) {
                final UserstopcoreDO item = resultsIterator.next();
                try {
                    Log.d("Item :",item.getUsertopscore());
                } catch (final AmazonClientException ex) {
                    Log.e(LOG_TAG, "Failed deleting item : " + ex.getMessage(), ex);
                }
            }

但是当我运行代码时,它会给我一个错误

Caused by: java.lang.IllegalArgumentException: Illegal query expression: No hash key condition is found in the query

但在我的情况下,我不需要任何条件,因为我想获取前10个记录而不是一个特定记录。那么如何处理这种情况呢?

2 个答案:

答案 0 :(得分:4)

请在查询表达式中设置哈希键。下面是主表和GSI的查询表达式示例(需要设置索引名称)。

查询主表: -

设置表的哈希键值。

UserstopcoreDO hashKeyObject = new UserstopcoreDO();
hashKeyObject.setUserId("1");

DynamoDBQueryExpression<UserstopcoreDO> queryExpressionForMainTable = new DynamoDBQueryExpression<UserstopcoreDO>()
    .withHashKeyValues(hashKeyObject);

查询索引: -

设置索引的索引名称和哈希键值。

UserstopcoreDO hashIndexKeyObject = new UserstopcoreDO();
    hashIndexKeyObject.setUsertoplevel("100");

DynamoDBQueryExpression<UserstopcoreDO> queryExpressionForGsi = new DynamoDBQueryExpression<UserstopcoreDO>()
            .withHashKeyValues(hashIndexKeyObject).withIndexName("usertopscore");

映射器中的GSI属性: -

@DynamoDBIndexHashKey(attributeName = "usertoplevel",  globalSecondaryIndexName = "usertopscore")
public String getUsertoplevel() {
    return _usertoplevel;
}

@DynamoDBIndexRangeKey(attributeName = "usertopscore", globalSecondaryIndexName = "usertopscore")
public String getUsertopscore() {
    return _usertopscore;
}

答案 1 :(得分:1)

如果要在不指定 all HashKeys的情况下“查询” DynamoDB,请改用Scan,即DynamoDBScanExpression。您可能还想将“ usertopscore”更改为RangeKey而不是HashKey。

从每个https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/datamodeling/DynamoDBQueryExpression.html的DynamoDBQueryExpression 需要 所有设置哈希键。

另请参见boto dynamodb2: Can I query a table using range key only?