DEV Community

Cover image for Inserting test data into DynamoDB local
Bira
Bira

Posted on

Inserting test data into DynamoDB local

DynamoDB is a fully managed NoSQL database service that offers high performance, scalability, and durability. It is a popular choice for storing large amounts of data that need to be accessed quickly.

One way to insert data into DynamoDB is to use a CSV file. This can be a convenient way to load data into DynamoDB, especially if you have a large amount of data to load.

In this blog post, we will show you how to insert test data into DynamoDB local using Python and CSV.

Prerequisites

Before you begin, you will need the following:

A Python development environment
The boto3 library
A CSV file with your test data
Enter fullscreen mode Exit fullscreen mode

Step 1: Create a DynamoDB Local instance

To start, you need to create a DynamoDB Local instance. This can be done by following the instructions in the DynamoDB Local documentation: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html.

Step 2: Create a DynamoDB table

Once you have created a DynamoDB Local instance, you need to create a DynamoDB table. This can be done by using the DynamoDb Admin (https://www.npmjs.com/package/dynamodb-admin) or NoSQL Workbench (https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/workbench.html)

Step 3: Generate Data using Excel.

You can easily regenerate requisite data from excel or Google Sheets and download as CSV files and then import into your DynamoDB

Creating dummy data using excel

Step 4: Load the CSV file into DynamoDB

Now that you have created a DynamoDB table, you can load the CSV file into DynamoDB. The following code loads the CSV file test_data.csv into the test_table table:

import boto3
import csv

# Create a DynamoDB client
dynamodb = boto3.resource('dynamodb', endpoint_url='http://localhost:8000')

# Open the CSV file
with open('./csv/user.csv', 'r') as csvfile:

    # Create a reader object
    reader = csv.reader(csvfile, delimiter=',')

    # Get the header
    header = next(reader, None)

    # Iterate over the rows in the CSV file
    for row in reader:

        # Create a dictionary to represent the item
        item = {}
        for i, column in enumerate(header):
            item[column] = row[i]
            print(column, row[i])

        # Put the item in the DynamoDB table
        dynamodb.Table('test_table').put_item(Item=item)
Enter fullscreen mode Exit fullscreen mode

Step 5: Verify the data in DynamoDB

Once you have loaded the CSV file into DynamoDB, you can verify the data by querying the test_table table using the tools mentioned in the step 2.

Viewing from DynamoDB Admin

Conclusion

In this blog post, we showed you how to insert test data into DynamoDB local using Python and CSV. We hope this helps you get started with DynamoDB.

For more information on DynamoDB, please visit the DynamoDB documentation: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/.

Top comments (0)