Open In App

How to Setup Azure Cosmos DB?

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.



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.

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

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

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 .

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 .

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 .

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 .

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

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

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}`);

node app.js

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.


Article Tags :