Boto 3の勉強 その2 (DynamoDB)

今日は、先日作成したDynamoDBのテーブルにアイテムを追加してみる。

テーブルは作成済みなので、既存のテーブルを変数として指定し、put_itemでアイテムを作成する。

table = dynamodb.Table("users")

table.put_item(
    Item={
        "username": "janedoe",
        "first_name": "Jane",
        "last_name": "Doe",
        "age": 25,
        "account_type": "standard_user",
    }
)

また、作成したアイテムをget_itemでのぞいてみる。
辞書型で取得できるので、アイテムItemを指定する。

response = table.get_item(Key={"username": "janedoe", "last_name": "Doe"})
item = response["Item"]
print(item)

実行結果

% python sample-dynamodb.py
{'username': 'janedoe', 'account_type': 'standard_user', 'last_name': 'Doe', 'first_name': 'Jane', 'age': Decimal('25')}