Advertisement

How to connect to AWS DynamoDB with Python

阅读量:

1. Background

为了 efficiently insert massive amounts of data into the SQL server database, I faced a limitation as the Express SQL version has a data limit of just 10 GB. Consequently, I had to try AWS DynamoDB, a database system optimized for handling large-scale, unstructured data.

2. Prerequisites

  • A DynamoDB table exists on AWS that can be accessed via the website.
  • The variables aws_access_key_id and aws_secret_access_key are established in your AWS account.

3. Examples

boto3 is the package to connect to DynamoDB, like the following:

复制代码
    import boto3

Please specify the following three parameters: *** aws_access_key_id *, * aws_secret_access_key ***, and region_name, such as those listed below.

复制代码
 dynamodb = boto3.resource('dynamodb', aws_access_key_id='accessid',

    
      aws_secret_access_key= 'accesskey', region_name='us-east-1')

The region_name is from the AWS website. That should be easy to notice.

4. Connect to table and save data

To connect to a table, we need to run this:

复制代码
    table = dynamodb.Table('test')

To save data, JSON remains the sole acceptable format. It represents a dictionary data structure in Python. The code is:

复制代码
    table.put_item(Item={'key':'k','value':'value'})

After completing it, you may verify the table. And if the data appears, now is the time to begin.

全部评论 (0)

还没有任何评论哟~