This file contains 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 (batchResponse) | |
{ | |
if (batchResponse.IsSuccessStatusCode) | |
{ | |
TransactionalBatchOperationResult<ParentClass> parentResult = batchResponse.GetOperationResultAtIndex<ParentClass>(0); | |
ParentClass parentClassResult = parentResult.Resource; | |
TransactionalBatchOperationResult<ChildClass> childResult = batchResponse.GetOperationResultAtIndex<ChildClass>(1); | |
ChildClass childClassResult = childResult.Resource; | |
} | |
} |
This file contains 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
TransactionalBatchResponse batchResponse = await batch.ExecuteAsync(); |
This file contains 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
string partitionKey = "The Family"; | |
ParentClass parent = new ParentClass(){ Id = "The Parent", PartitionKey = partitionKey, Name = "John", Age = 30 }; | |
ChildClass child = new ChildClass(){ Id = "The Child", ParentId = parent.Id, PartitionKey = partitionKey }; | |
TransactionalBatch batch = container.CreateTransactionalBatch(new PartitionKey(parent.PartitionKey)) | |
.CreateItem<ParentClass>(parent) | |
.CreateItem<ChildClass>(child); |
This file contains 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
Container container = cosmosClient.GetContainer("myDb", "myCollection"); | |
// Assuming your have your data available to be inserted or read | |
List<Task> concurrentTasks = new List<Task>(); | |
foreach(Item itemToInsert in ReadYourData()) | |
{ | |
concurrentTasks.Add(container.CreateItemAsync(itemToInsert, new PartitionKey(itemToInsert.MyPk))); | |
} | |
await Task.WhenAll(concurrentTasks); |
This file contains 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
CosmosClientOptions options = new CosmosClientOptions() { | |
AllowBulkExecution = true }; | |
CosmosClient cosmosclient = new CosmosClient(connectionString, options); |
This file contains 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
// To view the default settings, hold "alt" while clicking on the "Settings" button. | |
// For documentation on these settings, see: https://aka.ms/terminal-documentation | |
{ | |
"$schema": "https://aka.ms/terminal-profiles-schema", | |
"defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}", | |
"profiles": |
This file contains 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
[FunctionName("Trigger")] | |
public static void Trigger([CosmosDBTrigger( | |
databaseName: "%MonitoredDatabase%", | |
collectionName: "%MonitoredCollection%", | |
ConnectionStringSetting = "CosmosDB", | |
LeaseCollectionPrefix = "%MonitoredDatabaseLeasePrefix%", | |
LeaseCollectionName = "leases", | |
CreateLeaseCollectionIfNotExists = true)] IReadOnlyList<Document> documents, | |
ILogger log) | |
{ |
This file contains 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
private class CosmosDBConnectionString | |
{ | |
public CosmosDBConnectionString(string connectionString) | |
{ | |
// Use this generic builder to parse the connection string | |
DbConnectionStringBuilder builder = new DbConnectionStringBuilder | |
{ | |
ConnectionString = connectionString | |
}; |
This file contains 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
private static Lazy<IRemainingWorkEstimator> remainingWorkEstimator; | |
public static IRemainingWorkEstimator GetRemainingWorkEstimator( | |
DocumentClient monitoredCollectionClient, | |
DocumentClient leaseCollectionClient) | |
{ | |
if (remainingWorkEstimator == null) | |
{ | |
// Pull the Connection string from the environment, Environment.GetEnvironmentVariable will read the local.settings.json file or the deployed Function App configuration | |
CosmosDBConnectionString cosmosDBConnectionString = new CosmosDBConnectionString(Environment.GetEnvironmentVariable("CosmosDB")); |
This file contains 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
[FunctionName("Monitor")] | |
public static async Task Monitor( | |
[TimerTrigger("*/1 * * * * *", RunOnStartup = true)] TimerInfo timer, // Timer will trigger every 1 second, adjust CRON expression | |
[CosmosDB("%MonitoredDatabase%", "%MonitoredCollection%", ConnectionStringSetting = "CosmosDB")] DocumentClient monitoredCollectionClient, | |
[CosmosDB("%MonitoredDatabase%", "leases", ConnectionStringSetting = "CosmosDB")] DocumentClient leaseCollectionClient, | |
ILogger log) | |
{ | |
var estimator = GetRemainingWorkEstimator(monitoredCollectionClient, leaseCollectionClient); | |
var remainingWork = await estimator.GetEstimatedRemainingWork(); | |
// Send custom metric to App Insight |