是的,KeySchema将创建内部索引,用于快速检索表中的项目。下面是一个示例代码,演示如何使用KeySchema创建索引:
import boto3
# 创建DynamoDB客户端
client = boto3.client('dynamodb')
# 创建表
response = client.create_table(
AttributeDefinitions=[
{
'AttributeName': 'id',
'AttributeType': 'N' # id作为值类型为数字
},
{
'AttributeName': 'name',
'AttributeType': 'S' # name作为值类型为字符串
}
],
KeySchema=[
{
'AttributeName': 'id', # 用id作为主键
'KeyType': 'HASH'
}
],
GlobalSecondaryIndexes=[
{
'IndexName': 'nameIndex',
'KeySchema': [
{
'AttributeName': 'name', # 用name属性作为索引
'KeyType': 'HASH'
},
],
'Projection': {
'ProjectionType': 'ALL'
},
'ProvisionedThroughput': {
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
}
],
TableName='my-table',
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)
# 打印响应
print(response)
以上示例代码将创建一个名为“my-table”的表,并添加一个使用“id”字段作为主键,并创建名为“nameIndex”的全局二级索引,使用“name”字段作为索引键。索引包含所有属性,并具有每秒读/写容量单位为5个的预配吞吐量。