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