创建 dynamodb table 说 "invalid One or more parameter values were invalid: Some index key attributes are not defined in AttributeDefinitions"
Ceating dynamodb table says "invalid One or more parameter values were invalid: Some index key attributes are not defined in AttributeDefinitions"
我正在尝试用 python 创建发电机数据库 table。下面是我的脚本。我正在尝试创建一个分区键和排序键以及一堆列。
我试过的:
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.
有人可以建议我的代码有什么问题吗..只是想创建一个简单的 table。为什么它抱怨参数丢失。不确定..有人可以建议吗
编辑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 documentation所述,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 之后,您可以添加具有您想要的任何属性的项目。例如,将此代码添加到您的脚本中:
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'
},
我正在尝试用 python 创建发电机数据库 table。下面是我的脚本。我正在尝试创建一个分区键和排序键以及一堆列。
我试过的:
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.
有人可以建议我的代码有什么问题吗..只是想创建一个简单的 table。为什么它抱怨参数丢失。不确定..有人可以建议吗
编辑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 documentation所述,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 之后,您可以添加具有您想要的任何属性的项目。例如,将此代码添加到您的脚本中:
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'
},