2017-03-15 28 views
0

您好,我需要设置时间以编程方式通过AWS Java SDK为DynamoDB中的表生活。可能吗?我知道,TTL功能近期推出 - http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html如何在基于Java的DynamoDB应用程序中设置TTL

UPDATE: 没有特殊annotaion,但我们可以做手工:

@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 
+0

吧,正好! – idmitriev

回答

1

AmazonDynamoDBClient.updateTimeToLive记录here

+0

哇,这很容易,对这个问题感到抱歉,谢谢 – idmitriev

0

代码示例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); 
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); 
 
    }

相关问题