Created
December 5, 2024 22:05
-
-
Save seesharprun/fc563ad605bacffba2b47aafa2e49c20 to your computer and use it in GitHub Desktop.
Bulk & Stream support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net9.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Azure.Identity" Version="1.13.1" /> | |
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.46.0" /> | |
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> | |
</ItemGroup> | |
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"id": "0001", | |
"name": "Sample Person" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Azure.Core; | |
using Azure.Identity; | |
using Microsoft.Azure.Cosmos; | |
using Microsoft.Azure.Cosmos.Fluent; | |
string endpoint = "<nosql-account-endpoint>"; | |
TokenCredential credential = new DefaultAzureCredential(); | |
CosmosClient client = new CosmosClientBuilder(endpoint, credential) | |
.WithBulkExecution(true) | |
.Build(); | |
Container container = client.GetContainer("<database-name>", "<container-name>"); | |
List<FileInfo> files = | |
[ | |
new FileInfo("first.json"), | |
new FileInfo("second.json"), | |
new FileInfo("third.json") | |
]; | |
List<Task> tasks = new List<Task>(); | |
foreach (FileInfo file in files) | |
{ | |
tasks.Add(Task.Run(async () => | |
{ | |
using Stream stream = File.OpenRead(file.FullName); | |
ResponseMessage response = await container.UpsertItemStreamAsync(stream, PartitionKey.None); | |
if (response.IsSuccessStatusCode) | |
{ | |
Console.WriteLine($"Uploaded {file.Name}"); | |
} | |
})); | |
} | |
await Task.WhenAll(tasks); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"id": "0002", | |
"name": { | |
"first": "Sample", | |
"last": "Person" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"id": "0003", | |
"name": [ | |
"Sample", | |
"Person" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment