python - Ceating dynamodb 表说“无效的一个或多个参数值无效:某些索引键属性未在 AttributeDefinitions 中定义”
问题描述
我正在尝试用 python 创建 dynamo db 表。下面是我的脚本。我正在尝试创建一个分区键和排序键和一堆列。
我尝试了什么:
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.create_table(
TableName='g_view_data',
KeySchema=[
{
'AttributeName': 'customer_id',
'KeyType': 'HASH' #Partition key
},
{
'AttributeName': 'key_id',
'KeyType': 'RANGE' #Sort key
}
],
AttributeDefinitions=[
{
'AttributeName': 'cusotmer_id',
'AttributeType': 'N'
},
{
'AttributeName': 'key_id',
'AttributeType': 'N'
},
{
'AttributeName': 'dashboard_name',
'AttributeType': 'S'
},
{
'AttributeName': 'tsm',
'AttributeType': 'S'
},
{
'AttributeName': 'security_block',
'AttributeType': 'S'
},
{
'AttributeName': 'core_block',
'AttributeType': 'S'
},
{
'AttributeName': 'type',
'AttributeType': 'S'
},
{
'AttributeName': 'subscription',
'AttributeType': 'S'
},
{
'AttributeName': 'account_id',
'AttributeType': 'S'
},
{
'AttributeName': 'region',
'AttributeType': 'S'
},
{
'AttributeName': 'NAT',
'AttributeType': 'S'
},
{
'AttributeName': 'jb',
'AttributeType': 'S'
},
{
'AttributeName': 'dc',
'AttributeType': 'S'
},
{
'AttributeName': 'av',
'AttributeType': 'S'
},
{
'AttributeName': 'gl',
'AttributeType': 'S'
},
{
'AttributeName': 'backup',
'AttributeType': 'S'
},
{
'AttributeName': 'cpm',
'AttributeType': 'S'
},
{
'AttributeName': 'zb',
'AttributeType': 'S'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)
print("Table status:", table.table_status)
我得到的输出:
invalid One or more parameter values were invalid: Some index key attributes are not defined in AttributeDefinitions.
有人可以建议我的代码有什么问题吗?只是尝试创建一个简单的表。为什么它抱怨参数丢失。不确定..有人可以建议吗
Edit1:在属性中添加customer_id,key_id后,我得到了不同的错误
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the CreateTable operation: One or more parameter values were invalid: Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions
解决方案
如AWS 文档中所述,KeySchema 中的属性也必须在 AttributeDefinitions 中定义。请尝试将 customer_id 和 key_id 添加到您的 AttributeDefinitions 中。
至于 AttributeDefinitions,它们仅用于键(主键和索引)。所以这是一个对我有用的例子:
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.create_table(
TableName='g_view_data',
KeySchema=[
{
'AttributeName': 'customer_id',
'KeyType': 'HASH'
},
{
'AttributeName': 'key_id',
'KeyType': 'RANGE'
}
],
AttributeDefinitions=[
{
'AttributeName': 'customer_id',
'AttributeType': 'N'
},
{
'AttributeName': 'key_id',
'AttributeType': 'N'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)
print("Table status:", table.table_status)
然后在创建该表后,您可以添加具有所需任何属性的项目。例如,将此代码添加到您的脚本中:
table = dynamodb.Table('g_view_data')
item = table.put_item(
Item={
'customer_id': 234,
'key_id': 123,
'dashboard_name': 'test',
}
)
推荐阅读
- plsql - 页面项目中带有问号的菱形
- python - 按关键字对 2d numpy 字符串数组进行排序
- r - 仅引用 R 中 str_extract_all 中的最后一个匹配项
- reactjs - 无法使用 amqplib 库在 ReactJs 中连接 rabbitMQ
- bash - 1>/dev/null 和 >/dev/null 和 &>/dev/null
- javascript - 某些节点具有多个父节点的垂直图
- specflow - 两个给定步骤之间的规范流挂钩
- shell - 以下 shell 脚本行是什么意思?
- docker - Docker 容器无法启动 > FATA 008 无法初始化本地 MSP
- java - 更新 build.gradle 会从类路径中删除库