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?

已提問 8 個月前檢視次數 713 次
1 個回答
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
已回答 8 個月前
  • 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.

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南