Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamoDB - Get multiple items from DB by array of primary keys

I have an array of users id's and I want to get all users with that id from the dynamoDB table

Didn't find it in the documentation

any ideas?

like image 659
goldylucks Avatar asked Dec 29 '15 10:12

goldylucks


People also ask

Can DynamoDB have 2 primary keys?

DynamoDB supports two types of primary keys: Partition key: A simple primary key, composed of one attribute known as the partition key. Attributes in DynamoDB are similar in many ways to fields or columns in other database systems.

Can DynamoDB have multiple hash keys?

Using normal DynamoDB operations you're allowed to query either only one hash key per request (using GetItem or Query operations) or all hash keys at once (using the Scan operation).

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 you Query by sort key only DynamoDB?

You can not query only using a Sort Key. You need to specify a partition key to perform query operations. Else, you need to create a global secondary index or perform a scan operation.


2 Answers

I ended up using batchGet, an operation of AWS.DynamoDB.DocumentClient

There's no support for multiple items with same key, so I have to define the key over and over again like this:

var dynamoConfig = {
  sessionToken:    process.env.AWS_SESSION_TOKEN,
  region:          process.env.AWS_REGION
};
var dynamodbDocClient = new AWS.DynamoDB.DocumentClient(dynamoConfig);

var params = {
  RequestItems: {
    tableName: {
      Keys: [
        {
          id: 'user1Id'
        },
        {
          id: 'user2Id'
        },
        {
          id: 'user3Id'
        }
      ]
    }
  }
}

dynamodbDocClient.batchGet(paramsForQueringFormerEvaluators, function(err, data) {
  if (err) {
    console.log('createEvaluation: get former evaluators: err: ', err);
    return;
  }

  var users = data.Responses.tableName;
  console.log('createEvaluation: get former evaluators: ', users);

});
like image 66
goldylucks Avatar answered Oct 16 '22 03:10

goldylucks


You can use the BatchGetItem API for this.

Of course, I can't help you with any code snippet without knowing your table schema. But you can look at the documentation here.

like image 30
ketan vijayvargiya Avatar answered Oct 16 '22 04:10

ketan vijayvargiya