Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon dynamodb 在DynamoDB中为同一用户拥有多条记录_Amazon Dynamodb_Aws Api Gateway_Serverless - Fatal编程技术网

Amazon dynamodb 在DynamoDB中为同一用户拥有多条记录

Amazon dynamodb 在DynamoDB中为同一用户拥有多条记录,amazon-dynamodb,aws-api-gateway,serverless,Amazon Dynamodb,Aws Api Gateway,Serverless,我希望在DynamoDB中为uname(同一用户)创建多个记录,但当我执行POST请求时,该记录会得到更新。我希望为同一用户创建多个记录,而不是一个。 i、 e:在使用所有属性的POST方法之后,记录将得到更新,而不是生成新记录 GET请求时:“url/uname”只给出一条记录 我希望获得多个记录,如何实现 service: Test1 # You can pin your service to only deploy with a specific Serverless version #

我希望在DynamoDB中为uname(同一用户)创建多个记录,但当我执行POST请求时,该记录会得到更新。我希望为同一用户创建多个记录,而不是一个。 i、 e:在使用所有属性的POST方法之后,记录将得到更新,而不是生成新记录

GET请求时:“url/uname”只给出一条记录

我希望获得多个记录,如何实现

service: Test1

# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
# frameworkVersion: "=X.X.X"

provider:
  name: aws
  runtime: python3.6
  environment:
    DYNAMODB_TABLE: ${self:service}-${opt:stage, self:provider.stage}
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}"

functions:
  create:
    handler: create.create
    events:
      - http:
          path: form
          method: post
          cors: true

  list:
    handler: list.list
    events:
      - http:
          path: form
          method: get
          cors: true

  get:
    handler: get.get
    events:
      - http:
          path: form/{uname}
          method: get
          cors: true

resources:
  Resources:
    TodosDynamoDbTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          -
            AttributeName: uname
            AttributeType: S
        KeySchema:
          -
            AttributeName: uname
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 10
          WriteCapacityUnits: 10
        TableName: ${self:provider.environment.DYNAMODB_TABLE}
你需要的是一份工作。在GSI中,每个分区键没有一个项的约束。以下是您可以做的:

更改主表,以便主键对于每个用户请求都具有唯一的值。每次需要保存用户请求时,都会生成一个新的主键,如下所示(仅举一个示例):
randomInteger[1:10000]+timestamp+userID
。这应该足以避免两个请求具有相同的分区密钥

对于这些项中的每一项,还将userID保存为单独的属性(可以是排序键或常规属性)。当然,还要保存所需的所有其他属性

在GSI中,将userID设置为主键。当您需要来自给定用户的所有项时,只需查询GSI(而不是主表)中的userID,就可以得到所有项