4
votes

I am using AWS.DynamoDB.DocumentClient in a nodejs program to fetch items from multiple Dynamodb tables. To make code simple, I choose to use BatchGetItem/BatchGet method.

The challenge is I need to fetch items based on a Global Secondary Index, e.g. name+age, rather than the initial primary key generated when creating the table. I went through BatchGetItem/BatchGet but not see any parameters of using Global Secondary Index.

I ran some testing with the following code

var params = {
  RequestItems: {
    'Table-1': {
      Keys: [
        { 
            name: 'abc',
            age: 18,
         },
      ]
    }
  }
};

var docClient = new AWS.DynamoDB.DocumentClient();

docClient.batchGet(params, function(err, data) {
  if (err) console.log(err);
  else console.log(data);
});

And got following error.

> ValidationException: The provided key element does not match the
> schema

Does it mean BatchGetItem/BatchGet can't use Global Secondary Index, and I have to read from tables one by one?

1

1 Answers

0
votes

I don't believe so. You will likely have to query one-by-one.

INDEXES - The response includes the aggregate ConsumedCapacity for the operation, together with ConsumedCapacity for each table and secondary index that was accessed. Note that some operations, such as GetItem and BatchGetItem , do not access any indexes at all. In these cases, specifying INDEXES will only return ConsumedCapacity information for table(s).

Source: https://docs.aws.amazon.com/cli/latest/reference/dynamodb/batch-get-item.html