90
81

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python で DynamoDBを使う

Last updated at Posted at 2015-10-21

Node.jsでLambda書いてたら非同期処理に心が折れたので、最近リリースされたLambda/Pythonにしようと思って、試してみたメモです。

ドキュメント

準備

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'
            }
        )

DynamoDBの制限だと 25件までだけど、25件ずつ送るのも面倒みてくれる様子。

list_tables

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

90
81
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
90
81

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?