DynamoDB Export and Import Python

Amazon Web Services (AWS) offers a fully organized NoSQL database service. It offers a quick, dependable, and highly scalable method for storing and retrieving data. Applications with high traffic and data volume that demand constant performance and minimal latency can be handled using DynamoDB.

DynamoDB uses a key-value data format and includes functionalities such as:

  • Automated partitioning

  • Scalability

  • Fault tolerance

Moreover, it offers a variety of customizable querying options, including secondary and global secondary indexes, which provide effective data searching using qualities other than the primary key.

Installing boto3

Data exporting and importing from DynamoDB is possible using the Amazon SDK for Python. Here are some examples of data import and export from DynamoDB using Python and boto3:

To set up boto3, we can utilize pip, a Python package manager.

Open a terminal window and run the provided command to install boto3 :

pip install boto3
The command for installing boto3

We can verify whether boto3 is installed or not by running the following command:

pip show boto3
Verifying boto3 installation

Export data

To export data to DynamoDB in Python using boto3 we have to follow the following steps:

  • Log in to the AWS console using AWS credentials.

  • Create a table by defining attribute definitions and key schema.

  • In this example, we are reading data from a JSON file, so first, we will read data from a JSON file and then push that data into DynamoDB using the put_item() function.

Code example

Take a look at the following code for exporting data from DynamoDB:

main.py
data.json
import boto3
from botocore.exceptions import ClientError
import json
import os
def Create_Table(session,dynamodb):
# Create the DynamoDB resource
dynamodb_resource = session.resource('dynamodb')
# Define the table schema
table_name = 'Students'
attribute_definitions = [
{
'AttributeName': 'id',
'AttributeType': 'S'
}
]
key_schema = [
{
'AttributeName': 'id',
'KeyType': 'HASH'
}
]
table = dynamodb.create_table(
TableName=table_name,
KeySchema=key_schema,
AttributeDefinitions=attribute_definitions,
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)
# Wait for the table to be created
table_resource = dynamodb_resource.Table(table_name)
table_resource.wait_until_exists()
# Print the table details
print("Table created:", table_resource.table_name)
def Insert_Data(dynamodb,table_name):
#inserting data in students table
with open('data.json') as file:
data = json.load(file)
# Iterate over each record in the data
for record in data:
# Create the student record
student_data = {
'id': {'S': record['id']},
'name': {'S': record['name']},
'age': {'N': str(record['age'])},
'email': {'S': record['email']}
}
# Add the student record to the table
response = dynamodb.put_item(
TableName=table_name,
Item=student_data
)
print('Data added to the "students" table.')
def main():
session = boto3.Session(
aws_access_key_id= os.getenv("Access_Key"),
aws_secret_access_key=os.getenv("Secret_Access_Key"),
region_name=os.getenv("Region")
)
dynamodb = session.client('dynamodb')
print(" You are login")
try:
Create_Table(session,dynamodb)
except ClientError as e:
if e.response['Error']['Code'] == 'ResourceInUseException':
print("Table already exists")
Insert_Data(dynamodb,"Students")
main()

Explanation

Here is the explanation for the above code:

  • Lines 1–4: We import the modules and libraries required.

  • Lines 11–32: We define the table schema and list of attribute definitions for the table.

  • Lines 34–38: We create a table resource for the created table and wait until the table is created using the wait_until_exists() function.

  • Lines 45–46: We load data from the data.json file using the json.load() function.

  • Lines 49–56: We iterate over each record in the data list and create a student_data dictionary to store attribute-value pairs for each student record.

  • Lines 59–62: We add the student_data to the table using the dynamodb.put_item() method.

Note: Each item in the items list should be in the format expected by DynamoDB (i.e., a dictionary with key-value pairs representing the item attributes).

Import data

To import data from the DynamoDB table in Python, we have to follow the following steps:

  • Log in to the AWS console using AWS credentials.

  • Then, provide a table name and retrieve data from that table.

Example

Here is the code example to import data from DynamoDB using Python:

import boto3
import json
import os
def import_data(session,dynamodb,table_name):
dynamodb_resource = session.resource('dynamodb')
table = dynamodb_resource.Table(table_name)
# Scan the table to retrieve all items
response = table.scan()
items = response['Items']
# Process the items or perform desired operations
for item in items:
# Access the attributes of each item
id = item['id']
name = item['name']
age = item['age']
email = item['email']
#Printing the data
print(f"ID: {id}, Name: {name}, Age: {age}, Email: {email}")
# defining main
def main():
session = boto3.Session(
aws_access_key_id= os.getenv("Access_Key"),
aws_secret_access_key=os.getenv("Secret_Access_Key"),
region_name=os.getenv("Region")
)
dynamodb = session.client('dynamodb')
print(" You are login")
table_name = 'Students'
try:
import_data(session,dynamodb,table_name)
except:
print("Table not exists")
main()

Explanation

Here is the explanation of the above code:

  • Line 1–3: We import the modules.

  • Line 7–8: We create a DynamoDB resource object using the resource() method of the session object and retrieve the DynamoDB table specified by table_name using the Table method of the DynamoDB resource object.

  • Line 10–11: We scan the table using the scan() method of the table object. This scans the entire table and returns a response object.

  • Line 13–20: We iterate over each item in the items list and access the attributes of each item by using the corresponding keys. The values are assigned to variables id, name, age, and email.

  • Line 23–35: We define a main() function in which we perform login functionality and then call the import_data() function.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved