简体   繁体   中英

How to set Time to live in dynamodb item

I am trying to add items in dynamodb in batch. My table consists of composite primary key ie a combination of primary key and sort key. I have enabled time to live on my table but metrics for deletedItemsCount is showing no change.

Following is my code :-

def generate_item(data):
    item = {
        "pk": data['pk'],
        "ttl": str(int(time.time())), # current time set for testing
        "data": json.dumps({"data": data}),
        "sk": data['sk']
    }

    return item

def put_input_data(input_data, table_name):
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table(table_name)

    data_list = input_data["data"]

    try:
        with table.batch_writer() as writer:
            for index, data in enumerate(data_list):
                writer.put_item(Item=generate_item(data))
    except ClientError as exception_message:
        raise

On querying the table I can see item is getting added into the table, but graph for deletedItemsCount shows no change.

Can someone point where am I going wrong ? Would appreciate any hint.

Thanks

looks like your ttl attribute is a String, but...

The TTL attribute's value must be a Number data type. For example, if you specify for a table to use the attribute name expdate as the TTL attribute, but the attribute on an item is a String data type, the TTL processes ignore the item.

Source: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/time-to-live-ttl-before-you-start.html#time-to-live-ttl-before-you-start-formatting

Hope that resolves your issue.

The implementation of time-to-live (TTL) is different in different databases and you shouldn't assume a specific implementation in DynamoDB.

The usual requirement of TTL is that the object will not be visible when reading or writing after the TTL period, and not necessarily be evicted from the table by that time. When you access an item in the table, DynamoDB checks the TTL of the item and returns it or updates it only if it is valid (before its expiration TTL). If it is not valid anymore, DynamoDB will ignore the item, and from your perspective as a client, it will be similar to the experience that the item was already deleted.

UPDATE: Based on the comment below from @Nadav Har'El, it is your responsibility to check the validity of the items using the TTL value ( documentation here ).

The actual deletion or eviction is done by a sweeper that goes over the table periodically. Please also note that the deletion after TTL is a system-delete compared to a standard delete by a delete command from a client. If you are processing the DynamoDB stream you should be aware of that difference. You can read more about TTL and DynamoDB streams here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM