Skip to content

Commit ff35ef6

Browse files
Merge pull request #109430 from anfeldma-ms/CosmosDBJavaTTL
Update TTL how-to doc with Java SDK v3 v4
2 parents 55602ed + 3f91f4d commit ff35ef6

File tree

1 file changed

+202
-3
lines changed

1 file changed

+202
-3
lines changed

articles/cosmos-db/how-to-time-to-live.md

Lines changed: 202 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ description: Learn how to configure and manage time to live on a container and a
44
author: markjbrown
55
ms.service: cosmos-db
66
ms.topic: conceptual
7-
ms.date: 12/02/2019
8-
ms.author: mjbrown
7+
ms.date: 03/27/2020
8+
ms.author: anfeldma
99
---
1010

1111
# Configure time to live in Azure Cosmos DB
@@ -40,7 +40,7 @@ Use the following steps to enable time to live on a container with no expiration
4040
To create or enable TTL on a container see,
4141

4242
* [Create a container with TTL using Azure CLI](manage-with-cli.md#create-a-container-with-ttl)
43-
* [Create a container with TTL using Powershell](manage-with-powershell.md#create-container-unique-key-ttl)
43+
* [Create a container with TTL using PowerShell](manage-with-powershell.md#create-container-unique-key-ttl)
4444

4545
## Enable time to live on a container using SDK
4646

@@ -70,6 +70,28 @@ await client.GetDatabase("database").CreateContainerAsync(new ContainerPropertie
7070
});
7171
```
7272

73+
### <a id="java4-enable-noexpiry"></a>Java SDK V4 (Maven com.azure::azure-cosmos)
74+
75+
```java
76+
CosmosAsyncContainer container;
77+
78+
// Create a new container with TTL enabled and without any expiration value
79+
CosmosContainerProperties containerProperties = new CosmosContainerProperties("myContainer", "/myPartitionKey");
80+
containerProperties.setDefaultTimeToLiveInSeconds(-1);
81+
container = database.createContainerIfNotExists(containerProperties, 400).block().getContainer();
82+
```
83+
84+
### <a id="java3-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+
```
94+
7395
## Set time to live on a container using SDK
7496

7597
To set the time to live on a container, you need to provide a non-zero positive number that indicates the time period in seconds. Based on the configured TTL value, all items in the container after the last modified timestamp of the item `_ts` are deleted.
@@ -114,6 +136,28 @@ async function createcontainerWithTTL(db: Database, containerDefinition: Contain
114136
}
115137
```
116138

139+
### <a id="java4-enable-defaultexpiry"></a>Java SDK V4 (Maven com.azure::azure-cosmos)
140+
141+
```java
142+
CosmosAsyncContainer container;
143+
144+
// Create a new container with TTL enabled with default 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="java3-enable-defaultexpiry"></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 with default 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+
117161
## Set time to live on an item
118162

119163
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.
@@ -191,6 +235,76 @@ const itemDefinition = {
191235
};
192236
```
193237

238+
### <a id="java4-enable-itemexpiry"></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="java3-enable-itemexpiry"></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+
194308
## Reset time to live
195309

196310
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.
@@ -220,6 +334,37 @@ itemResponse.Resource.ttl = 60 * 30 * 30; // update time to live
220334
await client.GetContainer("database", "container").ReplaceItemAsync(itemResponse.Resource, "SO05");
221335
```
222336

337+
### <a id="java4-enable-modifyitemexpiry"></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="java3-enable-modifyitemexpiry"></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+
223368
## Turn off time to live
224369

225370
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.
@@ -250,6 +395,37 @@ itemResponse.Resource.ttl = null; // inherit the default TTL of the container
250395
await client.GetContainer("database", "container").ReplaceItemAsync(itemResponse.Resource, "SO05");
251396
```
252397

398+
### <a id="java4-enable-itemdefaultexpiry"></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="java3-enable-itemdefaultexpiry"></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+
253429
## Disable time to live
254430

255431
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.
@@ -274,6 +450,29 @@ containerResponse.Resource.DefaultTimeToLive = null;
274450
await client.GetContainer("database", "container").ReplaceContainerAsync(containerResponse.Resource);
275451
```
276452

453+
### <a id="java4-enable-disableexpiry"></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="java3-enable-disableexpiry"></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+
277476
## Next steps
278477

279478
Learn more about time to live in the following article:

0 commit comments

Comments
 (0)