/// <summary> /// Create a partitioned container. /// </summary> /// <returns>The created container.</returns> private static async Task <ContainerResponse> CreatePartitionedContainerAsync(BenchmarkConfig options, CosmosClient cosmosClient) { Microsoft.Azure.Cosmos.Database database = await cosmosClient.CreateDatabaseIfNotExistsAsync(options.Database); Container container = database.GetContainer(options.Container); try { return(await container.ReadContainerAsync()); } catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound) { // Show user cost of running this test double estimatedCostPerMonth = 0.06 * options.Throughput; double estimatedCostPerHour = estimatedCostPerMonth / (24 * 30); Console.WriteLine($"The container will cost an estimated ${Math.Round(estimatedCostPerHour, 2)} per hour (${Math.Round(estimatedCostPerMonth, 2)} per month)"); Console.WriteLine("Press enter to continue ..."); Console.ReadLine(); string partitionKeyPath = options.PartitionKeyPath; return(await database.CreateContainerAsync(options.Container, partitionKeyPath, options.Throughput)); } }
static async Task Main(string[] args) { CosmosClient cosmosClient = new CosmosClient(EndpointUrl, AuthorizationKey, new CosmosClientOptions() { AllowBulkExecution = true }); Microsoft.Azure.Cosmos.Database database = await cosmosClient.CreateDatabaseIfNotExistsAsync(Program.DatabaseName); await database.DefineContainer(Program.ContainerName, "/eyeColor") .WithIndexingPolicy() .WithIndexingMode(IndexingMode.Consistent) .WithIncludedPaths() .Attach() .WithExcludedPaths() .Path("/*") .Attach() .Attach() .CreateAsync(50000); List <KeyValuePair <PartitionKey, Stream> > itemsToInsert = new List <KeyValuePair <PartitionKey, Stream> >(ItemsToInsert); foreach (Model.Person item in Program.GetItemsToInsert()) { MemoryStream stream = new MemoryStream(); await JsonSerializer.SerializeAsync(stream, item); itemsToInsert.Add(new KeyValuePair <PartitionKey, Stream>(new PartitionKey(item.eyeColor), stream)); } Container container = database.GetContainer(ContainerName); List <Task> tasks = new List <Task>(ItemsToInsert); foreach (KeyValuePair <PartitionKey, Stream> item in itemsToInsert) { tasks.Add(container.CreateItemStreamAsync(item.Value, item.Key) .ContinueWith((Task <ResponseMessage> task) => { using (ResponseMessage response = task.Result) { if (!response.IsSuccessStatusCode) { Console.WriteLine($"Received {response.StatusCode} ({response.ErrorMessage})."); } } })); } // Wait until all are done await Task.WhenAll(tasks); }
public static async void Run([CosmosDBTrigger( databaseName: "contoso", collectionName: "category", ConnectionStringSetting = "CosmosConnectionString", LeaseCollectionName = "leases")] IReadOnlyList <Document> input, ILogger log) { if (input != null && input.Count > 0) { log.LogInformation("Documents modified " + input.Count); log.LogInformation("First document Id " + input[0].Id); var connectionString = "contoso-meo.redis.cache.windows.net:6380,password=EIhC70PPwkHZKSY5cc48SAy6YfM4FmahxchOEHKJjwo=,ssl=True,abortConnect=False"; var redisConnection = ConnectionMultiplexer.Connect(connectionString); IDatabase db = redisConnection.GetDatabase(); db.Execute("flushall"); string connStr = Environment.GetEnvironmentVariable("CosmosConnectionString"); CosmosClient cosmosClient = new CosmosClient(connStr); Microsoft.Azure.Cosmos.Database cosmosDb = cosmosClient.GetDatabase("contoso"); Container container = cosmosDb.GetContainer("category"); var sqlQueryText = $"SELECT c.CategoryId, c.CategoryName, c.Decription, c.Products, c.id FROM c"; log.LogInformation("Running query: {0}\n", sqlQueryText); QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText); FeedIterator <Category> queryResultSetIterator = container.GetItemQueryIterator <Category>(queryDefinition); List <Category> categories = new List <Category>(); while (queryResultSetIterator.HasMoreResults) { log.LogInformation("In loop"); FeedResponse <Category> currentResultSet = await queryResultSetIterator.ReadNextAsync(); foreach (Category cat in currentResultSet) { categories.Add(cat); log.LogInformation("\tRead {0}\n", cat); db.StringSet($"category:{cat.CategoryId}", cat.CategoryName); } } redisConnection.Dispose(); redisConnection = null; } }
public static async Task Main(string[] args) { using (CosmosClient client = new CosmosClient(_endpointUri, _primaryKey)) { Microsoft.Azure.Cosmos.Database database = client.GetDatabase(_databaseId); Container container = database.GetContainer(_containerId); List <Food> foods = new Faker <Food>() .RuleFor(p => p.Id, f => (-1 - f.IndexGlobal).ToString()) .RuleFor(p => p.Description, f => f.Commerce.ProductName()) .RuleFor(p => p.ManufacturerName, f => f.Company.CompanyName()) .RuleFor(p => p.FoodGroup, f => "Energy Bars") .Generate(10000); int pointer = 0; while (pointer < foods.Count) { StoredProcedureExecuteResponse <int> result = await container.Scripts.ExecuteStoredProcedureAsync <IEnumerable <Food>, int>(new PartitionKey("Energy Bars"), "bulkUpload", foods.Skip(pointer)); pointer += result.Resource; await Console.Out.WriteLineAsync($"{pointer} Total Items\t{result.Resource} Items Uploaded in this Iteration"); } Console.WriteLine("Execution paused for verification. Press any key to continue to delete."); Console.ReadKey(); bool resume = true; do { string query = "SELECT * FROM foods f WHERE f.foodGroup = 'Energy Bars'"; StoredProcedureExecuteResponse <DeleteStatus> result = await container.Scripts.ExecuteStoredProcedureAsync <string, DeleteStatus>(new PartitionKey("Energy Bars"), "bulkDelete", query); await Console.Out.WriteLineAsync($"Batch Delete Completed.\tDeleted: {result.Resource.Deleted}\tContinue: {result.Resource.Continuation}"); resume = result.Resource.Continuation; }while (resume); } }
public static async void Run([CosmosDBTrigger( databaseName: "contoso", collectionName: "orders-aggregate", ConnectionStringSetting = "CosmosConnectionString", LeaseCollectionName = "leases")] IReadOnlyList <Microsoft.Azure.Documents.Document> input, ILogger log) { if (input != null && input.Count > 0) { log.LogInformation("Documents modified " + input.Count); log.LogInformation("First document Id " + input[0].Id); string connStr = Environment.GetEnvironmentVariable("CosmosConnectionString"); CosmosClient cosmosClient = new CosmosClient(connStr); Microsoft.Azure.Cosmos.Database db = cosmosClient.GetDatabase("contoso"); Container container = db.GetContainer("orders-for-hour"); // custQuery is an IEnumerable<IGrouping<string, Customer>> var dateQuery = from o in input group o by o.GetPropertyValue <string>("OrderDateHour") into orderDateGroup select orderDateGroup; log.LogInformation(dateQuery.Count().ToString()); foreach (var dq in dateQuery) { log.LogInformation("DateQuery: " + dq.Key); var sqlQueryText = $"SELECT * FROM c WHERE c.id = '{dq.Key}'"; log.LogInformation("Running query: {0}\n", sqlQueryText); QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText); FeedIterator <OrderEvent> queryResultSetIterator = container.GetItemQueryIterator <OrderEvent>(queryDefinition); List <OrderEvent> orders = new List <OrderEvent>(); while (queryResultSetIterator.HasMoreResults) { log.LogInformation("In loop"); FeedResponse <OrderEvent> currentResultSet = await queryResultSetIterator.ReadNextAsync(); foreach (OrderEvent order in currentResultSet) { orders.Add(order); log.LogInformation("\tRead {0}\n", order); } } log.LogInformation("Orders Count: " + orders.Count); OrderEvent currentOrder = new OrderEvent() { id = dq.Key, Count = dq.Count(), Total = dq.Sum(x => x.GetPropertyValue <decimal>("Total")) }; if (orders.Count == 0) { log.LogInformation("Addin entry"); try { ItemResponse <OrderEvent> orderResponse = await container.CreateItemAsync <OrderEvent>(currentOrder); } catch (Exception ex) { log.LogInformation("Exception: " + ex.Message); } } else { ItemResponse <OrderEvent> orderResponse = await container.ReadItemAsync <OrderEvent>(dq.Key, new PartitionKey(dq.Key)); int currentCount = orderResponse.Resource.Count; decimal currentTotal = orderResponse.Resource.Total; OrderEvent newOrder = new OrderEvent() { id = dq.Key, Count = currentCount + currentOrder.Count, Total = currentTotal + currentOrder.Total }; orderResponse = await container.ReplaceItemAsync <OrderEvent>(newOrder, newOrder.id); } } } }