📜  DynamoDB-负载表(1)

📅  最后修改于: 2023-12-03 15:14:49.542000             🧑  作者: Mango

DynamoDB-负载表

什么是DynamoDB-负载表

DynamoDB-负载表是Amazon DynamoDB的一个组件,它使用无服务器技术来自动扩展表中的存储和吞吐量容量,以处理瞬时和预测性的高吞吐量和弹性负载。

功能和特点
  1. 自动扩展吞吐量容量,以匹配表的负载情况。这也意味着表将有一个相对较短的延迟。

  2. 自动回收存储空间,以优化表的性能。

  3. 支持不同的负载模式。DynamoDB-负载表支持峰值负载、周期性负载和高度不确定性负载等。

  4. 提供了对多个DynamoDB表的自动缩放和负载平衡,这使得整个系统具有更高的吞吐量和更好的可用性。

如何使用

使用DynamoDB-负载表非常简单,您只需要将其作为一个组件,集成到您的DynamoDB表中即可。

下面是一些使用DynamoDB-负载表的实例代码:

import boto3

# 创建DynamoDB客户端
dynamodb = boto3.client('dynamodb')

# 定义DynamoDB-负载表配置参数
autoscaling_suspended = False
minimum_units = 5
maximum_units = 100
scale_in_cooldown = 60
scale_out_cooldown = 60

# 定义DynamoDB表配置参数
table_name = 'my-table'
key_schema = [
    {
        'AttributeName': 'my-partition-key',
        'KeyType': 'HASH'
    },
    {
        'AttributeName': 'my-sort-key',
        'KeyType': 'RANGE'
    }
]
attribute_definitions = [
    {
        'AttributeName': 'my-partition-key',
        'AttributeType': 'S'
    },
    {
        'AttributeName': 'my-sort-key',
        'AttributeType': 'S'
    }
]
provisioned_throughput = {
    'ReadCapacityUnits': 5,
    'WriteCapacityUnits': 5
}

# 创建DynamoDB-负载表
response = dynamodb.create_table(
    TableName=table_name,
    KeySchema=key_schema,
    AttributeDefinitions=attribute_definitions,
    ProvisionedThroughput=provisioned_throughput,
    StreamSpecification={
        'StreamEnabled': True,
        'StreamViewType': 'NEW_AND_OLD_IMAGES'
    },
    SSESpecification={
        'Enabled': True
    },
    BillingMode='PAY_PER_REQUEST',
    TimeToLiveDescription={
        'AttributeName': 'expiration-time',
        'Enabled': True
    },
    GlobalSecondaryIndexes=[
        {
            'IndexName': 'my-gsi',
            'KeySchema': [
                {
                    'AttributeName': 'my-gsi-partition-key',
                    'KeyType': 'HASH'
                },
                {
                    'AttributeName': 'my-gsi-sort-key',
                    'KeyType': 'RANGE'
                }
            ],
            'Projection': {
                'ProjectionType': 'ALL'
            },
            'ProvisionedThroughput': {
                'ReadCapacityUnits': 5,
                'WriteCapacityUnits': 5
            }
        }
    ],
    SSESpecification={
        'Enabled': True,
        'SSEType': 'AES256'
    },
    TableName=table_name,
    Tags=[
        {
            'Key': 'my-tag-key',
            'Value': 'my-tag-value'
        },
    ]
)

# 在表中启用DynamoDB-负载表
response = dynamodb.update_table(
    TableName=table_name,
    StreamSpecification={
        'StreamEnabled': True,
        'StreamViewType': 'NEW_AND_OLD_IMAGES'
    },
    SSESpecification={
        'Enabled': True,
        'SSEType': 'AES256'
    },
    BillingMode='PAY_PER_REQUEST',
    ProvisionedThroughput={
        'ReadCapacityUnits': 5,
        'WriteCapacityUnits': 5
    },
    GlobalSecondaryIndexUpdates=[
        {
            'Create': {
                'IndexName': 'my-gsi',
                'KeySchema': [
                    {
                        'AttributeName': 'my-gsi-partition-key',
                        'KeyType': 'HASH'
                    },
                    {
                        'AttributeName': 'my-gsi-sort-key',
                        'KeyType': 'RANGE'
                    }
                ],
                'Projection': {
                    'ProjectionType': 'ALL'
                },
                'ProvisionedThroughput': {
                    'ReadCapacityUnits': 5,
                    'WriteCapacityUnits': 5
                }
            }
        }
    ],
    TimeToLiveDescription={
        'AttributeName': 'expiration-time',
        'Enabled': True
    },
    AutoScalingSettingsUpdate={
        'AutoScalingDisabled': autoscaling_suspended,
        'MinimumUnits': minimum_units,
        'MaximumUnits': maximum_units,
        'ScaleInCooldown': scale_in_cooldown,
        'ScaleOutCooldown': scale_out_cooldown
    }
)
总结

DynamoDB-负载表是一个非常有用的组件,它可以自动调整表的吞吐量容量和存储空间,以处理瞬时和预测性的高吞吐量和弹性负载。通过在其DynamoDB表中使用DynamoDB-负载表,程序员可以让整个系统具有更高的吞吐量和更好的可用性。