2017-09-06 20 views
-1

我有表AWS移动枢纽和我使用它非法查询表达式:没有哈希关键条件在查询中发现,在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+记录,现在我想所以我写了下面的查询

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

从中取前10名的记录但是当我运行的代码它给我一个错误

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

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

+0

这听起来像你想做的事,而不是'query'一个'scan'。 –

+0

所以我应该使用扫描的目的,或者我可以通过使用查询解决它? –

回答

1

请在查询表达式中设置散列键。以下是主表和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; 
} 
+0

我设置了hast键,它没有返回结果,因为我没有记录id 1,所以我想获得前10个结果 –

+0

如果你有10个或更多记录的散列键,你会得到10条记录。其实,我看到你的设计有问题。在你昨天的SO问题中,你提到GSI有usertoplevel是分区键和usertopscore作为Sort键。但是,usertoplevel在映射器中没有定义为散列键。不知道你现在拥有什么。 – notionquest

+0

我刚刚在AWS的移动集线器中创建表并下载源代码现在我应该更改哪些内容以获得期望的结果意味着前10个记录 –