Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set TTL in Java based app for DynamoDB

Hi I need to set time to live programmatically for a table in DynamoDB via AWS Java SDK. Is it possible? I know that TTL feature is introduced recently - http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html

UPDATE: There is no special annotaion, but we can do it manually:

@DynamoDBAttribute
private long ttl;

and configure it as ttl in AWS - 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
like image 534
idmitriev Avatar asked Mar 15 '17 14:03

idmitriev


People also ask

Does DynamoDB support TTL?

Amazon DynamoDB Time to Live (TTL) allows you to define a per-item timestamp to determine when an item is no longer needed. Shortly after the date and time of the specified timestamp, DynamoDB deletes the item from your table without consuming any write throughput.

What is TTL attribute name?

The TTL attribute's value must be a timestamp in Unix epoch time format in seconds. If you use any other format, the TTL processes ignore the item. For example, if you set the value of the attribute to 1645119622, that is Thursday, February 17, 2022 17:40:22 (GMT), the item will be expired after that time.

Is DynamoDB written in Java?

There is a development version known as DynamoDB Local used to run on developer laptops written in Java, but the cloud-native database architecture is proprietary closed-source.

Is DynamoDB good for time series data?

This takes out the guesswork of provisioning read and write units and you simply pay for what you use. All that to say, DynamoDb can work well for time series data, and can be much faster at querying the data, but you need to carefully consider how you need to access it.


1 Answers

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);
    }
like image 137
Jerry Z. Avatar answered Sep 28 '22 06:09

Jerry Z.