【问题标题】:Creating two dynamoDB tables in serverless.yml在 serverless.yml 中创建两个 dynamoDB 表
【发布时间】:2018-04-29 21:06:49
【问题描述】:

我正在尝试将 2 个表添加到 serverless.yml 以与 DynamoDB 链接。

我在 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”这两个表达式的含义是什么?

【问题讨论】:

    标签: node.js amazon-dynamodb serverless-framework


    【解决方案1】:

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

    注意:-

    您在创建 DynamoDB 表时只能定义关键属性。换句话说,您不需要定义所有其他非键属性。

    试试这个:-

    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    
    

    读写容量单位:-

    您以读取容量单位和 写入容量单位:

    一个读取容量单位代表一个强一致性读取 秒,或每秒两次最终一致的读取,用于向上的项目 大小为 4 KB。如果您需要读取大于 4 KB 的项目, DynamoDB 将需要消耗额外的读取容量单位。这 所需的读取容量单位总数取决于项目大小, 以及您是否想要最终一致或强烈一致 读。一个写入容量单位表示每秒一次写入 最大为 1 KB 的项目。如果你需要写一个更大的项目 超过 1 KB,DynamoDB 将需要消耗额外的写入容量 单位。所需的写入容量单位总数取决于 项目大小。

    Read and write capacity units

    【讨论】:

      【解决方案2】:

      简答:

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

      替代方案:

      使用DynamoDB On-Demand 并为 Db 表的使用付费而不是计算 WCU 和 RCU 可能更容易。

      示例

      以下是 3 个表格以格式化方式添加且不带半引号的示例:

      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

      【讨论】:

        猜你喜欢
        • 2018-12-18
        • 1970-01-01
        • 2017-11-30
        • 1970-01-01
        • 2018-12-03
        • 2019-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多