Use DynamoDB with Python

When I wrote Lambda in Node.js, I was struck by asynchronous processing, so I decided to use the recently released Lambda / Python and tried it.

document

http://boto3.readthedocs.org/en/latest/guide/dynamodb.html

Preparation

import boto3
import json
from boto3.dynamodb.conditions import Key, Attr


dynamodb = boto3.resource('dynamodb')
table    = dynamodb.Table('MY_TABLE_NAME')

query


res = table.query(
        IndexName='MY_INDEX_NAME',
        KeyConditionExpression=Key('MY_INDEX_NAME').eq(MY_INDEX_VALUE)
    )
for row in res['Items']:
    print(row)

delete_item

table.delete_item(Key={'key1': key1, 'key2': key2})

put_item

table.put_item(
    Item={
        "key1": value1,
        "key2": value2
   }
)

get_item

items = table.get_item(
            Key={
                 "key1": key1
            }
        )

batch_write

with table.batch_writer() as batch:
    for i in range(50):
        batch.put_item(
            Item={
                'account_type': 'anonymous',
                'username': 'user' + str(i),
                'first_name': 'unknown',
                'last_name': 'unknown'
            }
        )

http://boto3.readthedocs.org/en/latest/guide/dynamodb.html#batch-writing

The limit of DynamoDB is up to 25, but it seems that sending 25 each is troublesome.

list_tables

dynamodb = boto3.resource('dynamodb')
table_list = dynamodb.tables.all()
for table in table_list:
    print(table.table_name)

Recommended Posts

Use DynamoDB with Python
[Python] Use JSON with Python
Use mecab with Python3
Use Python 3.8 with Anaconda
Use python with docker
Use Trello API with python
Use Twitter API with Python
Use subsonic API with python3
Python: How to use async with
Use PointGrey camera with Python (PyCapture2)
Use vl53l0x with Raspberry Pi (python)
[Python] Use Basic/Digest authentication with Flask
Use rospy with virtualenv in Python3
Use Python in pyenv with NeoVim
How to use FTP with Python
Use Windows 10 speech synthesis with Python
Use OpenCV with Python 3 in Window
Use PostgreSQL with Lambda (Python + psycopg2)
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Use smbus with python3 under pyenv environment
Use DeepL with python (for dissertation translation)
Use Amazon Simple Notification Service with Python
[Python] Use string data with scikit-learn SVM
[Introduction to Python] Let's use foreach with Python
Use PIL and Pillow with Cygwin Python
Use cryptography library cryptography with Docker Python image
Manipulate DynamoDB data with Lambda (Node & Python)
Use Application Insights with Python 3 (including bottles)
Use C ++ functions from python with pybind11
Use selenium phantomjs webdriver with python unittest
Until you can use opencv with python
Use Python and MeCab with Azure Functions
Serial communication with Python
Zip, unzip with python
Primality test with Python
Python with eclipse + PyDev.
Use thingsspeak from python
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Use mecab-ipadic-neologd with igo-python
Try scraping with Python.
Use RTX 3090 with PyTorch
Use config.ini in Python
Sequential search with Python
"Object-oriented" learning with python