2
votes

Is it possible to do batch write to DynamoDB (using the Java SDK v1.11.1 with the document API) while using the value of a global secondary index as a conditional expression?

For example, I'd like to issue a batch update request that would update the attributes A, B and C for any items where the GSI MyIndex is equal to some value.

Is this possible, or do I have to do a query on the GSI and use the resulting primary keys for the batch write?

2

2 Answers

8
votes

Few things:

  • Queries must be against a Table/Index's Primary Key (so no joining on a GSI)
  • To the best of my knowledge you can't add ConditionalExpressions to batchWriteItem.
  • batchWriteItem can't update. (see first note here)

I'd do something like this:

// Get keys from GSI
dynamodb.query({
  IndexName: 'index',
  TableName: 'table',
  KeyConditionExpression: 'some expression',
}, (error, rows) =>
// Spin up a bunch of update requests
Promise.all(rows.Items.map((row) => dynamodb.update({
  ExpressionAttributeValues: {
    ':aVal': 'new value for a',
    ':bVal': 'etc',
    ':cVal': 'etc',
  },
  IndexName: 'index',
  Key: {
    primaryKey: row.primaryKey,
  },
  KeyConditionExpression: 'primaryKey = :primKey',
  UpdateExpression: 'set a = :aVal, b = :bVal, c = :cVal',
}).promise())));
0
votes

BatchWriteItem does not behave in the same way as individual PutItem and DeleteItem calls would. For example, you cannot specify conditions on individual put and delete requests.

see docs