Last active
September 14, 2022 11:34
-
-
Save deborahc/57b996e5f1dd82fcc1aacf00621c2459 to your computer and use it in GitHub Desktop.
Create a new CosmosClient and Container using the fluent builder pattern using the Azure Cosmos DB .NET SDK V3 (GA)
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 Microsoft.Azure.Cosmos; | |
using Microsoft.Azure.Cosmos.Fluent; // Use the new fluent namespace | |
using System.Threading.Tasks; | |
// Creating a new CosmosClient and Container using the fluent builder pattern | |
namespace CosmosDotnetSDKGetStarted | |
{ | |
class Program | |
{ | |
static async Task Main(string[] args) | |
{ | |
//Create new instance of CosmosClient using builder pattern | |
CosmosClient cosmosClient = new CosmosClientBuilder("endpoint", "key") | |
.WithApplicationRegion("West US 2") | |
.WithApplicationName("ProductReviewsApp") | |
.Build(); | |
//Define and create a new container using builder pattern | |
Container container = await cosmosClient.GetDatabase("databaseId").DefineContainer("containerId", "/PartitionKey") | |
// Define indexing policy with included and excluded paths | |
.WithIndexingPolicy() | |
.WithIncludedPaths() | |
.Path("/username/*") | |
.Path("/productName/*") | |
.Path("/rating/*") | |
.Attach() | |
.WithExcludedPaths() | |
.Path("/*") | |
.Attach() | |
.Attach() | |
// Define time to live (TTL) in seconds on container | |
.WithDefaultTimeToLive(30) | |
.CreateAsync(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment