Try using Amazon DynamoDB from Python

0. Introduction

Let's try various services recommended by AWS serverless. This time, let's use Amazon DynamoDB from Python.

1. 1. Build a Python environment on your Mac

There is no environment in the first place !! So, it is from the environment construction.

Build the environment by referring to the following (or as it is ...).

If you do not set .bash_profile, it seems that you have to hit the command to switch the Python version every time, so set this as well.

2. Create a script that hits Amazon DynamoDB from the Python environment on Mac

Create a Python script by referring to the following.

Since I'm from a local Mac, IAM Role isn't available, and I wanted to complete it all with a single script, so I'll use this authentication method.

python


import boto3
from boto3.session import Session

accesskey = "YOUR_ACCESSKEY"
secretkey = "YOUR_SECRETKEY"
region    = "YOUR_REGION"

session = Session(
                  aws_access_key_id=accesskey,
                  aws_secret_access_key=secretkey,
                  region_name=region)

3. 3. Follow the Amazon DynamoDB Getting Started Guide in AWS Documentation to see how you work with DynamoDB

Confirm the operation as AWS says.

  1. Step 1: Create a table-Amazon DynamoDB
  2. Step 2: Load sample data-Amazon DynamoDB
  3. Step 3: Create, load, update, delete items --Amazon DynamoDB
  4. Step 4: Query and Scan Data-Amazon DynamoDB
  5. Step 5: (Optional) Drop the table --Amazon DynamoDB

Below is a sample. I confirmed it by commenting on the call to the main function.

To be honest, I'm not used to Python, so I'm not sure if this is the way to write it ...

SamplaDynamoDB.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

# ---1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----
# ==============================================================================
#
# SampleDynamoDB.py
#
#   * ex) $ ./SamplaDynamoDB.py
#
# ==============================================================================

import sys
import io
import logging
import json
import decimal
import time

import datetime
from datetime import datetime as dt

import boto3
from boto3.session import Session
from boto3.dynamodb.conditions import Key, Attr

# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            if o % 1 > 0:
                return float(o)
            else:
                return int(o)
        return super(DecimalEncoder, self).default(o)

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
logging.basicConfig(level=logging.INFO, filename=(__file__ + ".log"), format="%(asctime)s %(levelname)s %(filename)s %(lineno)d %(funcName)s | %(message)s")

# ------------------------------------------------------------------------------
# Set
# ------------------------------------------------------------------------------
tmp_today = datetime.datetime.today()

# AWS
accesskey = "[Access key ID]"
secretkey = "[Secret access key]"
region = "ap-northeast-1"
session = Session(aws_access_key_id=accesskey, aws_secret_access_key=secretkey, region_name=region)
dynamodb = session.resource('dynamodb')

# ------------------------------------------------------------------------------
#"step 1:Create a table- Amazon DynamoDB」
# <http://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/gettingstartedguide/GettingStarted.Python.01.html>
# ------------------------------------------------------------------------------
def MoviesCreateTable():
    logging.info("<<<<<<<< %s Start >>>>>>>>", __name__)
    try:
        table = dynamodb.create_table(
            TableName='Movies',
            KeySchema=[
                {
                    'AttributeName': 'year',
                    'KeyType': 'HASH'  #Partition key
                },
                {
                    'AttributeName': 'title',
                    'KeyType': 'RANGE'  #Sort key
                }
            ],
            AttributeDefinitions=[
                {
                    'AttributeName': 'year',
                    'AttributeType': 'N'
                },
                {
                    'AttributeName': 'title',
                    'AttributeType': 'S'
                },
            ],
            ProvisionedThroughput={
                'ReadCapacityUnits': 10,
                'WriteCapacityUnits': 10
            }
        )
        logging.info("Table status : [%s]", table.table_status)
    except Exception as e:
        logging.error("Type : %s", type(e))
        logging.error(e)
    logging.info("<<<<<<<< %s End >>>>>>>>", __name__)

# ------------------------------------------------------------------------------
#"Step 2:Load sample data- Amazon DynamoDB」
# <http://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/gettingstartedguide/GettingStarted.Python.02.html>
# ------------------------------------------------------------------------------
def MoviesLoadData():
    logging.info("<<<<<<<< %s Start >>>>>>>>", __name__)
    try:
        with open("moviedata.json") as json_file:
            movies = json.load(json_file, parse_float = decimal.Decimal)
            for movie in movies:
                year = int(movie['year'])
                title = movie['title']
                info = movie['info']
                #logging.info("Adding Movie | Year:[%s], Title:[%s]", year, title)
                table.put_item(
                   Item={
                       'year': year,
                       'title': title,
                       'info': info,
                    }
                )
    except Exception as e:
        logging.error("Type : %s", type(e))
        logging.error(e)
    logging.info("<<<<<<<< %s End >>>>>>>>", __name__)

# ------------------------------------------------------------------------------
#"Step 3:Create, load, update, delete items- Amazon DynamoDB」
# <http://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/gettingstartedguide/GettingStarted.Python.03.html>
# ------------------------------------------------------------------------------
def MoviesItemOps01():
    logging.info("<<<<<<<< %s Start >>>>>>>>", __name__)
    try:
        table = dynamodb.Table('Movies')
        title = "The Big New Movie"
        year = 2015
        response = table.put_item(
           Item={
                'year': year,
                'title': title,
                'info': {
                    'plot':"Nothing happens at all.",
                    'rating': decimal.Decimal(0)
                }
            }
        )
        logging.info("PutItem succeeded:")
        logging.info(json.dumps(response, indent=4, cls=DecimalEncoder))
    except Exception as e:
        logging.error("type : %s", type(e))
        logging.error(e)
    logging.info("<<<<<<<< %s End >>>>>>>>", __name__)

def MoviesItemOps02():
    logging.info("<<<<<<<< %s Start >>>>>>>>", __name__)
    try:
        table = dynamodb.Table('Movies')
        title = "The Big New Movie"
        year = 2015
        try:
            response = table.get_item(
                Key={
                    'year': year,
                    'title': title
                }
            )
        except ClientError as e:
            logging.info(e.response['Error']['Message'])
        else:
            item = response['Item']
            logging.info("GetItem succeeded:")
            logging.info(json.dumps(item, indent=4, cls=DecimalEncoder))
    except Exception as e:
        logging.error("type : %s", type(e))
        logging.error(e)
    logging.info("<<<<<<<< %s End >>>>>>>>", __name__)

def MoviesItemOps03():
    logging.info("<<<<<<<< %s Start >>>>>>>>", __name__)
    try:
        table = dynamodb.Table('Movies')
        title = "The Big New Movie"
        year = 2015
        response = table.update_item(
            Key={
                'year': year,
                'title': title
            },
            UpdateExpression="set info.rating = :r, info.plot=:p, info.actors=:a",
            ExpressionAttributeValues={
                ':r': decimal.Decimal(5.5),
                ':p': "Everything happens all at once.",
                ':a': ["Larry", "Moe", "Curly"]
            },
            ReturnValues="UPDATED_NEW"
        )
        logging.info("UpdateItem succeeded:")
        logging.info(json.dumps(response, indent=4, cls=DecimalEncoder))
    except Exception as e:
        logging.error("type : %s", type(e))
        logging.error(e)
    logging.info("<<<<<<<< %s End >>>>>>>>", __name__)

def MoviesItemOps04():
    logging.info("<<<<<<<< %s Start >>>>>>>>", __name__)
    try:
        table = dynamodb.Table('Movies')
        title = "The Big New Movie"
        year = 2015
        response = table.update_item(
            Key={
                'year': year,
                'title': title
            },
            UpdateExpression="set info.rating = info.rating + :val",
            ExpressionAttributeValues={
                ':val': decimal.Decimal(1)
            },
            ReturnValues="UPDATED_NEW"
        )
        logging.info("UpdateItem succeeded:")
        logging.info(json.dumps(response, indent=4, cls=DecimalEncoder))
    except Exception as e:
        logging.error("type : %s", type(e))
        logging.error(e)
    logging.info("<<<<<<<< %s End >>>>>>>>", __name__)

def MoviesItemOps05():
    logging.info("<<<<<<<< %s Start >>>>>>>>", __name__)
    try:
        table = dynamodb.Table('Movies')
        title = "The Big New Movie"
        year = 2015
        logging.info("Attempting conditional update...")
        try:
            response = table.update_item(
                Key={
                    'year': year,
                    'title': title
                },
                UpdateExpression="remove info.actors[0]",
                ConditionExpression="size(info.actors) > :num",
                ExpressionAttributeValues={
                    ':num': 2
                },
                ReturnValues="UPDATED_NEW"
            )
        except ClientError as e:
            if e.response['Error']['Code'] == "ConditionalCheckFailedException":
                logging.error(e.response['Error']['Message'])
            else:
                raise
        else:
            logging.info("UpdateItem succeeded:")
            logging.info(json.dumps(response, indent=4, cls=DecimalEncoder))
    except Exception as e:
        logging.error("type : %s", type(e))
        logging.error(e)
    logging.info("<<<<<<<< %s End >>>>>>>>", __name__)

def MoviesItemOps06():
    logging.info("<<<<<<<< %s Start >>>>>>>>", __name__)
    try:
        table = dynamodb.Table('Movies')
        title = "The Big New Movie"
        year = 2015
        logging.info("Attempting a conditional delete...")
        try:
            response = table.delete_item(
                Key={
                    'year': year,
                    'title': title
                },
                ConditionExpression="info.rating <= :val",
                ExpressionAttributeValues= {
                    ":val": decimal.Decimal(8)
                }
            )
        except ClientError as e:
            if e.response['Error']['Code'] == "ConditionalCheckFailedException":
                logging.info(e.response['Error']['Message'])
            else:
                raise
        else:
            logging.info("DeleteItem succeeded:")
            logging.info(json.dumps(response, indent=4, cls=DecimalEncoder))
    except Exception as e:
        logging.error("type : %s", type(e))
        logging.error(e)
    logging.info("<<<<<<<< %s End >>>>>>>>", __name__)

# ------------------------------------------------------------------------------
#"Step 4:Query and scan data- Amazon DynamoDB」
# <http://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/gettingstartedguide/GettingStarted.Python.04.html>
# ------------------------------------------------------------------------------
def MoviesQuery01():
    logging.info("<<<<<<<< %s Start >>>>>>>>", __name__)
    try:
        table = dynamodb.Table('Movies')
        logging.info("Movies from 1933")
        response = table.query(
            KeyConditionExpression=Key('year').eq(1933)
        )
        logging.info("Query01 succeeded:")
        logging.info(json.dumps(response, indent=4, cls=DecimalEncoder))
        for i in response['Items']:
            logging.info("%s : %s", i['year'], i['title'])
    except Exception as e:
        logging.error("Type : %s", type(e))
        logging.error(e)
    logging.info("<<<<<<<< %s End >>>>>>>>", __name__)

def MoviesQuery02():
    logging.info("<<<<<<<< %s Start >>>>>>>>", __name__)
    try:
        table = dynamodb.Table('Movies')
        logging.info("Movies from 1992 - titles A-L, with genres and lead actor")
        response = table.query(
            ProjectionExpression="#yr, title, info.genres, info.actors[0]",
            ExpressionAttributeNames={ "#yr": "year" }, # Expression Attribute Names for Projection Expression only.
            KeyConditionExpression=Key('year').eq(1992) & Key('title').between('A', 'L')
        )
        logging.info("Query02 succeeded:")
        logging.info(json.dumps(response, indent=4, cls=DecimalEncoder))
        for i in response[u'Items']:
            logging.info(json.dumps(i, cls=DecimalEncoder))
    except Exception as e:
        logging.error("Type : %s", type(e))
        logging.error(e)
    logging.info("<<<<<<<< %s End >>>>>>>>", __name__)

# ------------------------------------------------------------------------------
#"Step 5: (option)Drop the table- Amazon DynamoDB」
# <http://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/gettingstartedguide/GettingStarted.Python.05.html>
# ------------------------------------------------------------------------------
def MoviesDeleteTable():
    logging.info("<<<<<<<< %s Start >>>>>>>>", __name__)
    try:
        table = dynamodb.Table('Movies')
        table.delete()
    except Exception as e:
        logging.error("Type : %s", type(e))
        logging.error(e)
    logging.info("<<<<<<<< %s End >>>>>>>>", __name__)

# ------------------------------------------------------------------------------
# Main
# ------------------------------------------------------------------------------
if __name__ == '__main__':
    logging.info("<<<<<<<< %s Start >>>>>>>>", __name__)

    # Set
    logging.info("REGION : [%s]", region)

    # Args
    logging.info("Argc : [%d]", len(sys.argv))
    for i in range(len(sys.argv)):
        logging.info("Argv[%d] : [%s]", i, sys.argv[i])

    # Check Table
    table = dynamodb.Table('Movies')
    logging.info("Table :")
    logging.info(table)

    # Create Table
    #MoviesCreateTable()
    #time.sleep(9)

    # Load Json Data
    #MoviesLoadData()

    # Query Data
    MoviesItemOps01()
    MoviesItemOps02()
    MoviesItemOps03()
    MoviesItemOps04()
    #MoviesItemOps05()
    #MoviesItemOps06()

    # Query Data
    MoviesQuery01()
    MoviesQuery02()

    # Delete Table
    #MoviesDeleteTable()

    logging.info("<<<<<<<< %s End >>>>>>>>", __name__)

# ---1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----

99. Addictive point

XX. Summary

This time, I was grateful that all the sources were in the introductory guide.

Next, I thought I'd try launching Amazon Lambda.

Recommended Posts

Try using Amazon DynamoDB from Python
Try using Tweepy [Python2.7]
[Python] Try using Tkinter's canvas
Query from python to Amazon Athena (using named profile)
Try IAM Database Authentication from Python
Using Rstan from Python with PypeR
Operate DynamoDB from Python like SQL.
Try python
Try using LevelDB in Python (plyvel)
Using Cloud Storage from Python3 (Introduction)
Try using Python argparse's action API
Try using the Python Cmd module
Run Ansible from Python using API
Precautions when using phantomjs from python
Access spreadsheets using OAuth 2.0 from Python
Try using Leap Motion in Python
Try using the Wunderlist API in Python
From Python to using MeCab (and CaboCha)
Try mathematical formulas using Σ with python
Try using the Kraken API in Python
Try using Dialogflow (formerly API.AI) Python SDK #dialogflow
Try using Python with Google Cloud Functions
Try using Junos On-box Python # 2 Commit Script
I tried using UnityCloudBuild API from Python
Try to operate Excel using Python (Xlwings)
Try calling Python from Ruby with thrift
Try using Junos On-box Python # 1 Op Script
[Amazon Linux] Switching from Python 2 series to Python 3 series
Try using Tkinter
Try using docker-py
Try using PDFMiner
Start using Python
Try using geopandas
Try using Selenium
Try using scipy
Python> try: / except:
sql from python
Try using the Python web framework Django (1)-From installation to server startup
MeCab from Python
Try using django-swiftbrowser
Try using matplotlib
Try using tf.metrics
Try using PyODE
Scraping using Python
Try operating Studio Library from Python. [Anim Save]
Creating numbering process using python in DynamoDB Local Numbering process
I want to email from Gmail using Python.
Try using the BitFlyer Ligntning API in Python
Try using the Python web framework Tornado Part 1
Create wav file from GLSL shader using python3
Operate the schedule app using python from iphone
Try using the collections module (ChainMap) of python3
Try using tensorflow ① Build python environment and introduce tensorflow
Try using the Python web framework Tornado Part 2
Try accessing the YQL API directly from Python 3
Run a python script from excel (using xlwings)
Try using ChatWork API and Qiita API in Python
Try using the DropBox Core API in Python
Region extraction method using cellular automaton Try region extraction from the image with growcut (Python)
Try running Amazon Timestream
Use thingsspeak from python