0
0
DynamoDBquery~30 mins

Adjacency list pattern in DynamoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Building a Family Tree Using the Adjacency List Pattern in DynamoDB
📖 Scenario: You want to create a simple family tree database using DynamoDB. Each person will have a unique ID and a reference to their parent's ID. This way, you can see who is related to whom by following the parent links.
🎯 Goal: Build a DynamoDB table structure using the adjacency list pattern to represent family members and their parent-child relationships.
📋 What You'll Learn
Create a DynamoDB table with a primary key called PersonID
Add items representing family members with attributes PersonID, Name, and ParentID
Use ParentID to link each child to their parent
Write a query to find all children of a specific parent using ParentID
Ensure the data structure supports easy traversal of family relationships
💡 Why This Matters
🌍 Real World
This pattern is useful for storing hierarchical data like organizational charts, family trees, or category trees in databases that do not support joins.
💼 Career
Understanding adjacency list pattern helps in designing efficient NoSQL database schemas and writing queries to navigate hierarchical relationships.
Progress0 / 4 steps
1
Create the DynamoDB table and add family members
Create a DynamoDB table named FamilyTree with PersonID as the primary key. Then add these family members as items with attributes exactly as follows: PersonID: '1', Name: 'John', ParentID: null, PersonID: '2', Name: 'Mary', ParentID: '1', and PersonID: '3', Name: 'Sam', ParentID: '1'.
DynamoDB
Need a hint?

Think of FamilyTree as a list of dictionaries where each dictionary is a family member with their ID, name, and parent's ID.

2
Add a variable to select the parent to find children for
Create a variable called parent_id and set it to the string '1'. This will be used to find all children of the parent with PersonID '1'.
DynamoDB
Need a hint?

This variable will help you filter the family members who have this parent.

3
Write a query to find children of the selected parent
Create a list called children that contains all items from FamilyTree where the ParentID matches the parent_id variable.
DynamoDB
Need a hint?

Use a list comprehension to filter family members by ParentID.

4
Add a final attribute to mark the root ancestor
Add a new key called IsRoot to each item in FamilyTree. Set it to true if ParentID is null, otherwise false.
DynamoDB
Need a hint?

Loop through each family member and add the IsRoot key based on whether ParentID is null.