Azure Cosmos DB and SQL API account - Hands on activity

Azure Cosmos DB


Azure Cosmos DB – brief Introduction 

In one of previous articles, we have talked about Azure Cosmos DB and its bits and pieces.  We see that Azure Cosmos DB is the globally distributed and fully managed database service for building world-scale applications. It leverages the Azure cloud infrastructure to support global-scale application workloads.

Azure Cosmos DB is formerly known as Document DB, a multi-model NoSQL database in an Azure cloud platform that offer to store and process massive amount of structured, semi-structured and unstructured data. It provides native support to various platforms to access your database like, MongoDB APIs, Cassandra, Azure Tables, Gremlin and SQL.

Here in this article, we will talk about SQL API and will do some hands on activity on top of Azure Cosmos DB SQL API account. SQL API is the ideal way if you build a non-relational document database and execute query like SQL syntax. In the context of some exercise, you can cover the following tasks herewith – 
  • Create an Azure Cosmos DB SQL API account
  • Create a document database and a collection (item)
  • Add data to the collection
  • Query to data by using Data Explorer 


Before taking place, we need some essential pre-requisites to accomplish this Azure Cosmos DB exercise.
  1. Azure subscription, if you don't have an account then sign up for a free Azure account -
  2. Hands on SQL syntax, though not mandatory, but if know then easier to  write different queries.

STEP – 1: Create an Azure Cosmos DB account

Login to the Azure portal

In the Microsoft Azure portal, click the + Create a resource from the Hub and click the Databases from the Azure Marketplace. It will load all available database services under the Featured section, select the Azure Cosmos DB or can search from the search box.

Azure Portal

Promptly the Create Azure Cosmos DB Account blade will be loaded, the time you click the Azure Cosmos DB. Here some required details needed to submit to proceed like Project Details as well Instance Details under Basic tab.

Make sure the correct subscription is selected and then choose an existing Resource group or can create a new using Create new.

In my case going with earlier created resource group ‘demogroup

Create Azure Cosmos DB Account

Under Instance Details, enter valid details for the following properties – 
  • Account Name – Enter a unique name to identify the Azure Cosmos account.
  • API – Here, the API determines the type of account to create, select Core (SQL) to create a document database and query by using SQL syntax.
  • Apache Spark – Go with by default Disabled mode.
  • Location – Required selecting a geographic location or region to host the Azure Cosmos DB account.
  • Geo-Redundancy – Go with by default Disabled mode.
  • Multi-region Writes – Go with by default Disabled mode.

Azure Cosmos - Instance Details

Now, nearly all properties have filled up; continue by clicking the Review + Create button at the bottom. However, you can skip the Network and Tags sections. Within few moments, post validation success a final review page will be appeared that display the details about the Azure Cosmos DB you are about to create.
Azure Cosmos - Validation Success

If you find everything arranged, then click the Create button that will take a few minutes for your Azure Cosmos DB to be deployed.

Azure Cosmos - Deployment

Sooner, you will be notified once the Azure Cosmos DB is created successfully.

Azure Cosmos - Deployed

Congratulation, an Azure Cosmos DB provisioned!! 😊

STEP – 2: Add a Database and a Container

We knew that Azure Cosmos DB is a globally distributed multi-model database that supports the document, graph, and key-value data models. A collection is a container of JSON documents and associated JavaScript application logic, i.e. stored procedures, triggers and user-defined functions.

A collection maps to a container in Azure Cosmos DB, therefore, it is a billable entity, where the cost is determined by the provisioned throughput expressed in request units per second.

Move to Azure portal and click the All resources from the Hub, select the Azure Cosmos DB account page that you created in the previous steps.

Azure Cosmos - Overview Page

Since you did not create any container yet, time to create a database and container by using Data Explorer. Select Data Explorer from the left navigation from the Azure Cosmos DB account page, which provides the option to create a container.

Data Explorer

Proceed ahead by clicking the New Container, which will load Add Container page where the details required for the new container as listed below properties –
  • Database id – Enter a new database id.
  • Provision database throughput – Go with by default non-selected.
  • Container id – Enter a unique identifier for the container.
  • Partition key – For example, moving with /category as the partition key as we used in this task.
  • My partition key is larger than 100 bytes
  • Throughput – Go to the by default option.

Data Explorer

Once you submit all details, and then continue by clicking the OK button. More readily, you will get the database and the collection under Data Explorer, as you provided the name during provisioning.

Data Explorer - Container

STEP – 3: Add data to Database

In the previous section, you set up a database and a collection, time to add data to your new database by using Data Explorer.

You can see the new database, expand the cosmoscontainer database and later on expand the items collection. Sooner you can get different types of multiple options to create new items, etc. Click Items under the collection and then select the New Item from the top menu accordingly.

Data Explorer - Expand Items

A new item pane will be loaded the time you click the New Item, required to add the following JSON based structure to the item.

    "id": "100",
    "category": "personal",
    "name": "groceries",
    "description": "do not forget apples.",
    "isComplete": false

Data Explorer - Add Item

Now add the new item by clicking the Save button. Next, select the New Item again, create, and save another item with a unique id.

You can add any properties and values since the item can have any structure, since Azure Cosmos DB does not follow any schema.

I did a couples of new entries, like – 

    "id": "101",
    "category": "personal",
    "name": "fashion",
    "description": "casual dress",
    "isComplete": false
    "id": "102",
    "category": "personal",
    "name": "medicine",
    "description": "bp medicines",
    "isComplete": false
    "id": "103",
    "category": "personal",
    "name": "groceries",
    "description": "green salad",
    "isComplete": false

You can see all entries based on id and category wise, under the Items section.

Data Explorer - Added Items

STEP – 4: Query to Data

In fact, Azure Cosmos DB does not enforce any schema for data, so you are, free to add different types of properties and values in the items. Now time to query by using Data Explorer to retrieve and filter your data.

You can see a by default SELECT query exists at the top of the Items tab in Data Explored, the query retrieves and display all items in the collection in ID order.

Data Explorer - Saved Items

If required, you can change the query, select Edit Filter, replace the default query with below listed one, and then select Apply Filter.

WHERE 'groceries'

Data Explorer - Edit Query

It will display the filtered items as soon as you click the Apply Filter button.

Data Explorer - Apply Filter

Looks like, it is very similar to SQL like queries, if you are hands on with SQL syntax then can write different query in the query predicate box easily. Along with this, you can use Data Explorer to create stored procedures, User Defined Functions, and Triggers etc.

Here, we walked through the Azure Cosmos DB and came to know that how cool it is to work with your Azure Cosmos DB data. In next article, we will create a .NET web app and will validate how much feasible to work with .NET program like to update your Azure Cosmos DB data by using a .NET web app.

Keep visiting for further posts.


  1. Replies
    1. Great Article Cloud Computing Projects

      Networking Projects

      Final Year Projects for CSE

      JavaScript Training in Chennai

      JavaScript Training in Chennai

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

  2. This was huge information for all those who need this. That was a really good blog and of course knowledgeable. Thank you for sharing this information with us. Oracle Fusion Manufacturing online Training