Skip to content

Commit 83cb5f9

Browse files
Merge pull request #114482 from anfeldma-ms/JavaCFTutorialBecomesHowTo
Java Change Feed tutorial becomes a how to guide
2 parents 9c1922d + e5be6e3 commit 83cb5f9

File tree

2 files changed

+20
-92
lines changed

2 files changed

+20
-92
lines changed

articles/cosmos-db/TOC.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
href: sql-api-java-get-started.md
4040
- name: Async Java
4141
href: sql-api-async-java-get-started.md
42-
- name: Async Java sample with change feed
43-
href: create-sql-api-java-changefeed.md
4442
- name: Node.js
4543
href: sql-api-nodejs-get-started.md
4644
- name: Build a web app
@@ -1020,6 +1018,8 @@
10201018
href: how-to-use-change-feed-estimator.md
10211019
- name: Migrate from change feed processor library
10221020
href: how-to-migrate-from-change-feed-library.md
1021+
- name: Create a Java SQL application with Change Feed
1022+
href: create-sql-api-java-changefeed.md
10231023
- name: Built-in analytics with Apache Spark
10241024
items:
10251025
- name: Azure Databricks Spark connector

articles/cosmos-db/create-sql-api-java-changefeed.md

Lines changed: 18 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
---
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.
44
author: anfeldma
55
ms.service: cosmos-db
66
ms.subservice: cosmosdb-sql
77
ms.devlang: java
8-
ms.topic: tutorial
9-
ms.date: 04/01/2020
8+
ms.topic: conceptual
9+
ms.date: 05/08/2020
1010
ms.author: anfeldma
1111
---
1212

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
1414

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+
>
1618
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.
1820

19-
* Personal computer
21+
## Prerequisites
2022

2123
* The URI and key for your Azure Cosmos DB account
2224

@@ -40,8 +42,6 @@ If you have not already done so, clone the app example repo:
4042
git clone https://github.com/Azure-Samples/azure-cosmos-java-sql-app-example.git
4143
```
4244

43-
> 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-
4545
Open a terminal in the repo directory. Build the app by running
4646

4747
```bash
@@ -50,7 +50,7 @@ mvn clean package
5050

5151
## Walkthrough
5252

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**.
5454

5555
![Azure Cosmos DB account](media/create-sql-api-java-changefeed/cosmos_account_empty.JPG)
5656

@@ -66,7 +66,7 @@ mvn clean package
6666
Press enter to create the grocery store inventory system...
6767
```
6868

69-
then return to the Azure Portal Data Explorer in your browser. You will see a database **GroceryStoreDatabase** has been added with three empty containers:
69+
then return to the Azure portal Data Explorer in your browser. You will see a database **GroceryStoreDatabase** has been added with three empty containers:
7070

7171
* **InventoryContainer** - The inventory record for our example grocery store, partitioned on item ```id``` which is a UUID.
7272
* **InventoryContainer-pktype** - A materialized view of the inventory record, optimized for queries over item ```type```
@@ -84,7 +84,7 @@ mvn clean package
8484

8585
Press enter. Now the following block of code will execute and initialize the Change Feed processor on another thread:
8686

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
8888

8989
```java
9090
changeFeedProcessorInstance = getChangeFeedProcessor("SampleHost_1", feedContainer, leaseContainer);
@@ -98,30 +98,15 @@ mvn clean package
9898
while (!isProcessorRunning.get()); //Wait for Change Feed processor start
9999
```
100100
101-
# [Java SDK 3.7.0](#tab/v3sdk)
102-
103-
```java
104-
changeFeedProcessorInstance = getChangeFeedProcessor("SampleHost_1", feedContainer, leaseContainer);
105-
changeFeedProcessorInstance.start()
106-
.subscribeOn(Schedulers.elastic())
107-
.doOnSuccess(aVoid -> {
108-
isProcessorRunning.set(true);
109-
})
110-
.subscribe();
111-
112-
while (!isProcessorRunning.get()); //Wait for Change Feed processor start
113-
```
114-
---
115-
116101
```"SampleHost_1"``` is the name of the Change Feed processor worker. ```changeFeedProcessorInstance.start()``` is what actually starts the Change Feed processor.
117102
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**.
119104
120105
![Leases](media/create-sql-api-java-changefeed/cosmos_leases.JPG)
121106
122107
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:
123108
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
125110
126111
```java
127112
public static ChangeFeedProcessor getChangeFeedProcessor(String hostName, CosmosAsyncContainer feedContainer, CosmosAsyncContainer leaseContainer) {
@@ -148,77 +133,21 @@ mvn clean package
148133
}
149134
```
150135
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();
156-
cfOptions.feedPollDelay(Duration.ofMillis(100));
157-
cfOptions.startFromBeginning(true);
158-
return ChangeFeedProcessor.Builder()
159-
.options(cfOptions)
160-
.hostName(hostName)
161-
.feedContainer(feedContainer)
162-
.leaseContainer(leaseContainer)
163-
.handleChanges((List<CosmosItemProperties> docs) -> {
164-
for (CosmosItemProperties document : docs) {
165-
//Duplicate each document update from the feed container into the materialized view container
166-
updateInventoryTypeMaterializedView(document);
167-
}
168-
169-
})
170-
.build();
171-
}
172-
173-
private static void updateInventoryTypeMaterializedView(CosmosItemProperties document) {
174-
typeContainer.upsertItem(document).subscribe();
175-
}
176-
```
177-
---
178-
179-
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```).
180137
181138
![Feed container](media/create-sql-api-java-changefeed/cosmos_items.JPG)
182139
183140
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```.
184141
185142
![Materialized view](media/create-sql-api-java-changefeed/cosmos_materializedview2.JPG)
186143
187-
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
188145
189146
![Materialized view](media/create-sql-api-java-changefeed/cosmos_materializedview-emph-todelete.JPG)
190147
191148
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.
192149
193-
# [Java SDK 4.0](#tab/v4sdk)
194-
195-
```java
196-
public static void deleteDocument() {
197-
198-
String jsonString = "{\"id\" : \"" + idToDelete + "\""
199-
+ ","
200-
+ "\"brand\" : \"Jerry's\""
201-
+ ","
202-
+ "\"type\" : \"plums\""
203-
+ ","
204-
+ "\"quantity\" : \"50\""
205-
+ ","
206-
+ "\"ttl\" : 5"
207-
+ "}";
208-
209-
ObjectMapper mapper = new ObjectMapper();
210-
JsonNode document = null;
211-
212-
try {
213-
document = mapper.readTree(jsonString);
214-
} catch (Exception e) {
215-
e.printStackTrace();
216-
}
217-
218-
feedContainer.upsertItem(document,new CosmosItemRequestOptions()).block();
219-
}
220-
```
221-
# [Java SDK 3.7.0](#tab/v3sdk)
150+
### <a id="java4-connection-policy-async"></a>Java SDK V4 (Maven com.azure::azure-cosmos) Async API
222151
223152
```java
224153
public static void deleteDocument() {
@@ -246,7 +175,6 @@ mvn clean package
246175
feedContainer.upsertItem(document,new CosmosItemRequestOptions()).block();
247176
}
248177
```
249-
---
250178
251179
The Change Feed ```feedPollDelay``` is set to 100ms; therefore, Change Feed responds to this update almost instantly and calls ```updateInventoryTypeMaterializedView()``` shown above. That last function call will upsert the new document with TTL of 5sec into **InventoryContainer-pktype**.
252180

0 commit comments

Comments
 (0)