Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query multiple data with array of primary ids in DynamoDB

I'm working on a application that is using DynamoDB as the database. I have a table of users like:

{
   id: 1, (Primary Key)
   name: "Indranil"
}

Now my requirement is, I have the array if ids like : [1,5,8] and I want to load all the users using single query.

Now I'm using the id as keyConditionExpreassion in the query and I can fetch each user data one after another, but then it'll result in multiple queries.

    let readparams  = {
      TableName : "user",
      KeyConditionExpression: "# = :",
      ExpressionAttributeNames:{
        "#": "id"
      },
      ExpressionAttributeValues: {
        ":id":1
      }
    };

Then I saw there is an 'IN' operation in the expressions but that can't be used in the KeyConditionExpression, it can be used in FilterExpreassion but in filterExpression I can't use primary key. And moreover I can't pass only filter expression in the query I have to pass a key expression as well but in my case I don't have any other condition to check.

    let readparams  = {
      TableName : "user",
      FilterExpression: "id IN (:id1, :id2)",
      ExpressionAttributeValues: {
        ":id1":1,
        ":id2":1
      }
    };

So eventually I've left with only option, to load all the data in the frontend and then filter those using ids in the frontend, but obviously that's not a good solution at all.

Can someone please point out what is the best solution here.

like image 998
Indranil Mondal Avatar asked Nov 16 '17 11:11

Indranil Mondal


People also ask

Does DynamoDB support array?

A DynamoDB Set deserializes into an object with its array of items stored under the values property. If we want a Set to deserialize into an array, we'll need to add an unmarshalling step where we assign the values property instead of the deserialized set object itself.

Can DynamoDB handle complex queries?

DynamoDB has many attractive features. For example, it can automatically scale to handle trillions of calls in a 24-hour period. It can be used as a key-value store or a document database, and it can handle complex access patterns much faster than a typical relational database.

What is the API call to retrieve multiple items from a DynamoDB table?

The BatchGetItem operation returns the attributes of one or more items from one or more tables. You identify requested items by primary key. A single operation can retrieve up to 16 MB of data, which can contain as many as 100 items.

Can DynamoDB have multiple GSI?

You can create 20 global secondary indexes for a DynamoDB table as of the time this page was written. Sometimes, though, your application might need to support multiple access patterns and exceed the current limit of global secondary indexes per table.


1 Answers

Batch get item API can be used to get multiple items by key attributes. If you have sort key defined for your table, you need to provide both partition and sort key attribute value.

Batch get item

Some sample code:-

var table = "phone";

var params = {
    "RequestItems" : {
        "phone" : {
            "Keys" : [ {
                "phone_num" : "+14085551212",
                "Country" : "USA"
            },
            {
                "phone_num" : "+14085551313",
                "Country" : "USA"
            }
             ]
        }
    }

};

docClient.batchGet(params, function(err, data) {
    if (err) {
        console.error("Unable to read item. Error JSON:", JSON.stringify(err,
                null, 2));
    } else {
        console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
    }
});
like image 142
notionquest Avatar answered Oct 28 '22 12:10

notionquest