DynamoDB는 Amazon에서 AWS에 최적화된 NoSQL 데이터베이스이다.
AWS DynamoDB를 사용하기 위해서 Amazon에서는 각 프로그래밍 언어 별 SDK를 배포하고 있다.
pip install boto3
import boto3
def create_data():
# dynamodb connect
client = boto3.client('dynamodb')
dynamodb = boto3.resource('dynamodb')
# dynamodb table connect
table = dynamodb.Table('channels')
# Create
table.put_item(
Item = {
'user_id': '1',
'first_name': 'Gildong',
'last_name': 'Hong',
'age': 30
}
)
import boto3
from boto3.dynamodb.conditions import Key, Attr
def get_data():
# dynamodb connect
client = boto3.client('dynamodb')
dynamodb = boto3.resource('dynamodb')
# dynamodb table connect
table = dynamodb.Table('channels')
# Read
# user_id가 1인 row가져오기
response = table.get_item(
Key = { 'user_id': '1' }
)
item = response['Items']
print(item)
import boto3
def update_data():
# dynamodb connect
client = boto3.client('dynamodb')
dynamodb = boto3.resource('dynamodb')
# dynamodb table connect
table = dynamodb.Table('channels')
# Update
table.update_item(
Key={ 'user_id': '1' },
UpdateExpression='SET age = :val1',
ExpressionAttributeValues={
':val1': 31
}
)
import boto3
def delete_data():
# dynamodb connect
client = boto3.client('dynamodb')
dynamodb = boto3.resource('dynamodb')
# dynamodb table connect
table = dynamodb.Table('channels')
# Delete
table.delete_item(
Key={ 'user_id': 1 }
)