What you'll learn
- Choose a partition key that is unique and frequently queried.
- Only key attributes have a declared type: every item's
Namemust be a String. Other attributes are schemaless. - Key values are case-sensitive: "Bulbasaur" and "bulbasaur" are two different items.
Section 01
Scenario
We'll store each Pokémon by its unique Name as the Partition Key.
Section 02
Table Definition
Define a DynamoDB table with Name as the partition key.
const table = new dynamodb.Table(this, "PokemonTable", {
tableName: "Pokemon",
partitionKey: { name: "Name", type: dynamodb.AttributeType.STRING },
});Section 03
Insert Item Example
With the document client you pass plain JavaScript values in Item. PutItem is a full replace: writing the same Name again overwrites the whole item (Quest 5 shows how to guard against that).
{
TableName: "Pokemon",
Item: {
Name: "Bulbasaur",
Type1: "grass",
Type2: "poison",
Height_m: 0.7,
Weight_kg: 6.9
}
}Section 04
Get Item Example
Retrieve the Pokémon by supplying the same partition key value:
{
TableName: "Pokemon",
Key: { Name: "Bulbasaur" }
}Ready · 10 steps · Amp
This animation plays when you scroll to it. Press Play to start it now.
All steps as text
- Your app sends PutItem to the Pokemon table with Name = "Pikachu".
- DynamoDB hashes "Pikachu" to a6bb8b2f, which routes the request to partition P3.
- P3 stores Pikachu (Type Electric, Level 5).
- This call consumed 1 WCU.
- DynamoDB responds with success: Pikachu stored under its Name.
- Your app sends GetItem to the Pokemon table with Name = "Pikachu".
- DynamoDB hashes "Pikachu" to a6bb8b2f, which routes the request to partition P3.
- P3 returns Pikachu.
- This call consumed 0.5 RCU.
- DynamoDB responds with success: one key, one partition, one item.
Check
Check your understanding
Quick Quiz · one question, as many tries as you need.
Which attribute is used as the partition key here?
Choose one answer.