Dynamodb 查询错误 - 不支持查询键条件 [英] Dynamodb query error - Query key condition not supported

查看:31
本文介绍了Dynamodb 查询错误 - 不支持查询键条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

$result =$dynamodbClient->createTable(array('表名' =>'喂养','属性定义' =>大批(array('AttributeName' => 'user_id', 'AttributeType' => 'S'),array('AttributeName' => 'feed_guid', 'AttributeType' => 'S'),array('AttributeName' => 'status_id', 'AttributeType' => 'N'),),'KeySchema' =>大批(array('AttributeName' => 'feed_guid', 'KeyType' => 'HASH'),),'GlobalSecondaryIndexes' =>大批(大批('索引名称' =>'状态索引','ProvisionedThroughput' =>大批 ('ReadCapacityUnits' =>5、'WriteCapacityUnits' =>5),'KeySchema' =>大批(大批('属性名' =>'status_id','KeyType' =>'哈希'),),'投影' =>大批('投影类型' =>'全部')),大批('索引名称' =>'UserIdIndex','ProvisionedThroughput' =>大批 ('ReadCapacityUnits' =>5、'WriteCapacityUnits' =>5),'KeySchema' =>大批(大批('属性名' =>'用户身份','KeyType' =>'哈希'),),'投影' =>大批('投影类型' =>'全部'))),'ProvisionedThroughput' =>大批('ReadCapacityUnits' =>10,'WriteCapacityUnits' =>20)));

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

 $result = $dynamodbClient->query(array('表名' =>'喂养','KeyConditionExpression' =>'feed_guid = :v_fid AND status_id = :v_sid ','ExpressionAttributeValues' =>大批(':v_fid' =>数组('S' => '71a27f0547cd5456d9ee7c181b6cb2f8'),':v_sid' =>数组('N' => 1)),'一致读取' =>错误的));

解决方案

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

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

请在这里找到更多关于查询全球二级索引的细节/p>

I am trying to query my dynamodb table to get feed_guid and status_id = 1. But it returns Query key condition not supported error. Please find my table schema and query.

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

Following is my query to update that table.

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

解决方案

As mentioned, the attribute included in "KeyConditionExpression" should be your hash key only, matching your base table schema (in this case 'feed_guid'). If you want to query on both 'feed_guid' and 'status_id', you need to create the table with hash and range key and specify 'status_id' as your range key.

Global secondary indexes are completely separate from the base table, so in this case you can query the indexes separately (use 'status_id' in key condition when querying StatusIndex and use 'user_id' in key condition when querying UserIdIndex).

Please find more details on querying global secondary indexes here

这篇关于Dynamodb 查询错误 - 不支持查询键条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆