Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying a Global Secondary Index in dynamodb Local

I am creating a table and GSI in DynamoDB, using these parameters, as per the documentation:

configId is the primary key of the table, and I am using the publisherId as the primary key for the GSI. (I've removed some unnecessary configuration parameters for brevity)

var params = {     TableName: 'Configs',     KeySchema: [          {             AttributeName: 'configId',             KeyType: 'HASH',         }     ],     AttributeDefinitions: [         {             AttributeName: 'configId',             AttributeType: 'S',         },         {             AttributeName: 'publisherId',             AttributeType: 'S',         }     ],     GlobalSecondaryIndexes: [          {              IndexName: 'publisher_index',              KeySchema: [                 {                     AttributeName: 'publisherId',                     KeyType: 'HASH',                 }             ]         }     ] }; 

I am querying this table using this:

{ TableName: 'Configs',   IndexName: 'publisher_index',   KeyConditionExpression: 'publisherId = :pub_id',   ExpressionAttributeValues: { ':pub_id': { S: '700' } } } 

but I keep getting the error:

"ValidationException: One or more parameter values were invalid: Condition parameter type does not match schema type"

In the docs it specifies that the primary KeyType can either be HASH or RANGE, and that you set the AttributeType in the AttributeDefinitions field. I am sending the publisherId as String, not sure what I am missing here.

Is the issue in the way I am creating the table, or the way I am querying? Thanks

like image 536
Stelios Savva Avatar asked Nov 21 '15 19:11

Stelios Savva


People also ask

Can you query on global secondary index?

A global secondary index lets you query over the entire table, across all partitions. A local secondary index lets you query over a single partition, as specified by the partition key value in the query. Queries on global secondary indexes support eventual consistency only.

How do you query a local secondary index?

Query a Local Secondary IndexYou must specify the index name, the query criteria for the index sort key, and the attributes that you want to return. In this example, the index is AlbumTitleIndex and the index sort key is AlbumTitle . The only attributes returned are those that have been projected into the index.

What is a global secondary index in DynamoDB?

DynamoDB supports two types of secondary indexes: Global secondary index — An index with a partition key and a sort key that can be different from those on the base table. A global secondary index is considered "global" because queries on the index can span all of the data in the base table, across all partitions.


2 Answers

Try to change ExpressionAttributeValues from this

{   TableName: 'Configs',  IndexName: 'publisher_index',  KeyConditionExpression: 'publisherId = :pub_id',  ExpressionAttributeValues: { ':pub_id': { S: '700' } }  } 

to

{   TableName: 'Configs',  IndexName: 'publisher_index',  KeyConditionExpression: 'publisherId = :pub_id',  ExpressionAttributeValues: { ':pub_id': '700'}  } 

From { ':pub_id': { S: '700' } } to { ':pub_id': '700'}.

I had the same problem and I spent 2 days for this. The official documentation in misleading.

like image 68
gior91 Avatar answered Oct 02 '22 10:10

gior91


Turns out it depends on whether you use AWS.DynamoDB or AWS.DynamoDB.DocumentClient.

Check the difference in the documentation: AWS.DynamoDB.query vs. AWS.DynamoDB.DocumentClient.query

In the DocumentClient the doc clearly states:

The document client simplifies working with items in Amazon DynamoDB by abstracting away the notion of attribute values. This abstraction annotates native JavaScript types supplied as input parameters, as well as converts annotated response data to native JavaScript types.

...

Supply the same parameters as AWS.DynamoDB.query() with AttributeValues substituted by native JavaScript types.

Maybe you where also referring to the DynamoDB API Reference which in fact makes no assumptions about the used SDK but uses plain HTTP Requests in the examples.

Thus using the AWS.DynamoDB.DocumentClient you would just provide a simple key-value map for ExpressionAttributeValues as suggested by @gior91.

like image 25
TheWebweiser Avatar answered Oct 02 '22 11:10

TheWebweiser