Skip to content

Instantly share code, notes, and snippets.

View PradeepLoganathan's full-sized avatar

Pradeep Loganathan PradeepLoganathan

View GitHub Profile
CosmosClient cosmosClient = new CosmosClientBuilder("https://localhost:8081", "key")
.WithConnectionModeDirect()
.WithApplicationName("CosmosStarter")
.Build();
public static class CosmosDbConnection
{
private const string EndpointUri = "https://localhost:8081";
private const string PrimaryKey = "yourprimarykey==";
private static CosmosClient _instance = null;
private static readonly CosmosClientOptions CosmosClientOptions = new CosmosClientOptions()
{
ConnectionMode = ConnectionMode.Direct,
ApplicationName = "CosmosStarter"
};
@PradeepLoganathan
PradeepLoganathan / customer.json
Created October 20, 2020 09:17
Sample Json document
{
"id": "CU7-36-8183",
"CustomerId": "CU7-36-8183",
"ModifiedDate": "2020-08-10T16:31:18.6928886+10:00",
"Title": "Mr.",
"FirstName": "Delfina",
"LastName": "Rutherford",
"MiddleName": "Skylar",
"Suffix": "II",
"CompanyName": "Cormier, Oberbrunner and Kunde",
public async Task<List<Order>> GetOrders(string customerId)
{
try
{
var sqlQueryText = "SELECT * FROM c WHERE c.CustomerId = @customerid";
Console.WriteLine("Running query: {0}\n", sqlQueryText);
QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText).WithParameter("@customerid", "CU7-36-8183" );
FeedIterator<Order> queryResultSetIterator = this._container.GetItemQueryIterator<Order>(queryDefinition);
List<Order> orders = new List<Order>();
public async Task<Customer> GetCustomer(string customerId)
{
try
{
var customerResponse = await this._container.ReadItemAsync<Customer>(customerId, new PartitionKey(customerId));
return customerResponse;
}
catch (CosmosException ex)
{
Console.WriteLine("Exception occured in GetCustomer: {0} Message body is {1}.\n", ex.Message,ex.ResponseBody);
public async Task AddCustomer(Customer customer)
{
try
{
ItemResponse<Customer> customerResponse = await this._container.CreateItemAsync<Customer>(customer, new PartitionKey(customer.CustomerId));
}
catch (CosmosException ex)
{
Console.WriteLine("Exception occured in AddCustomer: Message body is {0}.\n", ex.Message);
throw;
@PradeepLoganathan
PradeepLoganathan / CosmosDriver.cs
Last active October 20, 2020 06:15
Creating the cosmos container
public async Task CreateContainerAsync()
{
ContainerProperties containerProperties = new ContainerProperties()
{
Id = Guid.NewGuid().ToString(),
PartitionKeyPath = "/CustomerId",
IndexingPolicy = new IndexingPolicy()
{
Automatic = false,
IndexingMode = IndexingMode.Lazy,
@PradeepLoganathan
PradeepLoganathan / CosmosDriver.cs
Last active November 2, 2020 12:09
Creating a Cosmos Db database using Code
private readonly CosmosClient _cosmosClient;
private CosmosDatabase _database;
private CosmosContainer _container;
private readonly string _databaseId = "thetaDb";
private readonly string _containerId = "CustomerContainer";
public CosmosDriver()
{
_cosmosClient = CosmosDbConnection.Instance;
az cosmosdb sql database create \
--account-name eCommerceDb-AC \
--name "Customers" \
--resource-group eCommerceDb-RG
az cosmosdb create \
--name eCommerceDb-AC \
--kind GlobalDocumentDB \
--resource-group eCommerceDb-RG