在 serverless.yml 中创建两个 dynamoDB 表

Creating two dynamoDB tables in serverless.yml

我尝试使用 DynamoDB 将 2 个表添加到 serverless.yml 到 link。

我在 serverless.yml 中的部分代码:

...       
 resources:
      Resources:
        ItemsTable:
          Type: "AWS::DynamoDB::Table"
          Properties:
            TableName: "InvoiceConfig"
            AttributeDefinitions:
            - AttributeName: "providerName"
              AttributeType: "S"
            KeySchema:
            - AttributeName: "providerName"
              KeyType: "HASH"
            ProvisionedThroughput:
              ReadCapacityUnits: 2
              WriteCapacityUnits: 2
            TableName: "DifferentTermsPages"
            AttributeDefinitions:
            - AttributeName: "id"
              AttributeType: "S"
            - AttributeName: "providerName"
              AttributeType: "S"
            - AttributeName: "productType"
              AttributeType: "S"
            - AttributeName: "language"
              AttributeType: "S"
            - AttributeName: "terms"
              AttributeType: "L"
            KeySchema:
            - AttributeName: "id"
              KeyType: "HASH"
            - AttributeName: "providerName"
              KeyType: "HASH"
            - AttributeName: "productType"
              KeyType: "HASH"
            - AttributeName: "language"
              KeyType: "HASH"
            - AttributeName: "terms"
              KeyType: "HASH"
            ProvisionedThroughput:
              ReadCapacityUnits: 10
              WriteCapacityUnits: 10

对吗?

我的表是:

InvoiceConfig: with columns: providerName (String)
DifferentTermsPages: id (String), providerName (String), productType (String), language (String), terms (list)

我需要对 serverles.yml 进行更多更改吗?表达式 "ReadCapacityUnits" 和 "WriteCapacityUnits" 的含义是什么?

两个资源(即两个 DynamoDB table)之间应该有一些分离。

注:-

您可以在创建 DynamoDB 时仅定义关键属性 table。换句话说,您不需要定义所有其他非键属性。

试试这个:-

Resources:
ItemsTable:
  Type: "AWS::DynamoDB::Table"
  Properties:
    TableName: "InvoiceConfig"
    AttributeDefinitions:
    - AttributeName: "providerName"
      AttributeType: "S"
    KeySchema:
    - AttributeName: "providerName"
      KeyType: "HASH"
    ProvisionedThroughput:
      ReadCapacityUnits: 2
      WriteCapacityUnits: 2            
DifferentTermsPages:
  Type: "AWS::DynamoDB::Table"
  Properties:             
    TableName: "DifferentTermsPages"
    AttributeDefinitions:
    - AttributeName: "id"
      AttributeType: "S"
    KeySchema:
    - AttributeName: "id"
      KeyType: "HASH"
    ProvisionedThroughput:
      ReadCapacityUnits: 10
      WriteCapacityUnits: 10    

读写容量单位:-

You specify throughput capacity in terms of read capacity units and write capacity units:

One read capacity unit represents one strongly consistent read per second, or two eventually consistent reads per second, for an item up to 4 KB in size. If you need to read an item that is larger than 4 KB, DynamoDB will need to consume additional read capacity units. The total number of read capacity units required depends on the item size, and whether you want an eventually consistent or strongly consistent read. One write capacity unit represents one write per second for an item up to 1 KB in size. If you need to write an item that is larger than 1 KB, DynamoDB will need to consume additional write capacity units. The total number of write capacity units required depends on the item size.

Read and write capacity units

简答:

读取和写入容量单位是 数据库每秒允许处理的最大数据大小,如果您在任何一秒内超过此数量,您的请求都会受到限制。

选择:

仅使用 DynamoDB On-Demand 并为 Db table 使用付费可能比计算 WCU 和 RCU 更容易。

例子

这是一个以格式化方式添加且没有分号的 3 table 的示例:

resources:
  Resources:
    myDynamoDBTable1:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: Table1
        AttributeDefinitions:
          - AttributeName: ColumnName1
            AttributeType: S
          - AttributeName: ColumnName2
            AttributeType: N
        KeySchema:
          - AttributeName: ColumnName1
            KeyType: HASH
          - AttributeName: ColumnName2
            KeyType: RANGE
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1 
    myDynamoDBTable2:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: Table2
        AttributeDefinitions:
          - AttributeName: ColumnName1
            AttributeType: S
        KeySchema:
          - AttributeName: ColumnName1
            KeyType: HASH
        BillingMode: PAY_PER_REQUEST
    myDynamoDBTableN:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: TableN
        AttributeDefinitions:
          - AttributeName: ColumnName1
            AttributeType: S
        KeySchema:
          - AttributeName: ColumnName1
            KeyType: HASH
        BillingMode: PAY_PER_REQUEST

附加说明示例:

返回Read/Write Capacity Mode:

写入容量单位 (WCU) 公式:向上舍入 (DataSize / 1KB)

示例 1: 假设您预见到每秒将 10KB 数据写入数据库的流量。使用 WCU 公式,您需要 (10KB / 1KB) = 10WCU。

示例 2: 预计将 7.5KB 的数据流量写入数据库,我们需要:(7.5KB / 1KB) = 8WCU

读取容量单位 (RCU) 取决于强一致性或最终一致性模型。

强一致性模式:向上取整(DataSize / 4KB)
最终一致模式:四舍五入(DataSize / 4KB) / 2