Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamodb query error - Query key condition not supported

Tags:

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         )); 
like image 839
Arun SS Avatar asked Aug 05 '15 11:08

Arun SS


People also ask

Does Amazon DynamoDB support conditional operations?

Yes, like all the other database management systems, DynamoDB also supports all the conditional operators, User can specify a condition that is satisfied for a put, update, or delete operation to work on an item.

Can you Query DynamoDB without partition key?

The Query operation in Amazon DynamoDB finds items based on primary key values. You must provide the name of the partition key attribute and a single value for that attribute.

What is key condition?

KeyConditions are the selection criteria for a Query operation. For a query on a table, you can have conditions only on the table primary key attributes. You must provide the partition key name and value as an EQ condition. You can optionally provide a second condition, referring to the sort key.

Can we Query DynamoDB with sort key?

In an Amazon DynamoDB table, the primary key that uniquely identifies each item in the table can be composed not only of a partition key, but also of a sort key. Well-designed sort keys have two key benefits: They gather related information together in one place where it can be queried efficiently.


2 Answers

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

like image 80
Daniela Miao Avatar answered Sep 19 '22 00:09

Daniela Miao


Another option would be to use a FilterExpression in addition to the KeyConditionExpression. As Daniela mentioned KeyConditionExpression should contain only columns in the hash key. But any un-indexed columns can be included in the FilterExpression.

like image 44
Vasu Kargi Avatar answered Sep 21 '22 00:09

Vasu Kargi