@@ -4,8 +4,8 @@ description: Learn how to configure and manage time to live on a container and a
4
4
author : markjbrown
5
5
ms.service : cosmos-db
6
6
ms.topic : conceptual
7
- ms.date : 12/02/2019
8
- ms.author : mjbrown
7
+ ms.date : 03/27/2020
8
+ ms.author : anfeldma
9
9
---
10
10
11
11
# Configure time to live in Azure Cosmos DB
@@ -70,20 +70,27 @@ await client.GetDatabase("database").CreateContainerAsync(new ContainerPropertie
70
70
});
71
71
```
72
72
73
- ### <a id =" dotnet -enable-noexpiry" ></a >Java SDK V4 (com.azure. azure-cosmos)
73
+ ### <a id =" java -enable-noexpiry" ></a >Java SDK V4 (Maven com.azure:: azure-cosmos)
74
74
75
75
``` java
76
- private CosmosAsyncContainer container;
76
+ CosmosAsyncContainer container;
77
77
78
78
// Create a new container with TTL enabled and without any expiration value
79
- CosmosContainerProperties containerProperties = new CosmosContainerProperties (containerName , " /lastName " );
79
+ CosmosContainerProperties containerProperties = new CosmosContainerProperties (" myContainer " , " /myPartitionKey " );
80
80
containerProperties. setDefaultTimeToLiveInSeconds(- 1 );
81
- database. createContainerIfNotExists(containerProperties, 400 ). flatMap(containerResponse - > {
82
- container = containerResponse. getContainer();
83
- return Mono . empty();
84
- }). block();
81
+ container = database. createContainerIfNotExists(containerProperties, 400 ). block(). getContainer();
85
82
```
86
83
84
+ ### <a id =" java-enable-noexpiry " ></a >Java SDK V3 (Maven com.microsoft.azure::azure-cosmos)
85
+
86
+ ``` java
87
+ CosmosContainer container;
88
+
89
+ // Create a new container with TTL enabled and without any expiration value
90
+ CosmosContainerProperties containerProperties = new CosmosContainerProperties (" myContainer" , " /myPartitionKey" );
91
+ containerProperties. defaultTimeToLive(- 1 );
92
+ container = database. createContainerIfNotExists(containerProperties, 400 ). block(). container();
93
+ ```
87
94
88
95
## Set time to live on a container using SDK
89
96
@@ -129,6 +136,28 @@ async function createcontainerWithTTL(db: Database, containerDefinition: Contain
129
136
}
130
137
```
131
138
139
+ ### <a id="java-enable-noexpiry"></a>Java SDK V4 (Maven com.azure::azure-cosmos)
140
+
141
+ ```java
142
+ CosmosAsyncContainer container ;
143
+
144
+ // Create a new container with TTL enabled and without any expiration value
145
+ CosmosContainerProperties containerProperties = new CosmosContainerProperties (" myContainer" , " /myPartitionKey" );
146
+ containerProperties .setDefaultTimeToLiveInSeconds (90 * 60 * 60 * 24 );
147
+ container = database .createContainerIfNotExists (containerProperties , 400 ).block ().getContainer ();
148
+ ```
149
+
150
+ ### <a id="java-enable-noexpiry"></a>Java SDK V3 (Maven com.microsoft.azure::azure-cosmos)
151
+
152
+ ```java
153
+ CosmosContainer container ;
154
+
155
+ // Create a new container with TTL enabled and without any expiration value
156
+ CosmosContainerProperties containerProperties = new CosmosContainerProperties (" myContainer" , " /myPartitionKey" );
157
+ containerProperties .defaultTimeToLive (90 * 60 * 60 * 24 );
158
+ container = database .createContainerIfNotExists (containerProperties , 400 ).block ().container ();
159
+ ```
160
+
132
161
## Set time to live on an item
133
162
134
163
In addition to setting a default time to live on a container , you can set a time to live for an item . Setting time to live at the item level will override the default TTL of the item in that container .
@@ -206,6 +235,76 @@ const itemDefinition = {
206
235
};
207
236
```
208
237
238
+ ### <a id="java-enable-noexpiry"></a>Java SDK V4 (Maven com.azure::azure-cosmos)
239
+
240
+ ```java
241
+ // Include a property that serializes to "ttl" in JSON
242
+ public class SalesOrder
243
+ {
244
+ private String id ;
245
+ private String customerId ;
246
+ private Integer ttl ;
247
+
248
+ public SalesOrder (String id , String customerId , Integer ttl ) {
249
+ this .id = id ;
250
+ this .customerId = customerId ;
251
+ this .ttl = ttl ;
252
+ }
253
+
254
+ public String getId () {return this .id ;}
255
+ public void setId (String new_id ) {this .id = new_id ;}
256
+ public String getCustomerId () {return this .customerId ;}
257
+ public void setCustomerId (String new_cid ) {this .customerId = new_cid ;}
258
+ public Integer getTtl () {return this .ttl ;}
259
+ public void setTtl (Integer new_ttl ) {this .ttl = new_ttl ;}
260
+
261
+ // ...
262
+ }
263
+
264
+ // Set the value to the expiration in seconds
265
+ SalesOrder salesOrder = new SalesOrder (
266
+ " SO05" ,
267
+ " CO18009186470" ,
268
+ 60 * 60 * 24 * 30 // Expire sales orders in 30 days
269
+ );
270
+
271
+ ```
272
+
273
+ ### <a id="java-enable-noexpiry"></a>Java SDK V3 (Maven com.microsoft.azure::azure-cosmos)
274
+
275
+ ```java
276
+ // Include a property that serializes to "ttl" in JSON
277
+ public class SalesOrder
278
+ {
279
+ private String id ;
280
+ private String customerId ;
281
+ private Integer ttl ;
282
+
283
+ public SalesOrder (String id , String customerId , Integer ttl ) {
284
+ this .id = id ;
285
+ this .customerId = customerId ;
286
+ this .ttl = ttl ;
287
+ }
288
+
289
+ public String id () {return this .id ;}
290
+ public void id (String new_id ) {this .id = new_id ;}
291
+ public String customerId () {return this .customerId ;}
292
+ public void customerId (String new_cid ) {this .customerId = new_cid ;}
293
+ public Integer ttl () {return this .ttl ;}
294
+ public void ttl (Integer new_ttl ) {this .ttl = new_ttl ;}
295
+
296
+ // ...
297
+ }
298
+
299
+ // Set the value to the expiration in seconds
300
+ SalesOrder salesOrder = new SalesOrder (
301
+ " SO05" ,
302
+ " CO18009186470" ,
303
+ 60 * 60 * 24 * 30 // Expire sales orders in 30 days
304
+ );
305
+
306
+ ```
307
+
209
308
## Reset time to live
210
309
211
310
You can reset the time to live on an item by performing a write or update operation on the item . The write or update operation will set the `_ts ` to the current time , and the TTL for the item to expire will begin again . If you wish to change the TTL of an item , you can update the field just as you update any other field .
@@ -235,6 +334,37 @@ itemResponse.Resource.ttl = 60 * 30 * 30; // update time to live
235
334
await client .GetContainer (" database" , " container" ).ReplaceItemAsync (itemResponse .Resource , " SO05" );
236
335
```
237
336
337
+ ### <a id="java-enable-noexpiry"></a>Java SDK V4 (Maven com.azure::azure-cosmos)
338
+
339
+ ```java
340
+ // This examples leverages the Sales Order class above.
341
+ // Read a document, update its TTL, save it.
342
+ CosmosAsyncItemResponse < SalesOrder > itemResponse = container .readItem (" SO05" , new PartitionKey (" CO18009186470" ), SalesOrder .class )
343
+ .flatMap (readResponse -> {
344
+ SalesOrder salesOrder = readResponse .getItem ();
345
+ salesOrder .setTtl (60 * 30 * 30 );
346
+ return container .createItem (salesOrder );
347
+ }).block ();
348
+ ```
349
+
350
+ ### <a id="java-enable-noexpiry"></a>Java SDK V3 (Maven com.microsoft.azure::azure-cosmos)
351
+
352
+ ```java
353
+ // This examples leverages the Sales Order class above.
354
+ // Read a document, update its TTL, save it.
355
+ container .getItem (" SO05" , new PartitionKey (" CO18009186470" )).read ()
356
+ .flatMap (readResponse -> {
357
+ SalesOrder salesOrder = null ;
358
+ try {
359
+ salesOrder = readResponse .properties ().getObject (SalesOrder .class );
360
+ } catch (Exception err ) {
361
+
362
+ }
363
+ salesOrder .ttl (60 * 30 * 30 );
364
+ return container .createItem (salesOrder );
365
+ }).block ();
366
+ ```
367
+
238
368
## Turn off time to live
239
369
240
370
If time to live has been set on an item and you no longer want that item to expire , then you can get the item , remove the TTL field , and replace the item on the server . When the TTL field is removed from the item , the default TTL value assigned to the container is applied to the item . Set the TTL value to -1 to prevent an item from expiring and to not inherit the TTL value from the container .
@@ -265,6 +395,37 @@ itemResponse.Resource.ttl = null; // inherit the default TTL of the container
265
395
await client .GetContainer (" database" , " container" ).ReplaceItemAsync (itemResponse .Resource , " SO05" );
266
396
```
267
397
398
+ ### <a id =" java-enable-noexpiry " ></a >Java SDK V4 (Maven com.azure::azure-cosmos)
399
+
400
+ ``` java
401
+ // This examples leverages the Sales Order class above.
402
+ // Read a document, update its TTL, save it.
403
+ CosmosAsyncItemResponse<SalesOrder > itemResponse = container. readItem(" SO05" , new PartitionKey (" CO18009186470" ), SalesOrder . class)
404
+ .flatMap(readResponse - > {
405
+ SalesOrder salesOrder = readResponse. getItem();
406
+ salesOrder. setTtl(null );
407
+ return container. createItem(salesOrder);
408
+ }). block();
409
+ ```
410
+
411
+ ### <a id =" java-enable-noexpiry " ></a >Java SDK V3 (Maven com.microsoft.azure::azure-cosmos)
412
+
413
+ ``` java
414
+ // This examples leverages the Sales Order class above.
415
+ // Read a document, update its TTL, save it.
416
+ container. getItem(" SO05" , new PartitionKey (" CO18009186470" )). read()
417
+ .flatMap(readResponse - > {
418
+ SalesOrder salesOrder = null ;
419
+ try {
420
+ salesOrder = readResponse. properties(). getObject(SalesOrder . class);
421
+ } catch (Exception err) {
422
+
423
+ }
424
+ salesOrder. ttl(null );
425
+ return container. createItem(salesOrder);
426
+ }). block();
427
+ ```
428
+
268
429
## Disable time to live
269
430
270
431
To disable time to live on a container and stop the background process from checking for expired items, the ` DefaultTimeToLive ` property on the container should be deleted. Deleting this property is different from setting it to -1. When you set it to -1, new items added to the container will live forever, however you can override this value on specific items in the container. When you remove the TTL property from the container the items will never expire, even if there are they have explicitly overridden the previous default TTL value.
@@ -289,6 +450,29 @@ containerResponse.Resource.DefaultTimeToLive = null;
289
450
await client .GetContainer (" database" , " container" ).ReplaceContainerAsync (containerResponse .Resource );
290
451
```
291
452
453
+ ### <a id =" java-enable-noexpiry " ></a >Java SDK V4 (Maven com.azure::azure-cosmos)
454
+
455
+ ``` java
456
+ CosmosContainerProperties containerProperties = new CosmosContainerProperties (" myContainer" , " /myPartitionKey" );
457
+ // Disable TTL
458
+ containerProperties. setDefaultTimeToLiveInSeconds(null );
459
+ // Update container settings
460
+ container. replace(containerProperties). block();
461
+ ```
462
+
463
+ ### <a id =" java-enable-noexpiry " ></a >Java SDK V3 (Maven com.microsoft.azure::azure-cosmos)
464
+
465
+ ``` java
466
+ CosmosContainer container;
467
+
468
+ // Create a new container with TTL enabled and without any expiration value
469
+ CosmosContainerProperties containerProperties = new CosmosContainerProperties (" myContainer" , " /myPartitionKey" );
470
+ // Disable TTL
471
+ containerProperties. defaultTimeToLive(null );
472
+ // Update container settings
473
+ container = database. createContainerIfNotExists(containerProperties, 400 ). block(). container();
474
+ ```
475
+
292
476
## Next steps
293
477
294
478
Learn more about time to live in the following article:
0 commit comments