Skip to content

Commit a989300

Browse files
authored
Merge pull request #109165 from ealsur/users/ealsur/bulkv3
Cosmos DB - Bulk executor library migration document
2 parents 8b7991d + 5e2448c commit a989300

File tree

3 files changed

+94
-5
lines changed

3 files changed

+94
-5
lines changed

articles/cosmos-db/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,8 @@
11091109
href: bulk-executor-overview.md
11101110
- name: Bulk executor - .NET library
11111111
href: bulk-executor-dot-net.md
1112+
- name: Migrate from bulk executor to .NET SDK
1113+
href: how-to-migrate-from-bulk-executor-library.md
11121114
- name: Bulk executor - Java library
11131115
href: bulk-executor-java.md
11141116
- name: Reference

articles/cosmos-db/bulk-executor-dot-net.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,25 @@ ms.service: cosmos-db
66
ms.subservice: cosmosdb-sql
77
ms.devlang: dotnet
88
ms.topic: conceptual
9-
ms.date: 09/01/2019
9+
ms.date: 03/23/2020
1010
ms.author: ramkris
1111
ms.reviewer: sngun
1212
---
1313

1414
# Use the bulk executor .NET library to perform bulk operations in Azure Cosmos DB
1515

16+
> [!NOTE]
17+
> This bulk executor library described in this article is maintained for applications using the .NET SDK 2.x version. For new applications, you can use the **bulk support** that is directly available with the [.NET SDK version 3.x](tutorial-sql-api-dotnet-bulk-import.md) and it does not require any external library.
18+
19+
> If you are currently using the bulk executor library and planning to migrate to bulk support on the newer SDK, use the steps in the [Migration guide](how-to-migrate-from-bulk-executor-library.md) to migrate your application.
20+
1621
This tutorial provides instructions on using the bulk executor .NET library to import and update documents to an Azure Cosmos container. To learn about the bulk executor library and how it helps you leverage massive throughput and storage, see the [bulk executor library overview](bulk-executor-overview.md) article. In this tutorial, you will see a sample .NET application that bulk imports randomly generated documents into an Azure Cosmos container. After importing, it shows you how you can bulk update the imported data by specifying patches as operations to perform on specific document fields.
1722

1823
Currently, bulk executor library is supported by the Azure Cosmos DB SQL API and Gremlin API accounts only. This article describes how to use the bulk executor .NET library with SQL API accounts. To learn about using the bulk executor .NET library with Gremlin API accounts, see [perform bulk operations in the Azure Cosmos DB Gremlin API](bulk-executor-graph-dotnet.md).
1924

2025
## Prerequisites
2126

22-
* If you dont already have Visual Studio 2019 installed, you can download and use the [Visual Studio 2019 Community Edition](https://www.visualstudio.com/downloads/). Make sure that you enable "Azure development" during the Visual Studio setup.
27+
* If you don't already have Visual Studio 2019 installed, you can download and use the [Visual Studio 2019 Community Edition](https://www.visualstudio.com/downloads/). Make sure that you enable "Azure development" during the Visual Studio setup.
2328

2429
* If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio) before you begin.
2530

@@ -35,15 +40,15 @@ Now let's switch to working with code by downloading a sample .NET application f
3540
git clone https://github.com/Azure/azure-cosmosdb-bulkexecutor-dotnet-getting-started.git
3641
```
3742

38-
The cloned repository contains two samples "BulkImportSample" and "BulkUpdateSample". You can open either of the sample applications, update the connection strings in App.config file with your Azure Cosmos DB accounts connection strings, build the solution, and run it.
43+
The cloned repository contains two samples "BulkImportSample" and "BulkUpdateSample". You can open either of the sample applications, update the connection strings in App.config file with your Azure Cosmos DB account's connection strings, build the solution, and run it.
3944

4045
The "BulkImportSample" application generates random documents and bulk imports them to your Azure Cosmos account. The "BulkUpdateSample" application bulk updates the imported documents by specifying patches as operations to perform on specific document fields. In the next sections, you will review the code in each of these sample apps.
4146

4247
## Bulk import data to an Azure Cosmos account
4348

4449
1. Navigate to the "BulkImportSample" folder and open the "BulkImportSample.sln" file.
4550

46-
2. The Azure Cosmos DBs connection strings are retrieved from the App.config file as shown in the following code:
51+
2. The Azure Cosmos DB's connection strings are retrieved from the App.config file as shown in the following code:
4752

4853
```csharp
4954
private static readonly string EndpointUrl = ConfigurationManager.AppSettings["EndPointUrl"];
@@ -161,7 +166,7 @@ You can update existing documents by using the BulkUpdateAsync API. In this exam
161166
|NumberOfDocumentsUpdated (long) | The number of documents that were successfully updated out of the total documents supplied to the bulk update API call. |
162167
|TotalRequestUnitsConsumed (double) | The total request units (RUs) consumed by the bulk update API call. |
163168
|TotalTimeTaken (TimeSpan) | The total time taken by the bulk update API call to complete the execution. |
164-
169+
165170
## Performance tips
166171

167172
Consider the following points for better performance when using the bulk executor library:
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
title: Migrate from the bulk executor library to the bulk support in Azure Cosmos DB .NET V3 SDK
3+
description: Learn how to migrate your application from using the bulk executor library to the bulk support in Azure Cosmos DB SDK V3
4+
author: ealsur
5+
ms.service: cosmos-db
6+
ms.topic: conceptual
7+
ms.date: 03/24/2020
8+
ms.author: maquaran
9+
---
10+
11+
# Migrate from the bulk executor library to the bulk support in Azure Cosmos DB .NET V3 SDK
12+
13+
This article describes the required steps to migrate an existing application's code that uses the [.NET bulk executor library](bulk-executor-dot-net.md) to the [bulk support](tutorial-sql-api-dotnet-bulk-import.md) feature in the latest version of the .NET SDK.
14+
15+
## Enable bulk support
16+
17+
Enable bulk support on the `CosmosClient` instance through the [AllowBulkExecution](https://docs.microsoft.com/dotnet/api/microsoft.azure.cosmos.cosmosclientoptions.allowbulkexecution) configuration:
18+
19+
:::code language="csharp" source="~/samples-cosmosdb-dotnet-v3/Microsoft.Azure.Cosmos.Samples/Usage/BulkExecutorMigration/Program.cs" ID="Initialization":::
20+
21+
## Create Tasks for each operation
22+
23+
Bulk support in the .NET SDK works by leveraging the [Task Parallel Library](https://docs.microsoft.com/dotnet/standard/parallel-programming/task-parallel-library-tpl) and grouping operations that occur concurrently.
24+
25+
There is no single method that will take your list of documents or operations as an input parameter, but rather, you need to create a Task for each operation you want to execute in bulk.
26+
27+
For example, if your initial input is a list of items where each item has the following schema:
28+
29+
:::code language="csharp" source="~/samples-cosmosdb-dotnet-v3/Microsoft.Azure.Cosmos.Samples/Usage/BulkExecutorMigration/Program.cs" ID="Model":::
30+
31+
If you want to do bulk import (similar to using BulkExecutor.BulkImportAsync), you need to have concurrent calls to `CreateItemAsync` with each item value. For example:
32+
33+
:::code language="csharp" source="~/samples-cosmosdb-dotnet-v3/Microsoft.Azure.Cosmos.Samples/Usage/BulkExecutorMigration/Program.cs" ID="BulkImport":::
34+
35+
If you want to do bulk *update* (similar to using [BulkExecutor.BulkUpdateAsync](https://docs.microsoft.com/dotnet/api/microsoft.azure.cosmosdb.bulkexecutor.bulkexecutor.bulkupdateasync)), you need to have concurrent calls to `ReplaceItemAsync` method after updating the item value. For example:
36+
37+
:::code language="csharp" source="~/samples-cosmosdb-dotnet-v3/Microsoft.Azure.Cosmos.Samples/Usage/BulkExecutorMigration/Program.cs" ID="BulkUpdate":::
38+
39+
And if you want to do bulk *delete* (similar to using [BulkExecutor.BulkDeleteAsync](https://docs.microsoft.com/dotnet/api/microsoft.azure.cosmosdb.bulkexecutor.bulkexecutor.bulkdeleteasync)), you need to have concurrent calls to `DeleteItemAsync`, with the `id` and partition key of each item. For example:
40+
41+
:::code language="csharp" source="~/samples-cosmosdb-dotnet-v3/Microsoft.Azure.Cosmos.Samples/Usage/BulkExecutorMigration/Program.cs" ID="BulkDelete":::
42+
43+
## Capture task result state
44+
45+
In the previous code examples, you have created a concurrent list of tasks, and called the `CaptureOperationResponse` method on each of those tasks. This method is an extension that lets us maintain a *similar response schema* as BulkExecutor, by capturing any errors and tracking the [request units usage](request-units.md).
46+
47+
:::code language="csharp" source="~/samples-cosmosdb-dotnet-v3/Microsoft.Azure.Cosmos.Samples/Usage/BulkExecutorMigration/Program.cs" ID="CaptureOperationResult":::
48+
49+
Where the `OperationResponse` is declared as:
50+
51+
:::code language="csharp" source="~/samples-cosmosdb-dotnet-v3/Microsoft.Azure.Cosmos.Samples/Usage/BulkExecutorMigration/Program.cs" ID="OperationResult":::
52+
53+
## Execute operations concurrently
54+
55+
After the list of tasks are defined, wait until they are all complete. You can track the completion of the tasks by defining the scope of your bulk operation as shown in the following code snippet:
56+
57+
:::code language="csharp" source="~/samples-cosmosdb-dotnet-v3/Microsoft.Azure.Cosmos.Samples/Usage/BulkExecutorMigration/Program.cs" ID="WhenAll":::
58+
59+
## Capture statistics
60+
61+
The previous code waits until all operations are completed and calculates the required statistics. These statistics are similar to that of the bulk executor library's [BulkImportResponse](https://docs.microsoft.com/dotnet/api/microsoft.azure.cosmosdb.bulkexecutor.bulkimport.bulkimportresponse).
62+
63+
:::code language="csharp" source="~/samples-cosmosdb-dotnet-v3/Microsoft.Azure.Cosmos.Samples/Usage/BulkExecutorMigration/Program.cs" ID="ResponseType":::
64+
65+
The `BulkOperationResponse` contains:
66+
67+
1. The total time taken to process the list of operations through bulk support.
68+
1. The number of successful operations.
69+
1. The total of request units consumed.
70+
1. If there are failures, it displays a list of tuples that contain the exception and the associated item for logging and identification purpose.
71+
72+
## Performance improvements
73+
74+
As with other operations with the .NET SDK, using the stream APIs results in better performance and avoids any unnecessary serialization.
75+
76+
Using stream APIs is only possible if the nature of the data you use matches that of a stream of bytes (for example, file streams). In such cases, using the `CreateItemStreamAsync`, `ReplaceItemStreamAsync`, or `DeleteItemStreamAsync` methods and working with `ResponseMessage` (instead of `ItemResponse`) increases the throughput that can be achieved.
77+
78+
## Next steps
79+
80+
* To learn more about the .NET SDK releases, see the [Azure Cosmos DB SDK](sql-api-sdk-dotnet.md) article.
81+
* Get the complete [migration source code](https://github.com/Azure/azure-cosmos-dotnet-v3/tree/master/Microsoft.Azure.Cosmos.Samples/Usage/BulkExecutorMigration) from GitHub.
82+
* [Additional bulk samples on GitHub](https://github.com/Azure/azure-cosmos-dotnet-v3/tree/master/Microsoft.Azure.Cosmos.Samples/Usage/BulkSupport)

0 commit comments

Comments
 (0)