0
0

More than 1 year has passed since last update.

Python3: DynamoDB の scan

Last updated at Posted at 2022-07-22

次に記述してある制約条件を回避する方法です。
scan(**kwargs)

If the total number of scanned items exceeds
the maximum dataset size limit of 1 MB,
the scan stops 
scan_id.py
#! /usr/bin/python
#
#	scan_id.py
#
#					Jul/22/2022
# --------------------------------------------------------------------
import	sys
import  json
import	boto3

# --------------------------------------------------------------------
def scan_all(table, scan_kwargs):
    items = []
    done = False
    start_key = None
    while not done:
        if start_key:
            scan_kwargs['ExclusiveStartKey'] = start_key
        response = table.scan(**scan_kwargs)
        items.extend(response.get('Items', []))
        start_key = response.get('LastEvaluatedKey', None)
        done = start_key is None
    return items
# --------------------------------------------------------------------
def list_id_proc(icount,unit):
    str_out = str(icount) + '\t'
    str_out += unit['id']
    print(str_out)
# --------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")

dynamodb = boto3.resource('dynamodb')

name_table = 'cities'

table = dynamodb.Table(name_table)

icount = 0
#
data = scan_all(table, {})
for unit in data:
    list_id_proc(icount,unit)
    icount += 1
#
sys.stderr.write("icount = %d\n" % icount)
sys.stderr.write("len(data) = %d\n" % len(data))
sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------

レコード数のカウント

aws dynamodb scan --table-name cities --select "COUNT"

参考ページ
Python3: DynamoDB を使う
aws cli で DynamoDB を使う

0
0
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
0
0