Skip to content

Commit 8c8858a

Browse files
committed
Java SDK v3 and v4 TTL doc
1 parent 6117d52 commit 8c8858a

File tree

1 file changed

+193
-9
lines changed

1 file changed

+193
-9
lines changed

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

Lines changed: 193 additions & 9 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
@@ -70,20 +70,27 @@ await client.GetDatabase("database").CreateContainerAsync(new ContainerPropertie
7070
});
7171
```
7272

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)
7474

7575
```java
76-
private CosmosAsyncContainer container;
76+
CosmosAsyncContainer container;
7777

7878
// 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");
8080
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();
8582
```
8683

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+
```
8794

8895
## Set time to live on a container using SDK
8996

@@ -129,6 +136,28 @@ async function createcontainerWithTTL(db: Database, containerDefinition: Contain
129136
}
130137
```
131138

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+
132161
## Set time to live on an item
133162

134163
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 = {
206235
};
207236
```
208237

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+
209308
## Reset time to live
210309

211310
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
235334
await client.GetContainer("database", "container").ReplaceItemAsync(itemResponse.Resource, "SO05");
236335
```
237336

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+
238368
## Turn off time to live
239369

240370
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
265395
await client.GetContainer("database", "container").ReplaceItemAsync(itemResponse.Resource, "SO05");
266396
```
267397

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+
268429
## Disable time to live
269430

270431
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;
289450
await client.GetContainer("database", "container").ReplaceContainerAsync(containerResponse.Resource);
290451
```
291452

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+
292476
## Next steps
293477

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

0 commit comments

Comments
 (0)