25

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?

4 Answers 4

14

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);

});
Sign up to request clarification or add additional context in comments.

1 Comment

what is paramsForQueringFormerEvaluators in your case? params?
6

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.

Comments

3

You can probably use Scan method.

I'm currently using python boto3 package. Ref to scan in boto3. So the code would be like:

table = boto3.resourse('dynamodb').Table('users-table-name')
response = table.scan(
    ScanFilter={'user-id': {
        'AttributeValueList': user_ids,
        'ComparisonOperator': 'IN' }
    }
)

But i'm not sure what is better, using BatchGetItem or Scan. Probably it depends on two factors:

  1. how many records in table (table.item_count)
  2. how many items you need to get (count(user_ids))

If count(user_ids) << table.item_count then you definitely need to use BatchGetItem.

If count(user_ids) / table.item_count -> 1 then you need to use Scan.

3 Comments

Avoid scan operations if you know the items id's you're looking for. They're expensive to execute because the full data set - potentially across partitions - has to be scanned.
This can absolutely destroy you. Never use scan in a prod environment unless you have a very small table
Really bad idea, avoid use scan unless you don't known the PK. And if you don't known the PK, try to refactor your table to avoid scan!
1

I wrote a function that converts a list of IDs into a list of documents using batchGet:

async function getDocs(ids, docClient, tableName, key='id') {
  const getParams = {
    RequestItems: {
      [tableName]: {
        Keys: ids.map(id => ({[key]: id}))
      }
    }
  }
  return (await docClient.batchGet(getParams).promise()).Responses[tableName];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.