dynamo-db
  1. dynamo-db-read-operation

DynamoDB Read Operation

Syntax

response = table.get_item(
    Key={
        'partition_key': 'partition_key_value',
        'sort_key': 'sort_key_value'
    }
)

Example

response = table.get_item(
    Key={
        'id': '123',
        'timestamp': '2021-06-01T00:00:00'
    }
)

Output

The response variable will contain the item that matches the specified partition key and sort key.

Explanation

The get_item operation is used to retrieve an item from a table by its primary key. The primary key is composed of a partition key and an optional sort key. In the example above, the id attribute is the partition key and the timestamp attribute is the sort key.

Use

Use the get_item operation when you need to retrieve a specific item from a table based on its primary key.

Important Points

  • The Key parameter must be a dictionary that contains the partition key and sort key values.
  • If the item does not exist, the response will be an empty dictionary.
  • If the table has a composite primary key (partition key and sort key), both keys are required to retrieve the item.

Summary

The get_item operation is used to retrieve a specific item from a DynamoDB table based on its primary key. The response will contain the item that matched the specified key. Use this operation when you need to retrieve a single item from a table.

Published on: