What is Hash and Range Primary Key?

前端 未结 4 2026
北海茫月
北海茫月 2020-12-12 08:51

I am not able to understand what Range / primary key is here in the docs on Working with Tables and Data in DynamoDB

How does it work?

What do they mean by &qu

4条回答
  •  Happy的楠姐
    2020-12-12 09:36

    As the whole thing is mixing up let's look at it function and code to simulate what it means consicely

    The only way to get a row is via primary key

    getRow(pk: PrimaryKey): Row

    Primary key data structure can be this:

    // If you decide your primary key is just the partition key.
    class PrimaryKey(partitionKey: String)
    
    // and in thids case
    getRow(somePartitionKey): Row
    

    However you can decide your primary key is partition key + sort key in this case:

    // if you decide your primary key is partition key + sort key
    class PrimaryKey(partitionKey: String, sortKey: String)
    
    getRow(partitionKey, sortKey): Row
    getMultipleRows(partitionKey): Row[]
    

    So the bottom line:

    1. Decided that your primary key is partition key only? get single row by partition key.

    2. Decided that your primary key is partition key + sort key? 2.1 Get single row by (partition key, sort key) or get range of rows by (partition key)

    In either way you get a single row by primary key the only question is if you defined that primary key to be partition key only or partition key + sort key

    Building blocks are:

    1. Table
    2. Item
    3. KV Attribute.

    Think of Item as a row and of KV Attribute as cells in that row.

    1. You can get an item (a row) by primary key.
    2. You can get multiple items (multiple rows) by specifying (HashKey, RangeKeyQuery)

    You can do (2) only if you decided that your PK is composed of (HashKey, SortKey).

    More visually as its complex, the way I see it:

    +----------------------------------------------------------------------------------+
    |Table                                                                             |
    |+------------------------------------------------------------------------------+  |
    ||Item                                                                          |  |
    ||+-----------+ +-----------+ +-----------+ +-----------+                       |  |
    |||primaryKey | |kv attr    | |kv attr ...| |kv attr ...|                       |  |
    ||+-----------+ +-----------+ +-----------+ +-----------+                       |  |
    |+------------------------------------------------------------------------------+  |
    |+------------------------------------------------------------------------------+  |
    ||Item                                                                          |  |
    ||+-----------+ +-----------+ +-----------+ +-----------+ +-----------+         |  |
    |||primaryKey | |kv attr    | |kv attr ...| |kv attr ...| |kv attr ...|         |  |
    ||+-----------+ +-----------+ +-----------+ +-----------+ +-----------+         |  |
    |+------------------------------------------------------------------------------+  |
    |                                                                                  |
    +----------------------------------------------------------------------------------+
    
    +----------------------------------------------------------------------------------+
    |1. Always get item by PrimaryKey                                                  |
    |2. PK is (Hash,RangeKey), great get MULTIPLE Items by Hash, filter/sort by range     |
    |3. PK is HashKey: just get a SINGLE ITEM by hashKey                               |
    |                                                      +--------------------------+|
    |                                 +---------------+    |getByPK => getBy(1        ||
    |                 +-----------+ +>|(HashKey,Range)|--->|hashKey, > < or startWith ||
    |              +->|Composite  |-+ +---------------+    |of rangeKeys)             ||
    |              |  +-----------+                        +--------------------------+|
    |+-----------+ |                                                                   |
    ||PrimaryKey |-+                                                                   |
    |+-----------+ |                                       +--------------------------+|
    |              |  +-----------+   +---------------+    |getByPK => get by specific||
    |              +->|HashType   |-->|get one item   |--->|hashKey                   ||
    |                 +-----------+   +---------------+    |                          ||
    |                                                      +--------------------------+|
    +----------------------------------------------------------------------------------+
    

    So what is happening above. Notice the following observations. As we said our data belongs to (Table, Item, KVAttribute). Then Every Item has a primary key. Now the way you compose that primary key is meaningful into how you can access the data.

    If you decide that your PrimaryKey is simply a hash key then great you can get a single item out of it. If you decide however that your primary key is hashKey + SortKey then you could also do a range query on your primary key because you will get your items by (HashKey + SomeRangeFunction(on range key)). So you can get multiple items with your primary key query.

    Note: I did not refer to secondary indexes.

提交回复
热议问题