Skip to content

Instantly share code, notes, and snippets.

@deborahc
Last active September 14, 2022 11:34
Show Gist options
  • Save deborahc/57b996e5f1dd82fcc1aacf00621c2459 to your computer and use it in GitHub Desktop.
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)
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