[Python / DynamoDB / boto3] List of operations I tried


** Minimize the number of lines and characters as much as possible **, and cover (want to) how to operate DynamoDB from Python

I will write from what I actually tried (\ * ˙︶˙ \ *) و Good!

Operation list

Preparations common to all operations

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

# region_For name, write the name of your region
dynamodb = boto3.resource('dynamodb', region_name='us-east-2')

1. create_table () ... create table

Create table

table_name = 'hoge_table'

table = dynamodb.create_table(
    TableName = table_name,
    KeySchema = [
        {'AttributeName': 'primary_key_name', 'KeyType': 'HASH'},
        {'AttributeName': 'column_name', 'KeyType': 'RANGE'}
    AttributeDefinitions = [
        {'AttributeName': 'primary_key_name', 'AttributeType': 'N'},
        {'AttributeName': 'column_name', 'AttributeType': 'S'}
    ProvisionedThroughput = {
        'ReadCapacityUnits': 1, 'WriteCapacityUnits': 1

2. list_tables () ... Get table list

Get table list

# region_For name, write the name of your region
client = boto3.client('dynamodb', region_name='us-east-2')


3. get_item () ... Get 1

Get 1 record

table_name = 'hoge_table'
table = dynamodb.Table(table_name)

    Key={'primary_key_name': 1, 'column_name': 'string_value'}

4. scan () ... Filtering after fetching all

After getting all items, filter by Attribute It is said that it consumes extra resources compared to query () (AWS usage fee is extra)

Filtering after getting all

table_name = 'hoge_table'
table = dynamodb.Table(table_name)

    Limit=1 #You can specify the number of cases. There is no need to have one separately.

5. query () ... Conditional search

Get only records that meet the conditions

filtered search

table_name = 'hoge_table'
table = dynamodb.Table(table_name)

#Column name is appropriate
    KeyConditionExpression=Key('column_name1').eq('string_value') & Key('column_name2').between(from_str, to_str)

(I want to start a new line at some point, but I'm angry with the Lint tool ...)

6. batch_writer () ... insert all at once

table_name = 'hoge_table'
table = dynamodb.Table(table_name)
items_source = [
    {'primary_key_name': 1, 'column_name': 'hoge1'},
    {'primary_key_name': 2, 'column_name': 'hoge2'},
    {'primary_key_name': 9, 'column_name': 'hoge9'}

# `overwrite_by_pkeys`For the column specified in, if it is duplicated, the record will be overwritten.
#If this is not specified, an error will be thrown when the value is duplicated.
with table.batch_writer(overwrite_by_pkeys=['primary_key_name', 'column_name']) as batch:
    for item in items_source:

Referenced articles

-I tried to operate DynamoDB with Boto3 -Create table and register data in DynamoDB Local with Python (boto3)

Recommended Posts

[Python / DynamoDB / boto3] List of operations I tried
Summary of Python3 list operations
I tried to summarize the string operations of Python
I tried to create a list of prime numbers with python
I tried to get a list of AMI Names using Boto3
List of python modules
I tried hundreds of millions of SQLite with python
I tried Python> autopep8
[Python] Summary of S3 file operations with boto3
I tried Python> decorator
I tried to summarize how to use matplotlib of python
[OpenCV / Python] I tried image analysis of cells with OpenCV
[Python] I tried to get Json of squid ring 2
I tried using Python (3) instead of a scientific calculator
I tried "morphology conversion" of images with Python + OpenCV
I tried fp-growth with python
I tried scraping with Python
Summary of python file operations
I tried Python C extension
[Python] I tried using OpenPose
[Python] Copy of multidimensional list
I tried gRPC with Python
I tried scraping with python
I tried to find the entropy of the image with python
I tried "gamma correction" of the image with Python + OpenCV
I tried the accuracy of three Stirling's approximations in python
I tried LeetCode every day 141. Linked List Cycle (Python, Go)
I tried running Movidius NCS with python of Raspberry Pi3
Since Python 1.5 of Discord, I can't get a list of members
I tried adding post-increment to CPython. List of all changes
[Python] I tried to visualize the follow relationship of Twitter
[Python] I tried collecting data using the API of wikipedia
I tried a stochastic simulation of a bingo game with Python
I tried to implement blackjack of card game in Python
[Python3] List of sites that I referred to when I started Python
I tried to touch Python (installation)
I tried web scraping with python.
I tried using GrabCut of OpenCV
I tried using Thonny (Python / IDE)
I tried Grumpy (Go running Python).
I tried running prolog with python 3.8.2.
I tried Line notification in Python
I tried SMTP communication with Python
About the basics list of Python basics
[Python] I tried using YOLO v3
I tried scraping the ranking of Qiita Advent Calendar with Python
I tried standalone deployment of play with fabric [AWS operation with boto] [Play deployment]
I tried to get the index of the list using the enumerate function
I tried to make a regular expression of "amount" using Python
I tried to make a regular expression of "time" using Python
I tried to make a regular expression of "date" using Python
I tried to fix "I tried stochastic simulation of bingo game with Python"
I tried to improve the efficiency of daily work with Python
I tried to automatically collect images of Kanna Hashimoto with Python! !!
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried the asynchronous server of Django 3.0
I tried to implement permutation in Python
Wrangle x Python book I tried it [2]
I tried LeetCode every day 111. Minimum Depth of Binary Tree (Python, Go)
Display a list of alphabets in Python 3