【问题标题】:Querying a local secondary index of dynamoDB using low level java API使用低级 java API 查询 dynamoDB 的本地二级索引
【发布时间】: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


    【解决方案1】:

    您误解了本地指数。

    Query 始终采用哈希键。所以你的QueryRequest 应该有一个setHashKeyValue 设置。

    您不能对查询进行交叉哈希处理,不能使用哈希+范围主键,也不能使用 LSI。您可以使用 GSI 做到这一点,但我不确定这对您有什么帮助。

    目前尚不清楚您到底想要达到什么目的。如果您想要 all 用户名,那么您需要 Scan 而不是 Query - 因为您希望所有哈希键都存在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多