4

Our table has a composite primary key. There are a few use cases where we need to update the items based on just the partition key.

partitionKey    sortKey
------------    ---------
10020525        208025117-xxxxx-153068323-208025401
10020525        208025117-208025475-153068323-208025401

Following is how I am currently updating: (Using Java)

UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName("table").withKey(attMap);
UpdateItemResult updateItemResult = amazonDynamoDB.updateItem(updateItemRequest);

The 'attMap' already has the 'partitionKey' and 'sortKey'. Therefore, it does take care of updating the record matches the partition and sort key.

In a few cases (where there is a 'xxxxx' in the sort key), I want to update based on just the partitionKey.

Let me know if something is not clear. Would be happy to explain further.

miserable
  • 697
  • 1
  • 12
  • 31
  • 2
    Do you mean something like this?: `update x where pk=10020525 ` – Alex Jan 01 '20 at 12:20
  • 1
    Yes. Ignore the sort key in a few use cases. – miserable Jan 02 '20 at 03:40
  • 1
    You could first find all the items using partitionKey (sort key is not mandatory). But to update each item, you would have to map and update the items. You could also do batch update here – user3807691 Jan 02 '20 at 08:33

2 Answers2

4

Updating multiple items at once is not supported by DynamoDB. For this reason you cannot make update requests with just the partition key unless your table schema only has a partition key and no sort key.

This is probably not the answer you were hoping for but it is how DynamoDB works. You have to consider your use-cases carefully and choose a schema that works, or consider different database solutions.

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • Is there a workaround or any other way to achieve this? Other than considering a different database. – miserable Jan 02 '20 at 03:41
  • 2
    You can query first and then do a batch update but you will still have to identify each item by its exact partition+sort key. – Mike Dinescu Jan 02 '20 at 03:54
  • @MikeDinescu. How can i retrieve the 'id' of my DynamoDB database entry?: https://stackoverflow.com/questions/60929031/how-do-i-use-a-global-secondary-index-in-dynamodb-to-get-the-id-of-a-user-in-m – chai86 Mar 31 '20 at 10:30
0

By default Dynamo DB does not allow such updates, you need to provide both partition key and sort key in update operation. One thing worth trying is to use transaction request so that you include all items which match the partition key. This approach works when you have limited number of records with fits the SDK API limitations for single transaction request. Here is an example:

List<Update> updates = records.stream()
            .map(record -> Update.builder()
                    .tableName(this.someTable)
                    .key(ImmutableMap.of(
                         PART_KEY, AttributeValue.builder().s(record.partKey).build(), 
                         SORT_KEY, AttributeValue.builder().s(record.sortKey).build()))
                    .build()).collect(Collectors.toList());

Collection<TransactWriteItem> actions = new ArrayList<>();
updates.stream().forEach(update -> 
       actions.add(TransactWriteItem.builder().update(update).build()));

TransactWriteItemsRequest updateProfileTransaction = TransactWriteItemsRequest
            .builder()
            .transactItems(actions)
            .build();
Alex
  • 8,827
  • 3
  • 42
  • 58