DynamoDB使用全局辅助索引进行查询



我正在尝试查询最近一周创建的所有行。我已经为AWS console中的created键创建了一个索引。在我的查询中,我为created密钥添加了带有ComparisonOperator.GT的密钥条件。但当我运行查询时,它会抛出一个类似Query key condition not supported的错误。如果我将条件给定为ComparisonOperator.EQ,它将返回一行。但不适用于ComparisonOperator.GT

代码:

Condition rangeKeyCondition = new Condition();
    rangeKeyCondition.withComparisonOperator(ComparisonOperator.GT).withAttributeValueList(new AttributeValue().withS("11:26 23/10/2018 "));
    Map<String, Condition> keyConditions = new HashMap<String, Condition>();
    keyConditions.put("created", rangeKeyCondition);

    QueryRequest queryRequest = new QueryRequest();
    queryRequest.withTableName(getTableName(TABLE_NAME));
    queryRequest.withIndexName("created-index");
    queryRequest.withKeyConditions(keyConditions);
    QueryResult result = EventStoreInitializer.getAmazonDynamoDBClient().query(queryRequest);

我已经阅读了您的案例,因此对于您想要的输出,您不应该将"created"创建为索引bz,它将成为表的键,在keyCondition中,您不能使用GT和LT运算符。

您需要使用FilterExpression,在那里您可以使用GT和LT作为"已创建"字段。

最新更新