【发布时间】:2015-11-06 00:18:36
【问题描述】:
我正在尝试使用哈希键查询本地二级索引。
我的桌子:Table1 用户名(hash_key)、Id(range_key)、时间戳……
我的本地二级索引(因为我想查询所有用户名并按时间排序) 我的 LSI:Table1_TimestampLSI 用户名(hash_key),时间戳(range_key)
例如 用户1 | 123 | 2015 年 12 月 12 日 用户1 |第456章2015 年 11 月 1 日 用户2 |第789章2015 年 12 月 1 日
注意:我不能只将时间戳设为表本身的范围键,因为 (Username+Timestamp) 不是唯一的。所以我必须创建一个 ID 字段来确保唯一性。
因为我想要异步客户端,所以我使用的是低级 API(模型而非文档)Java API。
查询索引功能
HashMap<String, Condition> queryFilter = new HashMap<String, Condition>();
Condition condition = new Condition()
.withComparisonOperator(ComparisonOperator.EQ.toString())
.withAttributeValueList(new AttributeValue().withS(username));
queryFilter.put("Username", condition);
QueryRequest queryRequest = new QueryRequest(tableName + "_TimestampLSI").withQueryFilter(queryFilter);
queryRequest.setScanIndexForward(false);
Future<QueryResult> fQueryResult = dynamoDB.queryAsync(queryRequest,
new AsyncHandler<QueryRequest,QueryResult>() {
public void onSuccess(QueryRequest request, QueryResult result) {
System.out.println("Table: " + result);
}
public void onError(Exception exception) {
System.out.println("Error describing table: " + exception.getMessage());
// Callers can also test if exception is an instance of
// AmazonServiceException or AmazonClientException and cast
// it to get additional information
}
});
System.out.println("Result: " + fQueryResult);
我收到以下错误 错误描述表:必须在请求中指定 KeyConditions 或 KeyConditionExpression 参数。 (服务:AmazonDynamoDBv2;状态代码:400;错误代码:ValidationException;
我错过了什么吗?以为我应该能够仅根据哈希值查询索引。
【问题讨论】:
标签: java amazon-dynamodb aws-sdk