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?
Few things:
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())));
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With