Tạo table

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).

  1. Tạo file có tên MoviesCreateTable.py

  2. 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)
  1. Chạy chương trình bằng Window Command Prompt đã được xác thực và cấu hình ở bước 3.1 Cấu hình AWS CLI. Gõ lệnh:
python MoviesCreateTable.py

Trường hợp khác: sử dụng python "đường dẫn file MoviesCreateTable.py"

  1. Sau khi chạy chương trình trả về Table status: CREATING là tạo table thành công.