Trong bước này, chúng ta tạo một table có tên Movies. Primary key cho table bao gồm các attritube sau:
year – The partition key. AttributeType là N(number).
title – The sort key. AttributeType là S(string).
Tạo file có tên MoviesCreateTable.py
Sao chép đoạn mã sau và dán vào file MoviesCreateTable.py
import boto3
def create_movie_table(dynamodb=None):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.create_table(
TableName='Movies',
KeySchema=[
{
'AttributeName': 'year',
'KeyType': 'HASH' # Partition key
},
{
'AttributeName': 'title',
'KeyType': 'RANGE' # Sort key
}
],
AttributeDefinitions=[
{
'AttributeName': 'year',
'AttributeType': 'N'
},
{
'AttributeName': 'title',
'AttributeType': 'S'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)
return table
if __name__ == '__main__':
movie_table = create_movie_table()
print("Table status:", movie_table.table_status)
python MoviesCreateTable.py
Trường hợp khác: sử dụng python "đường dẫn file MoviesCreateTable.py"
Table status: CREATING là tạo table thành công.