Azure Cosmos DB is Microsoft’s proprietary globally-distributed, multi-model database service “for managing data at planet-scale”. It is schema-agnostic, horizontally scalable and generally classified as a NoSQL database.
Cosmos DB enables you to elastically and independently scale throughput and storage across any number of Azure regions worldwide. You can elastically scale throughput and storage, and take advantage of fast, single-digit-millisecond data access using your favorite API including SQL, MongoDB, Cassandra, Tables, or Gremlin.
You can connect and manage Cosmos databases by using tools like Azure Data Explorer or Cosmos DB Emulator. Today’s topic is to learn how to use cosmosDB on local machine before you start working on Azure cloud.
CosmosDB Emulator can be used to develop databases for Azure CosmosDB on local machine. Also, it’s a great tool to have if you like to learn about Azure CosmosDB but you have limited access to Azure for any reason. Azure CosmosDB team constantly works to make all available tools better including the emulator. Currently, emulator supports SQL, Cassandra, MongoDB, Gremlin and Table API. Data Explorer feature supports only SQL API for now. Emulator’s implementation is different than the service and you should not use it for stress testing, you can not test global replication or latency for read and writes.
You can download and install the Azure Cosmos Emulator from the Microsoft Download Center or you can run the emulator on Docker for Windows. For instructions on using the emulator on Docker for Windows, see Running on Docker.
On Emulators start page, there are couple of information which you may require while working with CosmosDB like URI, Primary Key, Primary Connection String and Mongo Connection String.
Setup Container / Database:-
Next, we will setup container/database and try to understand couple of terms used while setup.
Click on Explorer, below screen will appear where you can add container or database by clicking menu options:
Container:- An Azure Cosmos container is the unit of scalability both for provisioned throughput and storage. A container is horizontally partitioned and then replicated across multiple regions. The items that you add to the container and the throughput that you provision on it are automatically distributed across a set of logical partitions based on the partition key.
Partition Key:- Cosmos DB uses partitioning to scale individual containers in a database to meet the performance needs of your application. In partitioning, the items in a container are divided into distinct subsets called logical partitions. Logical partitions are formed based on the value of a partition key that is associated with each item in a container. All items in a logical partition have the same partition key value.
Once you have setup the container, you are ready to add items to the container. Items are same way you have rows in SQL database table. In below screen you can see there different menu options like New Item, Upload Item and SQL Query.
We can add new document either by New Item option or we can upload JSON files through Upload Item. Let us try, first option and create new item like Country details. Example can be seen in below screen.
Sample JSON document is available in below snippet.
Now, let us try querying this data using SQL query. In below screen I have showed how to open and write SQL queries.
Same way you can try to fetch particular item, using where clause as condition in SQL query to fetch particular item from database.
Azure Cosmos DB is one of the quickest solutions for building a globally distributed database in the Azure cloud. Cosmos DB is a feature rich database which supports various APIs, and it’s easy to query the database using SQL queries. With the use of the Local Emulator, users can reap most of the benefits of Azure Cloud locally, free of cost, and without actually subscribing to the Azure portal.