Introduction
Azure CosmosDB is a NoSQL Document database developed by Microsoft. Document databases are becoming very popular these days. They are very fast and give amazing flexibility for changing schema when compared with Relational Databases. Without being bound to the schema we can get the actual view of data. There are many popular cloud-native document databases such as Amazon DynamoDB, Azure ComosDB, Google Cloud Datastore, etc. Using the Cloud Native databases becomes difficult without a subscription. We need to look for possible approaches to save time and money for testing and developing our solutions.
In this tutorial, we will learn how to develop solutions in Azure CosmosDB locally. This approach is very useful for beginning your journey without requiring any subscription. It is very easy to set up this emulator. We get the same look and feel as if we are working in Azure. No additional installation is required. We can easily explore the data using a web browser. We will also use CosmosDB SDK for .NET Core and Create a sample database, container, and then a document.
Setup A Local Instance
- Download Azure Cosmos Emulator from the link.
- Complete the Installation and Launch Azure Cosmos DB Emulator from the Start Menu.
- Check the URL, https://localhost:8081/_explorer/index.html.
- (Optional) Click on Explore and Start with Sample Data.
Setup Visual Studio Project
- Create a new project in Visual Studio.
- Right Click on Solution and Select Manage Nuget Packages for Solution.
- Search for Microsoft.Azure.Cosmos and click Install.
Developing Solution
Add the following references:
- using Microsoft.Azure.Cosmos;
- using System;
- using System.Threading.Tasks;
- Create the class below: namespace CosmosDBSample {
- class CosmosHelper {
- private readonly string URI = "https://localhost:8081";
- private readonly string PrimaryKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
- private CosmosClient cosmosClient;
- private Database database;
- private Container container;
- private string databaseName = "SimpleDB";
- private string containerName = "DataContainer";
- private string partitionPath = "/id";
- public CosmosHelper() {
- this.cosmosClient = new CosmosClient(URI, PrimaryKey);
- }
- public async Task CreateTestRecord() {
- this.database = await this.cosmosClient.CreateDatabaseIfNotExistsAsync(databaseName);
- this.container = await this.database.CreateContainerIfNotExistsAsync(containerName, partitionPath);
- await this.container.CreateItemAsync(new {
- id = Guid.NewGuid(),
- Name = "Varun"
- });
- }
- }
- }
Now call this code from your main class.
- CosmosHelper cosmosHelper = new CosmosHelper();
- await cosmosHelper.CreateTestRecord();
Now, verify this in https://localhost:8081/_explorer/index.html
We can see from the above screenshot that SimpleDB, DataContainer is automatically created. Along with this document is inserted. If we will execute the above code multiple times only documents will increase since database and container are already created. This was very simple and very straight forward.
Summary
In this article, we have covered:
- How to Setup Azure CosmosDB Emulator Locally.
- Installing ComsosDB SDK from Nuget Package in our Visual Studio Project.
- Inserting a Sample Record and Verifying it from Web Browser Client.
Thanks for Reading! Keep exploring and please feel free to share your thoughts on this. Happy Reading!!