【问题标题】:need to create a TTL (time-to-live) feature during create table in dynamodb在 dynamodb 中创建表期间需要创建 TTL(生存时间)功能
【发布时间】:2020-11-19 19:39:21
【问题描述】:

我正在使用带有 dynamodb 的 nodejs。我需要在我的代码中添加 TTL 功能。当我在我的架构文件中添加以下代码并尝试创建表时,它会给出以下错误。此外,我的 timetolive 字段也没有在桌子上创建。

{
  AttributeDefinitions: [
    { AttributeName: 'foo', AttributeType: 'S' },
    { AttributeName: 'baar', AttributeType: 'N' },
    { AttributeName: 'timetolive', AttributeType: 'N' }
  ],
  KeySchema: [
    { AttributeName: 'foo', KeyType: 'HASH' },
    { AttributeName: 'baar', KeyType: 'RANGE' }
  ],
  ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 },
  TimeToLiveSpecification: { AttributeName: 'timetolive', Enabled: true },
  TableName: 'mytablename'
}

错误:

UnexpectedParameter: Unexpected key 'TimeToLiveSpecification' found in params

【问题讨论】:

    标签: amazon-web-services amazon-dynamodb schema ttl


    【解决方案1】:

    您应该从AttributeDefinitions删除 timetoliveAttributeDefinitions 仅适用于在主表及其索引中的散列或排序键中使用的属性。所以应该是:

    {
      AttributeDefinitions: [
        { AttributeName: 'foo', AttributeType: 'S' },
        { AttributeName: 'baar', AttributeType: 'N' }
      ],
      KeySchema: [
        { AttributeName: 'foo', KeyType: 'HASH' },
        { AttributeName: 'baar', KeyType: 'RANGE' }
      ],
      ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 },
      TimeToLiveSpecification: { AttributeName: 'timetolive', Enabled: true },
      TableName: 'mytablename'
    }
    

    【讨论】:

    • 你测试了这个答案吗?他得到的错误不是关于 AttributeDefinitions,而是关于不受支持的 TimeToLiveSpecification。实际上,CreateTable 操作没有这个选项。您需要创建表,然后才使用 UpdateTimeToLive 操作选择 TTL 属性。
    • 此解决方案不起作用。 AFAIK,您需要使用 UpdateTimeToLive 方法选择 TTL 属性。正如 Nadav 指出的那样。
    猜你喜欢
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 2014-04-13
    • 2013-10-05
    相关资源
    最近更新 更多