Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

amazon web services - Dynamodb query error - Query key condition not supported

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
        ));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...