This blog walks you through the essentials of creating a NoSQL account in Azure Cosmos DB. We'll start by setting up a Cosmos DB account, creating a database, and adding a container to hold our data. From there, you'll see how to add values in JSON format and retrieve them seamlessly, showcasing Cosmos DB's ease of use and powerful querying capabilities.
Here’s a quick overview of what we’ll cover:
Setting Up a Cosmos DB Account: Understand the steps to create a NoSQL account in Azure Cosmos DB.
Database and Container Creation: Learn how to create a database and define a container to organize your data.
Adding and Retrieving Data: Explore how to insert values in JSON format into your container and retrieve them with simple queries.
Prerequisites
Azure Account
Create a Cosmos Account
In the Azure portal search for Azure CosmosDB.
Click on Create Azure Cosmos DB account.
Click on Create under Azure Cosmos DB for NoSQL.
Add the following information:
Subscription | Azure subscription name |
Resource Group | Create/Select a resource group |
Account Name | Globally unique name for your account |
Availability Zones | Disable |
Location | Azure Region |
Click next and keep other values as default. Review and create.
The deployment is in process
It will take some time for your deployment to get created.
Click on Go to resource.
Creating a database and a container
Before creating a database and a container in Cosmos, let us first understand what they are.
Database:
A database in Cosmos DB is like a "folder" that contains multiple containers. It’s a logical grouping of your data.
Example: You could create a database for an e-commerce application.
Container:
A container in Cosmos DB is where the actual data is stored. Containers are schema-less and can store items (records or documents) in JSON format.
Think of containers like "tables" in a relational database, but more flexible because you don’t need to predefine a strict schema.
Example: In an e-commerce database:
You could have a Products container to store product details.
A Customers container for customer profiles.
An Orders container to track customer purchases.
Click on Data Explorer
Click on + New Container.
Enter the values as follows:
Database | Create new |
Database id | cosmic db |
Share throughput across containers | Don't select |
Container id | products |
Partition key | /category/name |
Container throughput (autoscale) | Autoscale |
Container Max RU/s | 1000 |
Click on Ok.
Add data to the container
Select the Items option.
Click on New Item.
Add the following JSON:
{
"id": "juice-01",
"name": "apple",
"company": {
"category": "beverage",
"name": "globaldrinks"
},
"price": 50
}
Click on Save.
Query the data in your container
Select New SQL query from the taskbar.
To retrieve the data from the database, we will write a simple query:
SELECT VALUE {
"name": p.name,
"title": CONCAT(p.company.category, " from ", p.company.name),
"price": p.price
}
FROM
products p
Click on Execute Query.
You should get the following result:
[
{
"name": "apple",
"title": "beverage from globaldrinks",
"price": 50
}
]
Conclusion
Azure Cosmos DB simplifies the process of managing unstructured data, making it a powerful choice for developers and enterprises alike. By creating a NoSQL account and interacting with your data in a database, you can experience the ease and efficiency that Cosmos DB offers.