Amazon web services 如何在serverless.yml中为DynamoDB定义复合键?

Amazon web services 如何在serverless.yml中为DynamoDB定义复合键?,amazon-web-services,amazon-dynamodb,serverless-framework,Amazon Web Services,Amazon Dynamodb,Serverless Framework,我试图定义一个具有3列组合键的dynamodb表。这可能吗 我看到过一些示例和教程,它们的代码如下: resources: Resources: TodosDynamoDbTable: Type: 'AWS::DynamoDB::Table' DeletionPolicy: Retain Properties: AttributeDefinitions: - AttributeName:

我试图定义一个具有3列组合键的dynamodb表。这可能吗

我看到过一些示例和教程,它们的代码如下:

resources:
  Resources:
    TodosDynamoDbTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          -
            AttributeName: id
            AttributeType: S
        KeySchema:
          -
            AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: ${self:provider.environment.DYNAMODB_TABLE}
  Properties:
    AttributeDefinitions:
      - AttributeName: id
        AttributeType: N
      - AttributeName: server
        AttributeType: S
      - AttributeName: room
        AttributeType: S
      - AttributeName: friendlyName
        AttributeType: S
但它只有一列和一个键。我的桌子看起来像这样:

resources:
  Resources:
    TodosDynamoDbTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          -
            AttributeName: id
            AttributeType: S
        KeySchema:
          -
            AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: ${self:provider.environment.DYNAMODB_TABLE}
  Properties:
    AttributeDefinitions:
      - AttributeName: id
        AttributeType: N
      - AttributeName: server
        AttributeType: S
      - AttributeName: room
        AttributeType: S
      - AttributeName: friendlyName
        AttributeType: S
我想在
id
server
room
上有一把钥匙。用户可以位于同一服务器上的多个房间中,也可以位于多个服务器上的多个房间中。但是,在这三个关键点上,它总是独一无二的


我不知道如何定义
KeySchema
部分。有什么帮助吗?

首先,您不能在DynamoDB中创建复合键(如在关系数据库中)

表中的键是散列键和范围键(也称为排序键和可选键)。由于这限制了查询能力,DynamoDB支持创建称为全局二级索引(GSI)和本地二级索引(LSI)的索引来扩展查询能力

根据您的模式,由于id、server和room的组合是唯一的,因此可以对哈希键使用串联,例如
id\u server\u room
,以便强制表中的项具有唯一性


然后可以创建id、服务器和房间作为属性。为了有效地从这些属性进行查询,请根据需要创建GSI。

首先,您不能在DynamoDB中创建复合键(如在关系数据库中)

表中的键是散列键和范围键(也称为排序键和可选键)。由于这限制了查询能力,DynamoDB支持创建称为全局二级索引(GSI)和本地二级索引(LSI)的索引来扩展查询能力

根据您的模式,由于id、server和room的组合是唯一的,因此可以对哈希键使用串联,例如
id\u server\u room
,以便强制表中的项具有唯一性

然后可以创建id、服务器和房间作为属性。为了有效地从这些属性进行查询,请根据需要创建GSIs