Open In App

How to Setup Azure Cosmos DB?

Last Updated : 26 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Azure Cosmos DB is a managed database service that provides high availability and scalability to applications. It provides support for various NoSQL databases. It is available in cross regions hence providing high throughput. In this article, we will set up Azure cosmos DB for NoSQL. So let’s see how to set it up.

Instead of you maintaining the Database in Azure Virtual Machine Azure will create the database and they will maintain it for you by this you can more focus on the data than managing the database.

Steps to Setup Azure Cosmos DB

Step 1: Navigate to the Azure Cosmos DB page on the Azure portal. Under this page click on Create to create Azure Cosmos DB account.

Azure Cosmos DB

Step 2: On the next window select the API which you want to create cosmos db for. For this article, we will set for No SQL. select it from the options.

Chose the type required

Step 3: Now select your subscription and resource group . Also specify name and location for your cosmos db instance .

Configure Azure Cosmos DB

Step 4: Also specify capacity for instance . Keep other options as default .

Select the subscription types

Step 5: Click on next for Global Distribution . If you want your database to be global distributed then enable the options . For know we will keep them as default .

Global Distribution

Step 6: Now you can configure other options as your requirement . Else click on review and create . Review the details and then click on create .Once the deployment is successful click on go to resource . You should see output as below .

DATA base has been created

Step 7: Congratulations we have successfully set up cosmos db database in Azure . Now go to Data Explorer to create database and perform operations in cosmos DB .

Example To Access Azure Cosmos DB With NodeJS

Go to data explorer from left navigation and click on new container . Give new databaseid and container name. Also provide partition key for container . Configure other settings as per your requirement or leave as default . Then Click on create .

Configure the details

New Container

Once the database is created, you need to install azure cosmosDB sdk for nodejs using npm run below command to install the sdk

npm install @azure/cosmos

Now create file called app.js and import sdk then create connection as below . Make sure you replace the endpoint and key with your original values .

Node

const { CosmosClient } = require(“@azure/cosmos”);
const endpoint = “YOUR_COSMOSDB_ENDPOINT”;
const key = “YOUR_COSMOSDB_KEY”;

const client = new CosmosClient({ endpoint, key });

  • In this article we will write code for creating item in azure cosmosDB . Replace database and container ID’s with your original ID’s .The following code will create item with id “gfg1” and name “gfg Item”.

Node

const database = client.database(Your_databaseId);
const container = database.container(Your_containerId);

const newItem = { id: “gfg1”, name: “gfg Item” };

const { resource: createdItem } = await container.items.create(newItem);
console.log(`Created item with id: ${createdItem.id}`);

  • Once you write above code run it using below command .
node app.js
  • You should see similar output as below.

Node app.js

  • You can also see inserted item under data explorer by navigating to your database and container.

NoSQL API

FAQ’s On Azure Cosmos DB

1. How Do I Access And Manage My Data In Cosmos DB?

Visit Data explorer on azure portal where you can access as well as manager your CosmosDB .

2. How Can I Configure The Geographic Distribution Of My Data?

Configure geo-redundancy while creating your azure CosmosDB account to setup geographic distribution.

3. Which API’s Are Supported By Azure Cosmos Db?

Azure provides various APIs like NOSQL , MongoDB , Cassandra , Gremlin , ETCD , Table etc.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads