Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to batch write with conditional expression on a GSI with AWS DynamoDB?

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?

like image 374
RTF Avatar asked Oct 21 '25 13:10

RTF


2 Answers

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())));
like image 152
John Jones Avatar answered Oct 23 '25 08:10

John Jones


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

like image 29
Pedro Garcia Avatar answered Oct 23 '25 06:10

Pedro Garcia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!