我试图了解 DynamoDB 表如何与 GSI 配合使用,我对他们的文档感到非常困惑。
从这个链接中,音乐库表看起来像这样,格式类似 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)
组合.但他们写道
和排序键=下载ID
同样来自此链接,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 格式的表应该是什么样子的呢?
该图有点令人困惑,但"详细信息","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"
},
]