Viet
Viet

Reputation: 6943

AWS DynamoDB tables and GSI

I'm trying to understand how DynamoDB tables work with GSI and I'm very confused by their documentation.

From this link, a music library table looks like this in a JSON-like format (if I understand it correctly):

// Music Library Table
[
    {
        "song_id": "song-129", // Partition Key
        "details": { /** details is a Sort Key */
            "title": "Wild Love",
            "artist": "Argyboots",
            "downloads": 15000,
            // etc.
        },
        "month-2018-01": { /** Also a Sort Key? */
            "month": "2018-01", /** GSI Primary Key */
            "month_total": 1000 /** GSI Secondary Key */
        },
        "download_id_1": { /** Also a Sort Key? */
            "time": "timestamp"
        },
        "download_id_2": { /** Also a Sort Key? */
            "time": "timestamp"
        },
        "download_id_3": { /** Also a Sort Key? */
            "time": "timestamp"
        },
    }
]

It seems like there are several combinations of Primary Keys = (Partition Key + Details / Month / DownloadID). But they wrote

and Sort-Key=DownloadID

Also from this link, an HR table looks like this:

// HR Table
[
    {
        "employee_id": "hr-974", /** Partition Key */
        "employee_name": { /** Also a Sort Key? */
            "name": "Murphy, John",
            "start_date": "2008-11-08",
            // etc.
        },
        "YYY-Q1": { /** Also a Sort Key? */
            "order_total": "$5,000",
            "name": "Murphy, John"
        },
        // ...
        "v0_job_title": { /** Also a Sort Key? */
            "job_title": "operator-1",
            "start_date": "2008-11-08",
            // etc.
        },
        "v1_job_title": { /** Also a Sort Key? */
            "job_title": "operator-2",
            "start_date": "2008-11-10",
            // etc.
        }
    }
]

But it seems like it doesn't because:

Use the global secondary index to find all employees working in a particular warehouse by searching on a warehouse ID (such as Warehouse_01).

It seems like warehouses have their own entries with their own IDs.

So how should the tables look like in JSON format?

Upvotes: 0

Views: 137

Answers (1)

Deiv
Deiv

Reputation: 3097

That diagram is a bit confusing, but "details", "month-2018-01", etc. are not all individual sort keys. They are actually all under one "sort key", same way "Song-129" is not a partition key in it's own way, it's under the partition key "song_ID".

To make things clearer, this is what it would look like in JSON format:

// Music Library Table (if this is a list containing all individual items in the table)
[
    {
        "song_id": "song-129", // Partition Key
        "sort_key": "details", // Sort Key
        "title": "Wild Love",
        "artist": "Argyboots",
        "downloads": 15000,
        // etc.
    },
    {
        "song_id": "song-129", // Partition Key
        "sort_key": "month-2018-01", // Sort Key
        "month": "2018-01",  // GSI Partition Key
        "month_total": "1000"  // GSI Sort Key
    },
    {
        "song_id": "song-129", // Partition Key
        "sort_key": "download_id_1", // Sort Key
        "time": "timestamp"
    },
    {
        "song_id": "song-129", // Partition Key
        "sort_key": "download_id_2", // Sort Key
        "time": "timestamp"
    },
    {
        "song_id": "song-129", // Partition Key
        "sort_key": "download_id_3", // Sort Key
        "time": "timestamp"
    },
]

Upvotes: 2

Related Questions