Referring to https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-adjacency-graphs.html. I was wondering if anyone could help me.
The first image is o
In the first picture you posted the items in the base table (primary key) would look like this:
First_id(Partition key) Second_id(Sort Key) Dated
------------- ---------- ------
Invoice-92551 Invoice-92551 2018-02-07
Invoice-92551 Bill-4224663 2017-12-03
Invoice-92551 Bill-4224687 2018-01-09
Invoice-92552 Invoice-92552 2018-03-04
Invoice-92552 Bill-4224687 2018-01-09
Bill-4224663 Bill-4224663 2018-12-03
Bill-4224687 Bill-4224687 2018-01-09
And the same items in the GSI the items would look like this
Second_id(Partition Key) First_id
---------- ---------------
Invoice-92551 Invoice-92551
Bill-4224663 Invoice-92551
Bill-4224687 Invoice-92551
Invoice-92552 Invoice-92552
Bill-4224687 Invoice-92552
Bill-4224663 Bill-4224663
Bill-4224687 Bill-4224687
They have drawn it in quite a confusing way.
When you do a query on the base table, you can use a query with the partition key Invoice-92551
and you get both the Invoice item plus all the bill items that belong to it.
Imagine you are viewing invoice Invoice-92551
in an application and you can see it has two associated bills (Bill-4224663
and Bill-4224687
). If you clicked on the bill, the application would probably do a query on the GSI. The GSI query would have partition key Bill-4224687
. If you look at the GSI table I have drawn above, you can see this will return two items, showing that Bill-4224687
is part of two invoices (Invoice-92551
and Invoice-92552
)
In your second picture, the words 'bookID' and 'albumID' etc are supposed to represent actual IDs (lets say 293847 and 3340876).
I would draw his example like this:
ProductID(Partition Key) TypeID(Sort Key) Title Name
--------- ------ ------ ------
Album1 Album1 Dark Side
Album1 Album1:Track1 Speak to me
Album1 Album1:Track2 Breathe
Movie8 Movie8 Idiocracy
Movie8 Movie8:Actor1 Luke Wilson
Movie8 Movie8:Actor2 Maya Rudolph
Here are your queries:
Partition key: Album1
Gives you ALL the information (inc tracks) on Album 1 (Dark Side)
Partition key: Album1 and Sort Key: Album1:Track2
Gives you just the information on Breathe.
Partition key: Movie8
Gives you ALL the information (inc actors) on Movie8 (Idiocracy)
If I was building the table I would make it so the words Movie, Album etc were part of the actual ID (say Movie018274 and Album983745987) but that's not required, it just makes the IDs more human readable.
Stu's answer is not quite correct, the table actually looks as it is illustrated:
First_id(Partition key) Second_id(Sort Key) Dated
------------- ---------- ------
Invoice-92551 Invoice-92551 2018-02-07
Invoice-92551 Bill-4224663 2017-12-03
Invoice-92551 Bill-4224687 2018-01-09
Invoice-92552 Invoice-92552 2018-03-04
Invoice-92552 Bill-4224687 2018-01-09
Bill-4224663 Bill-4224663 2018-12-03
Bill-4224687 Bill-4224687 2018-01-09
In the table above, the Bill items (i.e. partition key = Bill-xxxxx) hold common information for the bill, where as the Invoice items with Bill items as sort key hold information for the bill that is specific to the given invoice.
In order to fully reconstruct a bill, a GSI is required that allows you to lookup the complete information for a bill (i.e. the common record + invoice specific records):
Second_id(Partition Key) First_id Data
---------- --------------- -----------
Bill-4224663 Bill-4224663 Common bill data
Bill-4224663 Invoice-92551 Bill data for Invoice-92551
Bill-4224687 Bill-4224687 Common bill data
Bill-4224687 Invoice-92551 Bill data for Invoice-92551
Bill-4224687 Invoice-92552 Bill data for Invoice-92552
Invoice-92551 Invoice-92551 Redundant data!
Invoice-92552 Invoice-92552 Redundant data!