Skip to content

Instantly share code, notes, and snippets.

Avatar

Pradeep Loganathan PradeepLoganathan

View GitHub Profile
View gist:34ac10abe6d2be9ee463cafb131b483e
CosmosContainerResponse container = await this._database.DefineContainer("CustomerContainer", "/CustomerId")
.CreateAsync(5000);
View cosmosstarter.cs
CosmosClient cosmosClient = new CosmosClientBuilder("https://localhost:8081", "key")
.WithConnectionModeDirect()
.WithApplicationName("CosmosStarter")
.Build();
View CosmosDbConnection.cs
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",
PortReuseMode = PortReuseMode.PrivatePortPool
View customer.json
{
"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",
View CosmosDriver.cs
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>();
View CosmosDriver.cs
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);
View CosmosDriver.cs
public async Task AddCustomer(Customer customer)
{
try
{
ItemResponse<Customer> customerResponse = await this._container.CreateItemAsync<Customer>(customer, new PartitionKey(customer.CustomerId));
Console.WriteLine("Created item in database with id: {0} Operation consumed {1} RUs.\n", customerResponse.Resource.CustomerId, customerResponse.RequestCharge);
}
catch (CosmosException ex)
{
Console.WriteLine("Exception occured in AddCustomer: {0} Message body is {1}.\n", ex.Message,ex.ResponseBody);
@PradeepLoganathan
PradeepLoganathan / CosmosDriver.cs
Last active Oct 20, 2020
Creating the cosmos container
View CosmosDriver.cs
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 Oct 21, 2020
Creating a Cosmos Db database using Code
View CosmosDriver.cs
private readonly CosmosClient _cosmosClient;
private Database _database;
private Container _container;
private readonly string _databaseId = "thetaDb";
private readonly string _containerId = "CustomerContainer";
public CosmosDriver()
{
_cosmosClient = CosmosDbConnection.Instance;
View CosmosDatabase.ps
az cosmosdb sql database create \
--account-name eCommerceDb-AC \
--name "Customers" \
--resource-group eCommerceDb-RG
You can’t perform that action at this time.