dynamic nested map fields as key in dynamodb

0

Hi, I am trying to evaluate if I can model a dynamodb table to have dynamic keys without adding and removing global secondary indexes. Assume entries like this:

{
  "name": { "S": "myitem" },
  "keys": {
    "M": {
      "key1": { "S": "aaa" },
      "key2": { "S": "bbb" },
      ...
    }
  }
}

Each item in the DB has a unique name. I have several consumer applications, each queries the table using its own key (key1, key2, ...) and needs to retrieve matching items. The list of keys is not uniform across table items. The set of possible key names is known but it changes over time in a controlled way, not too frequently though.

The goal is to model the data in a way that allows using dynamodb indexes and avoid inefficient scans. I thought about maintaining a global secondary index per key name but this complicates the data model as the keys must be moved out to top-level properties which makes mapping the items to consumer objects more complicated, and also forces us to modify table indexes.

Any ideas?

asked 7 months ago660 views
1 Answer
1

Hi Tarek,

This is a perfect candidate for something known as single table design. With single table design, you can set up a Partition Key and Sort Key with the names PK and SK respectively. The PK and SK make up the composite primary key.

Inside the PK and SK, you can define the data as follows:

PK


KEY#keyvalue

KEY#aaaa

KEY#bbb

SK


SORTKEY#sortkeyvalue

SORTKEY#sortaaaa

SORTKEY#sortbbb

In fact, you can even define the data as follows in these keys: KEY#keyvalue#SECONDKEY#secondkeyvalue.

This is a difficult concept to explain this brief post. Instead, I recommend that you review: https://explore.skillbuilder.aws/learn/public/learning_plan/view/1840/amazon-dynamodb-learning-plan

The section "Architecting Applications and Tables for DynamoDB" inside of "Developing with DynamoDB" covers single table design.

Here's an article that also describes this approach: https://aws.amazon.com/blogs/compute/creating-a-single-table-design-with-amazon-dynamodb/

Hope this helps. If it does, please accept this answer and give it a thumbs up.

profile picture
answered 7 months ago
  • Hi Bryant, I did evaluate the single table design but it requires several reads in order to get the full image to avoid data duplication, one for the relationship and another for the related data. my goal is to perform a single atomic operation while looking up an item by a relationship key.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions