21

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?

Shining Love Star
  • 5,734
  • 5
  • 39
  • 49

4 Answers4

13

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

});
Shining Love Star
  • 5,734
  • 5
  • 39
  • 49
5

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.

ketan vijayvargiya
  • 5,409
  • 1
  • 21
  • 34
5

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.

vishes_shell
  • 22,409
  • 6
  • 71
  • 81
  • 7
    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. – Andres Oct 26 '20 at 00:39
  • 1
    This can absolutely destroy you. Never use scan in a prod environment unless you have a very small table – Kloar Jan 04 '22 at 16:15
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];
}
Jack
  • 5,354
  • 2
  • 29
  • 54