This post is the continuation from the previous post [AWS -Python] DynamoDB with Boto3, which deals with the basic DynamoDB operations.
Batch Write
import boto3
client = boto3.client('dynamodb')
# create a table
response = client.batch_write_item(
RequestItems={
'Product': [
{
'PutRequest': {
'Item': {
'Name': { 'S': 'Bread' },
'Code': { 'S': 'B3' },
'Price': { 'N': '5.49' },
}
}
},
{
'PutRequest': {
'Item': {
'Name': { 'S': 'Laptop' },
'Code': { 'S': 'LPT_A1' },
'Price': { 'N': '1250.00' },
}
}
}
]
},
)
# Table Resource
dynamodb = boto3.resource('dynamodb')
productTables = dynamodb.Table('Product')
print('Item Count:', productTables.item_count)
Paginator – Scan
import boto3
client = boto3.client('dynamodb')
paginator = client.get_paginator('scan')
# scan all
scan_iterator = paginator.paginate(
TableName = 'Product',
PaginationConfig={
'MaxItems': 1000,
'PageSize': 3
}
)
print('==== Sacn - All ====')
for index, page in enumerate(scan_iterator):
print(f'==== page {index+1} - Count: {page["Count"]} ====')
for item in page['Items']:
print(f'\tName: {item["Name"]["S"]}, Code: {item["Code"]["S"]}, Price: ${float(item["Price"]["N"]):,.2f}')
# scan filtering
filter_scan_iterator = paginator.paginate(
TableName = 'Product',
ExpressionAttributeValues={
':price': {
'N': '100',
},
},
FilterExpression = 'Price >= :price',
PaginationConfig={
'MaxItems': 1000,
'PageSize': 3
}
)
print()
print('==== Sacn - Filtering ====')
for index, page in enumerate(filter_scan_iterator):
print(f'==== page {index+1} - Count: {page["Count"]} ====')
for item in page['Items']:
print(f'\tName: {item["Name"]["S"]}, Code: {item["Code"]["S"]}, Price: ${float(item["Price"]["N"]):,.2f}')
- Note that the paging is done before filtering in the scan operation.
- You can notice the empty page.
Paginator – Query
import boto3
client = boto3.client('dynamodb')
paginator = client.get_paginator('query')
# query all
query_iterator = paginator.paginate(
TableName = 'Product',
ExpressionAttributeValues={
':productName': {
'S': 'Bread',
},
},
ExpressionAttributeNames={
'#ProductName': 'Name',
},
KeyConditionExpression = '#ProductName = :productName',
PaginationConfig={
'MaxItems': 100,
'PageSize': 2
}
)
print('==== Query - All ====')
for index, page in enumerate(query_iterator):
print(f'==== page {index+1} - Count: {page["Count"]} ====')
for item in page['Items']:
print(f'\tName: {item["Name"]["S"]}, Code: {item["Code"]["S"]}, Price: ${float(item["Price"]["N"]):,.2f}')
# query filtering
query_filter_iterator = paginator.paginate(
TableName = 'Product',
ExpressionAttributeValues={
':productName': {
'S': 'Bread',
},
':price': {
'N': '5',
},
},
ExpressionAttributeNames={
'#ProductName': 'Name',
},
KeyConditionExpression = '#ProductName = :productName',
FilterExpression = 'Price >= :price',
PaginationConfig={
'MaxItems': 100,
'PageSize': 2
}
)
print()
print('==== Query - Filtering ====')
for index, page in enumerate(query_filter_iterator):
print(f'==== page {index+1} - Count: {page["Count"]} ====')
for item in page['Items']:
print(f'\tName: {item["Name"]["S"]}, Code: {item["Code"]["S"]}, Price: ${float(item["Price"]["N"]):,.2f}')
