AWS DynamoDB是一种托管的NoSQL数据库服务,它专为大规模互联网应用程序而设计,具有高可扩展性和高性能。以下是一个使用AWS SDK for Python(Boto3)的简单示例,展示如何连接到DynamoDB并执行一些操作:
import boto3
# 创建DynamoDB客户端
dynamodb = boto3.client('dynamodb')
# 创建表
response = dynamodb.create_table(
TableName='my_table',
KeySchema=[
{
'AttributeName': 'id',
'KeyType': 'HASH' # Partition key
}
],
AttributeDefinitions=[
{
'AttributeName': 'id',
'AttributeType': 'N'
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)
# 插入数据
response = dynamodb.put_item(
TableName='my_table',
Item={
'id': {'N': '1'},
'name': {'S': 'John Doe'},
'age': {'N': '25'}
}
)
# 查询数据
response = dynamodb.get_item(
TableName='my_table',
Key={
'id': {'N': '1'}
}
)
item = response['Item']
print(item)
# 删除表
response = dynamodb.delete_table(
TableName='my_table'
)
请注意,上述示例是使用Python的Boto3库进行的,你可以根据自己的需求选择其他语言的AWS SDK。还可以根据DynamoDB的数据模型和操作符来执行更复杂的数据操作。