DynamoDB Table Samples –
Overview.
Scope:
- Into,
- Key concepts for tables,
- Table of Primary Key Types,
- Indexes,
- Data Model,
- Partitioning,
- Sample Designs Orders Table for E-commerce,
- Sample Designs Orders Table for IoT Device Readings,
- Sample Item (json),
- Sample for User Profiles with Flexible Attributes,
- Table of sample of DynamoDB tables (showing key structures & attributes defined) for client orders,
- Primary Key Design,
- Visual Architecture of DynamoDB Table for
Orders, - How it works,
Intro:
- Amazon DynamoDB is a fully managed NoSQL database service designed for high performance, scalability, and flexibility.
1. Key concepts for tables:
- A collection of items.
- Items are the records; attributes are the
fields.
- Every table must have a primary key (either simple or composite).
2. Table of Primary Key Types
|
Type |
Structure |
Use Case |
|
Simple Primary Key. |
Partition Key only (PK) |
When each item is uniquely
identified by one attribute |
|
Composite Primary Key. |
Partition Key (PK) + Sort Key (SK) |
When twtech needs multiple related items
per partition, sorted by SK |
3.
Indexes
- Global Secondary Index (GSI) → Alternate PK/SK for queries unrelated
to the main table’s key.
- Local Secondary Index (LSI) → Same PK as the table but a different
SK for sorting/filtering.
4. Data
Model
- Items are schema-less — attributes
can vary between items.
- twtech can store:
- Strings, Numbers, Booleans
- Lists, Maps (nested JSON-like)
- Binary data
5.
Partitioning
- DynamoDB automatically splits tables into
partitions based on PK (Public Key Key) hash.
- The goal: evenly distribute reads/writes.
A. Sample Designs Orders Table for E-commerce
Primary Key:
- Partition Key (PK): OrderId (string)
- Sort Key (SK): none (simple PK)
Attributes:
- CustomerId
- OrderDate
- Status
- Items (list of maps)
- TotalAmount
GSI1:
- Partition Key (PK): CustomerId
- Sort Key (SK): OrderDate
- Allows: “Get all orders for a customer, sorted by date”
B. Sample Designs Orders Table for IoT Device Readings
Primary Key:
- Partition Key (PK): DeviceId
- Sort Key (SK): Timestamp (ISO 8601)
Attributes:
- Temperature
- Humidity
- BatteryLevel
GSI1:
- Partition Key (PK): Timestamp (for time-based aggregation
across devices)
- Sort Key (SK): none
Sample Item (json)
# json
{
"DeviceId": "SENSOR-001",
"Timestamp": "2025-08-11T15:00:00Z",
"Temperature": 23.5,
"Humidity": 45,
"BatteryLevel": 88
}
C. Sample for User
Profiles with Flexible Attributes
Primary Key:
- PK: UserId (string)
Attributes:
- Name
- Email
- Preferences (map)
- JoinedDate
NB:
- The is No sort key — each user is a single item.
Sample Item (json):
# json
{
"UserId": "U1001",
"Name": "twtech",
"Email": "twtech671@gmail.com",
"Preferences": {
"Theme": "Dark",
"Notifications": true
},
"JoinedDate": "2024-05-12"
}
Table of DynamoDB tables (showing the key structures & attributes defined) for client orders.
|
Attribute
Name |
Type |
Role |
Notes |
|
OrderId. |
String. |
Partition Key (PK). |
Uniquely identifies the order.
Example: ORD12345 |
|
CustomerId. |
String. |
Sortable/secondary attribute. |
Could be used in a Global Secondary
Index (GSI) |
|
OrderDate. |
String. |
ISO 8601 date/time string. |
Example: 2025-08-11T15:30:00Z |
|
Status. |
String. |
Attribute. |
Example values: Pending, Shipped, Delivered |
|
Items. |
List<Map>. |
Attribute. |
Each item has ProductId, Quantity, Price |
|
TotalAmount. |
Number. |
Attribute. |
Example: 149.99 |
Primary
Key Design
- Partition Key: OrderId
- Note, that there is No Sort Key in this example (simple primary key).
- If twtech needs multiple orders per customer sorted by date, it could design:
- Partition Key: CustomerId
- Sort Key: OrderDate
Sample
Item (JSON)
# json
{
"OrderId": "ORD12345",
"CustomerId": "CUST987",
"OrderDate":
"2025-08-11T15:30:00Z",
"Status": "Shipped",
"Items": [
{ "ProductId": "P1001",
"Quantity": 2, "Price": 25.00 },
{ "ProductId": "P2005",
"Quantity": 1, "Price": 99.99 }
],
"TotalAmount": 149.99
}
Optional
Index
If twtech often query by customer:
- Global Secondary Index (GSI)
- Partition Key: CustomerId
- Sort Key: OrderDate
- Lets twtech to query: "All orders for CUST987
in the last 30 days"
Visual Architecture of DynamoDB Table for Orders
Global Secondary Index (GSI1)
- Partition Key:
CustomerId - Sort Key:
OrderDate
How it works
1. Partitioning
- DynamoDB distributes items into partitions using the hash
value of the Partition Key (
OrderId).
2. GSI (Global Secondary Index)
- The GSI creates an alternate sorted view of the table using
CustomerIdandOrderDatefor fast queries by customer over time.
3. Scaling
- As data grows, DynamoDB automatically adds partitions behind the scenes.
Sample A – Client Orders Table (E-commerce)
Sample B – IoT Device Readings
Sample C – User Profiles
What This Shows
- Main Table contains the full item data.
- GSI (Global Secondary Index) is a projection:
- Uses a different PK/SK.
- Can contain all attributes (if ProjectionType
= ALL) or just keys + select attributes.
- Items in the GSI point back to the
same data in the main table.
No comments:
Post a Comment