打发电机表说"invalid One or more parameter values were invalid: Some index key attributes are not defined



我正在尝试用python创建dynamo数据库表。下面是我的剧本。我正在尝试创建一个分区键、排序键和一堆列。

我尝试了什么:

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. 

有人能告诉我我的代码出了什么问题吗。。只是想创建一个简单的表。为什么它抱怨参数丢失。不确定。。有人能建议你吗

第1版:在属性中添加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',
}
)

这太晚了,但您在属性定义中的customer_id中有一个拼写错误

KeySchema=[
{
'AttributeName': 'customer_id',
'KeyType': 'HASH'  #Partition key
},
{
'AttributeName': 'key_id',
'KeyType': 'RANGE'  #Sort key
}
],
AttributeDefinitions=[
{
'AttributeName': 'cusotmer_id',
'AttributeType': 'N'
},

最新更新