2017-03-15 32 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); 
 
    }

相關問題