How to get Item from DynamoDB table using Python

This post explains how to get items from DynamoDB tables using Python. Python Boto3 SDK provides APIs like get_item and batch_get_item to retrieve items from the table.


See Also

Prerequisite

You will need a DynamoDB table with some data in it in order to complete this tutorial. For instructions on creating and populating data in a DynamoDB table, see the pages below.


DynamoDB get_item()

The get_item method returns a set of attributes for the item with the given primary key. If there is no matching item, get_item doesn't return any data and there will no Item element is the response.

get_item using eventually consistent read

Let's have a look to below code snippet that retrieve an item using get_item.

   
  import boto3
  
  
  dynamodb_resource = boto3.resource("dynamodb")
  table_name = "sample-movie-table-resource"
  table = dynamodb_resource.Table(table_name)
  
  item_key = {"year":2013, "title": "The Great Gatsby"}
  
  def get_single_item(item_key):
      response = table.get_item(Key=item_key)
      return response
  
  if __name__ == "__main__":
      response = get_single_item(item_key=item_key)
      print(response["Item"])
       

In the above code snippet get_item operation fetches the item having key defined as item_key. If you noticed, in the response there is no information about the ConsumedCapacity, modify the code as shown below to get details about the ConsumedCapacity for this operation.

   
  import boto3
  
  
  dynamodb_resource = boto3.resource("dynamodb")
  table_name = "sample-movie-table-resource"
  table = dynamodb_resource.Table(table_name)
  
  item_key = {"year": 2013, "title": "The Great Gatsby"}
  
  
  def get_single_item(item_key):
      response = table.get_item(Key=item_key, ReturnConsumedCapacity="TOTAL")
      return response
  
  
  if __name__ == "__main__":
      response = get_single_item(item_key=item_key)
      print(response["ConsumedCapacity"])
      
     

Output

   
  {'TableName': 'sample-movie-table-resource', 'CapacityUnits': 0.5}
   

get_item using strongly consistent read

In the previous code snippets there was no parameter to determine read consistency model, so in this case operation always uses eventually consistent reads. To use the strongly consistent read, modify the code as shown below.

   
  import boto3
  
  
  dynamodb_resource = boto3.resource("dynamodb")
  table_name = "sample-movie-table-resource"
  table = dynamodb_resource.Table(table_name)
  
  item_key = {"year": 2013, "title": "The Great Gatsby"}
  
  
  def get_single_item(item_key):
      response = table.get_item(
          Key=item_key, ReturnConsumedCapacity="TOTAL", ConsistentRead=True
      )
      return response
  
  
  if __name__ == "__main__":
      response = get_single_item(item_key=item_key)
      print(response["ConsumedCapacity"])
     

Output

   
  {'TableName': 'sample-movie-table-resource', 'CapacityUnits': 1.0}
   

get_item eventually vs strongly consistent reads

As we can see from above examples strongly consistent read consumed 1.0 CapacityUnits while eventually read consumed 0.5 CapacityUnits.


DynamoDB batch_get_item()

The batch_get_item method returns one or more items from one or more tables. Let's have a look to below code snippet for using batch_get_item method to retrieve items in parallel which minimizes the response latency.

batch_get_item using eventually consistent read

   
  import boto3
  
  
  dynamodb_resource = boto3.resource("dynamodb")
  table_name = "sample-movie-table-resource"
  table = dynamodb_resource.Table(table_name)
  
  batch_keys = {
      table_name:{
          "Keys":[
              {"year": 2013, "title": "The Great Gatsby"},
              {"year": 2013, "title": "Now You See Me"},
              {"year": 2014, "title": "Divergent"},
              {"year": 2014, "title": "X-Men: Days of Future Past"},
              {"year": 2013, "title": "Iron Man 3"},
              {"year": 2013, "title": "Thor: The Dark World"},
              {"year": 2013, "title": "Star Trek Into Darkness"},
  
          ]
      }
  } 
  
  
  def get_batch_items(batch_keys):
      response = dynamodb_resource.batch_get_item(
          RequestItems=batch_keys,
          ReturnConsumedCapacity='TOTAL'
      )
      return response
  
  
  if __name__ == "__main__":
      response = get_batch_items(batch_keys=batch_keys)
      print(response)
      # processed items
      print(response["Responses"][table_name])
      # processed item count
      print(len(response["Responses"][table_name]))
      # consumed CapacityUnits
      print(response["ConsumedCapacity"])
    
   

batch_get_item using strongly consistent read

   
  import boto3
  
  
  dynamodb_resource = boto3.resource("dynamodb")
  table_name = "sample-movie-table-resource"
  table = dynamodb_resource.Table(table_name)
  
  batch_keys = {
      table_name: {
          "Keys": [
              {"year": 2013, "title": "The Great Gatsby"},
              {"year": 2013, "title": "Now You See Me"},
              {"year": 2014, "title": "Divergent"},
              {"year": 2014, "title": "X-Men: Days of Future Past"},
              {"year": 2013, "title": "Iron Man 3"},
              {"year": 2013, "title": "Thor: The Dark World"},
              {"year": 2013, "title": "Star Trek Into Darkness"},
          ],
          "ConsistentRead": True,
      },
  }
  
  
  def get_batch_items(batch_keys):
      response = dynamodb_resource.batch_get_item(
          RequestItems=batch_keys, ReturnConsumedCapacity="TOTAL"
      )
      return response
  
  
  if __name__ == "__main__":
      response = get_batch_items(batch_keys=batch_keys)
      print(response)
      # processed items
      print(response["Responses"][table_name])
      # processed item count
      print(len(response["Responses"][table_name]))
      # consumed CapacityUnits
      print(response["ConsumedCapacity"])
      
     

batch_get_item eventually vs strongly consistent reads

From the output of above snippets we can see batch_get_item eventually consistent read consumed 3.5 (0.5 * 7 items) CapacityUnits which is sum of CapacityUnits consumed by single item in get_item operation and similarly batch_get_item strongly consistent read consumed 7.0 (1.0 * 7 Items) CapacityUnits which is sum of CapacityUnits consumed by single item in get_item operation for strongly consistent read.


Category: Python