gpt4 book ai didi

amazon-dynamodb - DynamoDB 查询二级索引,如何定义索引

转载 作者:行者123 更新时间:2023-12-04 07:04:24 25 4
gpt4 key购买 nike

我一直在四处走动,只是不清楚该怎么做。

我有一个简单的表,我想在其中对几列进行查询。据我了解,这意味着为要查询的每一列创建一个二级索引。我已经使用无服务器框架 serverless.yml 定义了该表,并且收到了各种奇怪的错误消息。

当前的 serverless.yml 定义是:

resources:
Resources:
MessagesDynamoDBTable:
Type: 'AWS::DynamoDB::Table'
Properties:
AttributeDefinitions:
- AttributeName: messageId
AttributeType: S
- AttributeName: room
AttributeType: S
- AttributeName: userId
AttributeType: S
KeySchema:
- AttributeName: messageId
KeyType: HASH
- AttributeName: userId
KeyType: RANGE
LocalSecondaryIndexes:
- IndexName: roomIndex
KeySchema:
- AttributeName: room
KeyType: HASH
Projection:
ProjectionType: "KEYS_ONLY"
- IndexName: userId
KeySchema:
- AttributeName: userId
KeyType: HASH
Projection:
ProjectionType: "KEYS_ONLY"
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:custom.tableName}

它意味着类似于 Slack 服务 - 因此我想从房间、用户等查询条目。

根据我能够找到的文档,这个定义是有道理的。一个应该为索引中使用的列声明属性,我已经这样做了。 KeySchema - 我真的只需要 messageId 字段,但是一条错误消息表明它需要一个 RANGE 索引,所以我将 userId 字段添加到 KeySchema 只是为了关闭它。根据我能够找到的文档,二级索引字段看起来是正确的。

有了这个定义,我在尝试使用 serverless deploy 进行部署时遇到了这个错误
An error occurred: MessagesDynamoDBTable - Property AttributeDefinitions is inconsistent 
with the KeySchema of the table and the secondary indexes.

我尝试了几种变体,也遇到了其他奇怪的错误。以下是一些,但我不记得对定义进行了哪些相应的更改。
An error occurred: MessagesDynamoDBTable - One or more parameter values were invalid: 
Index KeySchema does not have a range key for index: userId (Service: AmazonDynamoDBv2; Status Code: 400;
Error Code: ValidationException; Request ID: 1KFA2IMASC12HRLLDPG753CU63VV4KQNSO5AEMVJF66Q9ASUAAJG).

An error occurred: MessagesDynamoDBTable - 1 validation error detected: Value '[com.amazonaws.dynamodb.v20120810.KeySchemaElement@aa4cdc91,
com.amazonaws.dynamodb.v20120810.KeySchemaElement@d2cd6f64, com.amazonaws.dynamodb.v20120810.KeySchemaElement@4d7c1f9,
com.amazonaws.dynamodb.v20120810.KeySchemaElement@d2cd6f64]' at 'keySchema' failed to satisfy constraint: Member must have length less
than or equal to 2 (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: BOVVBQ1F35VA18CCF3L5MSKS1FVV4KQNSO5AEMVJF66Q9ASUAAJG).

An error occurred: MessagesDynamoDBTable - Property AttributeDefinitions is inconsistent with the KeySchema
of the table and the secondary indexes.

An error occurred: MessagesDynamoDBTable - One or more parameter values were invalid: Index KeySchema does not have a range key for index:
userIdIndex (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: KFS63VSPKDUC60DV6U2V47UP27VV4KQNSO5AEMVJF66Q9ASUAAJG).

An error occurred: MessagesDynamoDBTable - One or more parameter values were invalid: Table KeySchema does not have a range key,
which is required when specifying a LocalSecondaryIndex (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 86Q2JSPM6Q9UPNIEOVHALLIIQJVV4KQNSO5AEMVJF66Q9ASUAAJG).

最佳答案

它不起作用的原因是本地二级索引中的键必须与表具有相同的分区键。因此,在您的情况下,您的本地二级索引必须将 messageId 作为其 HASH 键,将 roomuserId 作为其各自索引上的 RANGE 键。而且由于您的表已经由 (messageId, userId) 键控,因此您不需要 userId Local Secondary Index

这种设置在技术上可行:

MessagesDynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: messageId
AttributeType: S
- AttributeName: room
AttributeType: S
- AttributeName: userId
AttributeType: S
KeySchema:
- AttributeName: messageId
KeyType: HASH
- AttributeName: userId
KeyType: RANGE
LocalSecondaryIndexes:
- IndexName: roomIndex
KeySchema:
- AttributeName: messageId
KeyType: HASH
- AttributeName: room
KeyType: RANGE
Projection:
ProjectionType: KEYS_ONLY
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:custom.tableName}

但是,如果您想做的是按房间和用户查询,那么您可能想要采用不同的表设计。您尝试执行的操作最终会要求您始终使用 messageId 作为查询的一部分来查询表,因为它是分区键。因此,您无法仅通过 roomuserId 进行查询。您可能想要的是 Global Secondary Indexes 。在这种情况下,这将起作用:
MessagesDynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: messageId
AttributeType: S
- AttributeName: room
AttributeType: S
- AttributeName: userId
AttributeType: S
KeySchema:
- AttributeName: messageId
KeyType: HASH
GlobalSecondaryIndexes:
- IndexName: roomIndex
KeySchema:
- AttributeName: room
KeyType: HASH
Projection:
ProjectionType: KEYS_ONLY
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
- IndexName: userIndex
KeySchema:
- AttributeName: userId
KeyType: HASH
Projection:
ProjectionType: KEYS_ONLY
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:custom.tableName}

请注意,制作 ProjectionType: KEYS_ONLY 意味着当您查询 roomIndexuserIndex 时,您将得到的只是 messageIds - 然后您必须使用 messageIds 重新查询表以获取其他属性。您可能希望根据您的使用模式使用不同的 ProjectionType

关于amazon-dynamodb - DynamoDB 查询二级索引,如何定义索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50597791/

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