您好,我需要通过 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
最佳答案
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);
}
关于java - 如何在基于 Java 的 DynamoDB 应用程序中设置 TTL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42812415/