AWS DynamoDB表和GSI

0

【以下的问题经过翻译处理】 我正在尝试理解DynamoDB表如何使用GSI,但他们的文档让我很困惑。

根据https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-gsi-aggregation.html,以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"
        },
    }
]

看起来有几种主键组合=(分区键+Details/Month/DownloadID)。但他们写道:

并且排序键=DownloadID

另外从https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-gsi-overloading.html,一个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格式中应该是什么样子的?

profile picture
EXPERTE
gefragt vor 8 Monaten46 Aufrufe
1 Antwort
0

【以下的回答经过翻译处理】 DynamoDB表起初非常令人困惑,对于了解关系型数据库的人来说,需要将其视为JSON甚至是如何在电子表格中布局数据。

让我们集中关注HR数据,这是你的第二个问题。

首先,您需要在键为Warehouse_01的表中查找数据。

返回的数据是:

{ PK: "HR-974", SK: "Warehouse_01", "Attr1": "Murphy,John" }

使用该PK,您现在可以进行第二次getItem调用,其中PK = HR-974并获取所有这些数据(员工信息,Q1订单总数,仓库ID,工作标题历史记录)。

如果您仅想要员工数据,可以查找PK = HR-974且SK = Employee_Name。

虽然不在示例中,但仓库也可能有一个条目。

PK = Warehouse_01,并且可以使用以下任何一个SK:位置,库存类型,仓库类型等以及数据项属性。

JSON看起来很棘手,因为:

1.取决于您通过表抓取的哪个数据片段 2.是来自DynamoDB的JSON还是特定于应用程序的JSON

在您的应用程序中,您必须跟踪SK是EmployeeName时Attr1表示EmployeeName的事实,但是如果SK是<YYYY-QQ>,则Attr1表示OrderTotals。

原始JSON

{

PK:HR-974,SK:“Employee_Name”,Attr1:“Murphy,John”,...

}

应用程序级JSON

{

EmployeeID:HR-974,Type:“Employee_Name”,EmployeeName:“Murphy,John”,...

}

如果这很有用,请订阅我的https://thathelpfulhuman.com/serverless

profile picture
EXPERTE
beantwortet vor 8 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen