You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/cosmos-db/create-sql-api-java-changefeed.md
+18-90Lines changed: 18 additions & 90 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,24 @@
1
1
---
2
-
title: Tutorial - an end-to-end Async Java SQL API application sample with Change Feed
3
-
description: This tutorial walks you through a simple Java SQL API application which inserts documents into an Azure Cosmos DB container, while maintaining a materialized view of the container using Change Feed.
2
+
title: Create an end-to-end Azure Cosmos DB Java SDK v4 application sample by using Change Feed
3
+
description: This how-to guide walks you through a simple Java SQL API application which inserts documents into an Azure Cosmos DB container, while maintaining a materialized view of the container using Change Feed.
4
4
author: anfeldma
5
5
ms.service: cosmos-db
6
6
ms.subservice: cosmosdb-sql
7
7
ms.devlang: java
8
-
ms.topic: tutorial
9
-
ms.date: 04/01/2020
8
+
ms.topic: conceptual
9
+
ms.date: 05/08/2020
10
10
ms.author: anfeldma
11
11
---
12
12
13
-
# Tutorial - an end-to-end Async Java SQL API application sample with Change Feed
13
+
# How to create a Java application that uses Azure Cosmos DB SQL API and Change Feed Processor
14
14
15
-
This tutorial guide walks you through a simple Java SQL API application which inserts documents into an Azure Cosmos DB container, while maintaining a materialized view of the container using Change Feed.
15
+
> [!IMPORTANT]
16
+
> For more information on Azure Cosmos DB Java SDK v4, please view the Azure Cosmos DB Java SDK v4 Release notes, [Maven repository](https://mvnrepository.com/artifact/com.azure/azure-cosmos), Azure Cosmos DB Java SDK v4 [performance tips](performance-tips-java-sdk-v4-sql.md), and Azure Cosmos DB Java SDK v4 [troubleshooting guide](troubleshoot-java-sdk-v4-sql.md).
17
+
>
16
18
17
-
## Prerequisites
19
+
This how-to guide walks you through a simple Java application which uses the Azure Cosmos DB SQL API to insert documents into an Azure Cosmos DB container, while maintaining a materialized view of the container using Change Feed and Change Feed Processor. The Java application communicates with the Azure Cosmos DB SQL API using Azure Cosmos DB Java SDK v4.
18
20
19
-
* Personal computer
21
+
## Prerequisites
20
22
21
23
* The URI and key for your Azure Cosmos DB account
22
24
@@ -40,8 +42,6 @@ If you have not already done so, clone the app example repo:
> You have a choice to work through this Quickstart with Java SDK 4.0 or Java SDK 3.7.0. **If you would like to use Java SDK 3.7.0, in the terminal type ```git checkout SDK3.7.0```**. Otherwise, stay on the ```master``` branch, which defaults to Java SDK 4.0.
44
-
45
45
Open a terminal in the repo directory. Build the app by running
46
46
47
47
```bash
@@ -50,7 +50,7 @@ mvn clean package
50
50
51
51
## Walkthrough
52
52
53
-
1. As a first check, you should have an Azure Cosmos DB account. Open the **Azure Portal** in your browser, go to your Azure Cosmos DB account, and in the left pane navigate to **Data Explorer**.
53
+
1. As a first check, you should have an Azure Cosmos DB account. Open the **Azure portal** in your browser, go to your Azure Cosmos DB account, and in the left pane navigate to **Data Explorer**.
54
54
55
55

56
56
@@ -66,7 +66,7 @@ mvn clean package
66
66
Press enter to create the grocery store inventory system...
67
67
```
68
68
69
-
thenreturn to the Azure Portal Data Explorer in your browser. You will see a database **GroceryStoreDatabase** has been added with three empty containers:
69
+
thenreturn to the Azure portal Data Explorer in your browser. You will see a database **GroceryStoreDatabase** has been added with three empty containers:
70
70
71
71
***InventoryContainer** - The inventory record for our example grocery store, partitioned on item ```id``` which is a UUID.
72
72
***InventoryContainer-pktype** - A materialized view of the inventory record, optimized for queries over item ```type```
@@ -84,7 +84,7 @@ mvn clean package
84
84
85
85
Press enter. Now the following block of code will execute and initialize the Change Feed processor on another thread:
86
86
87
-
# [Java SDK 4.0](#tab/v4sdk)
87
+
### <a id="java4-connection-policy-async"></a>Java SDK V4 (Maven com.azure::azure-cosmos) Async API
while (!isProcessorRunning.get()); //Wait for Change Feed processor start
113
-
```
114
-
---
115
-
116
101
```"SampleHost_1"``` is the name of the Change Feed processor worker. ```changeFeedProcessorInstance.start()``` is what actually starts the Change Feed processor.
117
102
118
-
Return to the Azure Portal Data Explorer in your browser. Under the **InventoryContainer-leases** container, click **items** to see its contents. You will see that Change Feed Processor has populated the lease container, i.e. the processor has assigned the ```SampleHost_1``` worker a lease on some partitions of the **InventoryContainer**.
103
+
Return to the Azure portal Data Explorer in your browser. Under the **InventoryContainer-leases** container, click **items** to see its contents. You will see that Change Feed Processor has populated the lease container, i.e. the processor has assigned the ```SampleHost_1``` worker a lease on some partitions of the **InventoryContainer**.
1. Press enter again in the terminal. This will trigger 10 documents to be inserted into **InventoryContainer**. Each document insertion appears in the Change Feed as JSON; the following callback code handles these events by mirroring the JSON documents into a materialized view:
123
108
124
-
# [Java SDK 4.0](#tab/v4sdk)
109
+
### <a id="java4-connection-policy-async"></a>Java SDK V4 (Maven com.azure::azure-cosmos) Async API
125
110
126
111
```java
127
112
public static ChangeFeedProcessor getChangeFeedProcessor(String hostName, CosmosAsyncContainer feedContainer, CosmosAsyncContainer leaseContainer) {
@@ -148,77 +133,21 @@ mvn clean package
148
133
}
149
134
```
150
135
151
-
# [Java SDK 3.7.0](#tab/v3sdk)
152
-
153
-
```java
154
-
public static ChangeFeedProcessor getChangeFeedProcessor(String hostName, CosmosContainer feedContainer, CosmosContainer leaseContainer) {
155
-
ChangeFeedProcessorOptions cfOptions = new ChangeFeedProcessorOptions();
1. Allow the code to run 5-10sec. Then return to the Azure Portal Data Explorer and navigate to **InventoryContainer > items**. You should see that items are being inserted into the inventory container; note the partition key (```id```).
136
+
1. Allow the code to run 5-10sec. Then return to the Azure portal Data Explorer and navigate to **InventoryContainer > items**. You should see that items are being inserted into the inventory container; note the partition key (```id```).
1. Now, in Data Explorer navigate to **InventoryContainer-pktype > items**. This is the materialized view - the items in this container mirror **InventoryContainer** because they were inserted programmatically by Change Feed. Note the partition key (```type```). So this materialized view is optimized for queries filtering over ```type```, which would be inefficient on **InventoryContainer** because it is partitioned on ```id```.
1. We're going to delete a document from both **InventoryContainer** and **InventoryContainer-pktype** using just a single ```upsertItem()``` call. First, take a look at Azure Portal Data Explorer. We'll delete the document for which ```/type == "plums"```; it is encircled in red below
144
+
1. We're going to delete a document from both **InventoryContainer** and **InventoryContainer-pktype** using just a single ```upsertItem()``` call. First, take a look at Azure portal Data Explorer. We'll delete the document for which ```/type == "plums"```; it is encircled in red below
Hit enter again to call the function ```deleteDocument()``` in the example code. This function, shown below, upserts a new version of the document with ```/ttl == 5```, which sets document Time-To-Live (TTL) to 5sec.
The Change Feed ```feedPollDelay``` is set to 100ms; therefore, Change Feed responds to this update almost instantly and calls ```updateInventoryTypeMaterializedView()``` shown above. That last functioncall will upsert the new document with TTL of 5sec into **InventoryContainer-pktype**.
0 commit comments