gpt4 book ai didi

lambda - lambda 上的 DynamoDB 验证异常

转载 作者:行者123 更新时间:2023-12-03 15:45:57 25 4
gpt4 key购买 nike

调用我的 lambda 技能时出现以下错误

ClientError: An error occurred (ValidationException) 
when calling the CreateTable operation: 1 validation error detected:
Value '[com.amazonaws.dynamodb.v20120810.KeySchemaElement@2273ace6,
com.amazonaws.dynamodb.v20120810.KeySchemaElement@4d13ab9,
com.amazonaws.dynamodb.v20120810.KeySchemaElement@115e22b2]' at
'keySchema' failed to satisfy constraint: Member must have length less than or equal to 2

这是代码:
def write_values_to_db(ddid, token, intent):
pid = ...
dynamodb_client = boto3.client('dynamodb')
try:
response = dynamodb_client.create_table(
AttributeDefinitions=[
{
'AttributeName': 'pid',
'AttributeType': 'S',
},
{
'AttributeName': 'ddid',
'AttributeType': 'S',
},
{
'AttributeName': 'token',
'AttributeType': 'S',
},
],
KeySchema=[
{
'AttributeName': 'pid',
'KeyType': 'HASH',
},
{
'AttributeName': 'ddid',
'KeyType': 'RANGE',
},
{
'AttributeName': 'token',
'KeyType': 'RANGE',
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5,
},
TableName='Values',
)
except dynamodb_client.exceptions.ResourceInUseException:
dynamodb_client.put_item(
TableName='Values',
Item={
'pid': pid,
'ddid': ddid,
'token': token
}
)

根据我的仪表板,错误在 TableName='Values' 上线。我正在学习教程并且只更改了某些内容,所以我不明白为什么这不起作用。我无法在本地环境中进行测试,因为我有区域/凭据问题。

最佳答案

您代码中的 KeySchema 应如下所示,

AttributeDefinitions=[
{
'AttributeName': 'pid',
'AttributeType': 'S',
}
],
KeySchema=[
{
'AttributeName': 'pid',
'KeyType': 'HASH'
}
]

您最多只能有一个哈希键和一个范围键。

如果你想要额外的索引,你可以用二级索引创建它们。

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LSI.html

以下是全局二级索引的语法。

引用: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html
GlobalSecondaryIndexes: [
{
IndexName: 'STRING_VALUE', /* required */
KeySchema: [ /* required */
{
AttributeName: 'STRING_VALUE', /* required */
KeyType: HASH | RANGE /* required */
},
/* more items */
],
Projection: { /* required */
NonKeyAttributes: [
'STRING_VALUE',
/* more items */
],
ProjectionType: ALL | KEYS_ONLY | INCLUDE
},
ProvisionedThroughput: { /* required */
ReadCapacityUnits: 0, /* required */
WriteCapacityUnits: 0 /* required */
}
},
/* more items */
]

关于lambda - lambda 上的 DynamoDB 验证异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46125221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com