AWS DynamoDB表和GSI

时间:2019-03-29 14:34:52

标签: json amazon-web-services amazon-dynamodb

我试图了解DynamoDB表如何与GSI配合使用,并且对其文档感到非常困惑。

在此link中,音乐库表的格式类似于JSON(如果我正确理解的话):

// 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"
        },
    }
]

似乎Primary Keys = (Partition Key + Details / Month / DownloadID)有几种组合。但是他们写了

  

and Sort-Key =下载ID

此外,此link的HR表如下所示:

// 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.
        }
    }
]

但似乎不是因为:

  

使用全局二级索引通过搜索仓库ID(例如Warehouse_01)来查找在特定仓库中工作的所有员工。

看来仓库有自己的条目和ID。

那么这些表的JSON格式应该如何?

1 个答案:

答案 0 :(得分:2)

该图有点让人困惑,但是“详细信息”,“ month-2018-01”等并非都是单独的排序键。它们实际上都在一个“排序键”下,以同样的方式,“ Song-129”不是按其自身方式的分区键,而是在分区键“ song_ID”下。

为了使事情更清楚,这是JSON格式的样子:

// 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"
    },
]