【问题标题】:How to set TTL in Java based app for DynamoDB如何在基于 Java 的应用程序中为 DynamoDB 设置 TTL
【发布时间】:2017-08-06 08:22:04
【问题描述】:

您好,我需要通过 AWS Java SDK 以编程方式为 DynamoDB 中的表设置时间。可能吗?我知道最近引入了TTL功能-http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html

更新: 没有特别的注释,但是我们可以手动做:

@DynamoDBAttribute
private long ttl;

并在 AWS 中将其配置为 ttl - http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/time-to-live-ttl-how-to.html

long now = Instant.now().getEpochSecond(); // unix time
long ttl = 60 * 60 * 24; // 24 hours in sec
setTtl(ttl + now); // when object will be expired

【问题讨论】:

    标签: java amazon-web-services amazon-dynamodb


    【解决方案1】:

    http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/time-to-live-ttl-how-to.html

        public void function(final AmazonDynamoDB client, final String tableName, final String ttlField){
    
            //table created now enabling TTL
            final UpdateTimeToLiveRequest req = new UpdateTimeToLiveRequest();
            req.setTableName(tableName);
    
            final TimeToLiveSpecification ttlSpec = new TimeToLiveSpecification();
            ttlSpec.setAttributeName(ttlField);
            ttlSpec.setEnabled(true);
            req.withTimeToLiveSpecification(ttlSpec);
    
            client.updateTimeToLive(req);
        }

    【讨论】:

      【解决方案2】:

      AmazonDynamoDBClient.updateTimeToLive 记录here 或直接链接here

      【讨论】:

      • 我不确定此链接是否正确,它只是将我带到 AWS SDK 文档的根目录,这并不是非常有用...
      • @JanikZikovsky 很难深入链接到那里的文档,但是如果您转到链接然后搜索 AmazonDynamoDBClient,转到该页面然后搜索 updateTimeToLive 您会找到文档用于更新 TTL 设置。
      • this link 怎么样 - 虽然它没有框架。
      【解决方案3】:

      代码示例here

        //Then set ttl field like below for days in java
          //ttl 60 days
          Calendar cal = Calendar.getInstance(); //current date and time      
          cal.add(Calendar.DAY_OF_MONTH, 60); //add days
          double ttl =  (cal.getTimeInMillis() / 1000L);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-30
        • 2017-08-26
        • 2011-02-10
        相关资源
        最近更新 更多