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

Dav*_*ron 2 amazon-dynamodb serverless-framework dynamodb-queries amazon-dynamodb-index

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

我有一个简单的表,我想在其中对几列进行查询。据我了解,这意味着为要查询的每一列创建二级索引。我已经使用Serverless框架定义了表,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}
Run Code Online (Sandbox Code Playgroud)

它的意思是类似于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.
Run Code Online (Sandbox Code Playgroud)

我尝试了几种变体,还遇到了其他奇怪的错误。以下是一些内容,但我不记得对定义进行了相应的更改。

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).
Run Code Online (Sandbox Code Playgroud)

Bri*_*ant 7

它不起作用的原因是,本地二级索引中的键必须具有与表相同的分区键。所以你的情况,你那地方的指标必须有messageIdHASH关键和roomuserIdRANGE它们各自的索引键。并且由于您的表已经被键控,(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}
Run Code Online (Sandbox Code Playgroud)

但是,如果您要按房间和用户查询,那么您可能希望采用其他表格设计。您试图做的事情最终将需要您始终使用messageIdas作为查询的一部分来查询表,因为它是分区键。因此,您将无法仅通过roomand 查询userId。您可能需要的是全局二级索引。在这种情况下,这将起作用:

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}
Run Code Online (Sandbox Code Playgroud)

请注意,ProjectionType: KEYS_ONLY在查询roomIndexuserIndex返回结果时,您的出路是公正的messageIds-然后,您必须使用来重新查询表messageIds以获取其他属性。您可能要ProjectionType根据使用方式使用其他方式。