1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Python + boto3でDynamoDBのデータを取得する方法(scan編)

Last updated at Posted at 2025-10-05

AWS の DynamoDB に保存されたデータを Python から取得する方法をご紹介します。
今回は scan を使ったデータ取得 に絞って解説します。

前提条件

  • AWS CLI がインストール済み
  • Python3 系と boto3 がインストール済み

1. AWS CLI でプロファイルを設定する

まずは認証情報を~/.aws/config, ~/.aws/credentialsに登録します。

aws configure --profile my-profile

「my-profile」は自分の好きなプロファイル名に設定してください。

コマンド実施後:

AWS Access Key ID [None]: <アクセスキー>
AWS Secret Access Key [None]: <シークレットアクセスキー>
Default region name [None]: <リージョン名>
Default output format [None]: <出力フォーマット>

各情報を入力すると my-profile という名前で~/.aws/config, ~/.aws/credentialsに認証情報が保存されます。

アクセスキーとシークレットアクセスキーは、以下の記事などを参考に作成できます。

リージョン名は以下の方法で確認可能です。

  • AWS マネジメントコンソールで右上のリージョンドロップダウンに現在選択中のリージョン名が表示されます(例:ap-northeast-1

出力フォーマットはboto3に影響しないので、何も入力せずEnterで問題ないです。

2. boto3 でセッションを作成する

boto3 側で先ほどのプロファイルを使用します。

import boto3

# プロファイルを指定し、セッションを取得
session = boto3.Session(profile_name="my-profile")

# DynamoDB リソースを取得
dynamodb = session.resource("dynamodb")

# テーブル指定
table = dynamodb.Table("取得したいテーブル名")

3. scan でデータを取得する

DynamoDB の scan を使ってデータを取り出します。
例えば「Subject 属性が math のアイテム」を取得する場合、

from boto3.dynamodb.conditions import Attr

response = table.scan(
    FilterExpression=Attr("Subject").eq("math")
)

items = response.get("Items", [])

# ページング処理(全件取得する場合)
while "LastEvaluatedKey" in response:
    response = table.scan(
        FilterExpression=Attr("Subject").eq("math"),
        ExclusiveStartKey=response["LastEvaluatedKey"]
    )
    items.extend(response.get("Items", []))

# 結果を出力
for item in items:
    print(item)

4. scan で使える条件一覧

Attr を使うと、DynamoDB の属性に対してフィルタ条件を指定できます。
代表的な条件を表にまとめてみました。

条件 サンプルコード 説明
eq Attr("age").eq(30) age が 30 のアイテム
ne Attr("status").ne("done") status が done 以外
lt Attr("score").lt(50) score が 50 未満
lte Attr("score").lte(80) score が 80 以下
gt Attr("price").gt(1000) price が 1000 より大きい
gte Attr("price").gte(5000) price が 5000 以上
between Attr("date").between("2025-01-01", "2025-01-31") 期間内のデータ
begins_with Attr("name").begins_with("A") name が "A" で始まる
contains Attr("tags").contains("urgent") tags に "urgent" が含まれる
is_in Attr("type").is_in(["A", "B", "C"]) type が A, B, C のいずれか

参考:DynamoDB customization reference

5. 注意点

  • scan はテーブル全体を読み込むため、コストが高く速度も遅い
  • 小規模テーブルや簡単なフィルタには便利だが、大規模テーブルでは query を検討する必要あり
  • 大量データを扱う場合は必ずページング処理を入れる 必要あり

まとめ

  • scan を使うと DynamoDB の全件検索ができる
  • boto3 では Attr を使ってフィルタ条件を指定可能
  • scan は便利だけど非効率なので、 query の検討も必要

次回は query を使って パーティションキーで効率的にデータを取得する方法 を解説予定です!

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?