【问题标题】:Dynamodb query error - Query key condition not supportedDynamodb 查询错误 - 不支持查询键条件
【发布时间】:2015-10-28 03:12:17
【问题描述】:

我正在尝试查询我的 dynamodb 表以获取 feed_guid 和 status_id = 1。但它返回 Query key condition not supported 错误。 请找到我的表架构和查询。

$result =$dynamodbClient->createTable(array(
            'TableName' => 'feed',
            'AttributeDefinitions' => array(
                array('AttributeName' => 'user_id', 'AttributeType' => 'S'),
                array('AttributeName' => 'feed_guid',    'AttributeType' => 'S'),
                array('AttributeName' => 'status_id',  'AttributeType' => 'N'),
            ),
            'KeySchema' => array(
                array('AttributeName' => 'feed_guid', 'KeyType' => 'HASH'),
            ),

            'GlobalSecondaryIndexes' => array(
                array(
                    'IndexName' => 'StatusIndex',
                    'ProvisionedThroughput' => array (
                        'ReadCapacityUnits' => 5,
                        'WriteCapacityUnits' => 5
                    ),
                    'KeySchema' => array(
                        array(
                            'AttributeName' => 'status_id',
                            'KeyType' => 'HASH'
                        ),
                    ),
                    'Projection' => array(
                        'ProjectionType' => 'ALL'
                    )
                ),

                array(
                    'IndexName' => 'UserIdIndex',
                    'ProvisionedThroughput' => array (
                        'ReadCapacityUnits' => 5,
                        'WriteCapacityUnits' => 5
                    ),
                    'KeySchema' => array(
                        array(
                            'AttributeName' => 'user_id',
                            'KeyType' => 'HASH'
                        ),
                    ),
                    'Projection' => array(
                        'ProjectionType' => 'ALL'
                    )
                )

            ),
            'ProvisionedThroughput' => array(
                'ReadCapacityUnits'  => 10,
                'WriteCapacityUnits' => 20
            )
        ));

以下是我更新该表的查询。

 $result = $dynamodbClient->query(array(
            'TableName' => 'feed',
            'KeyConditionExpression' => 'feed_guid = :v_fid AND status_id = :v_sid ',
            'ExpressionAttributeValues' =>  array(
                ':v_fid' => array('S' => '71a27f0547cd5456d9ee7c181b6cb2f8'),
                ':v_sid' => array('N' => 1)
            ),
            'ConsistentRead' => false
        ));

【问题讨论】:

  • 您尚未将 status_id 定义为执行此查询的范围
  • 所以我们不能只使用二级索引?

标签: amazon-web-services bigdata amazon-dynamodb


【解决方案1】:

如前所述,“KeyConditionExpression”中包含的属性应该只是您的哈希键,与您的基表架构匹配(在本例中为“feed_guid”)。如果要同时查询 'feed_guid' 和 'status_id',则需要使用哈希和范围键创建表,并指定 'status_id' 作为范围键。

全局二级索引与基表完全分离,所以这种情况下可以单独查询索引(查询StatusIndex时在key condition中使用'status_id',在查询UserIdIndex时在key condition中使用'user_id')。

更多查询全局二级索引详情请查看here

【讨论】:

  • 另外值得注意的是哈希键条件needs to be an equality:=;因此,如果您尝试在关键条件表达式的基于哈希的条件中使用 >=<begins_with 等,则可能会出现此错误。
【解决方案2】:

除了 KeyConditionExpression 之外,另一种选择是使用 FilterExpression。正如 Daniela 提到的 KeyConditionExpression 应该只包含散列键中的列。但是任何未索引的列都可以包含在 FilterExpression 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多