Microsoft Azure Cosmos DB is an abstraction level over data stores like Azure DocumentDB and Table Storages, which is a globally distributed multi-model database service. The benefit of Cosmos DB is that it provides low latency reads and writes, even when they still implement a single write node and multiple read nodes in their geo-replication feature. Developers can quickly create and query documents, key/value, and graph databases using Azure Cosmos DB.
Create a New Cosmos Database Account and Save Data
Log in to AzurePortal or create an Azure account if you don't have one, as shown in Figure 1. Once logged in, Click Create a resource > Databases > Azure Cosmos DB (see Figure 2).
Figure 1: Azure New Resource Group
Figure 2: Azure New Cosmos DB
On the New account page, the enter following setting details for the new Azure Cosmos DB account.
- ID: It's a unique name to identify your Azure Cosmos DB account. Remember that the 'documents.azure.com' name will be appended to the ID that you provide to create your URI.
- API: The API determines the type of account to create. Azure Cosmos DB provides five APIs: SQL (document database), Gremlin (graph database), MongoDB (document database), Table API, and Cassandra API. In this example, I have selected SQL API.
- Subscription: The Azure subscription name under which that you have created an Azure account. I have used Azure Free Subscription Credit.
- Resource Group: Create a New Resource group or select an existing group.
- Location: Geographic location to host your Azure Cosmos DB account.
Click 'Review + create' after entering all the above details, as you can see in Figure 3.
Figure 3: Azure New Cosmos DB Account Settings
Figure 4 shows how you review all the details. Click Create.
Figure 4: Azure New Cosmos DB Account Settings Review
The Cosmos DB account creation takes a few minutes (see Figure 5). Wait for the portal to complete the DB creation.
Figure 5: Azure New Cosmos DB Account Created
Once the DB account is successfully created, we will add a new collection to the DB. To create a collection and database, we will use the Data Explorer tool of the Azure portal.
Click Data Explorer in Azure Portal > New Collection.
The add collection area is displayed, as you can see on the far right of Figure 6. On the Add collection page, enter the following settings for the new collection.
Figure 6: Cosmos DB Collection Settings
- Database ID: The new database name. Database names must contain from 1 to 255 characters.
- Collection ID: Enter a new valid collection name.
- Storage Capacity: Two options are there. I have used the default value of Fixed (10 GB).
- Throughput: Selected the default throughput to 1000 request units per second (RU/s).
Optionally, you can add Unique keys for the collection. In this example, I have left the field empty. Unique keys provide developers with the ability to add a layer of data integrity to the database. Click OK. Data Explorer will display the new database and collection, as demonstrated in Figure 7.
Figure 7: Cosmos DB Collection and Settings
Next, we will add data to the new collection created by using Data Explorer.
In Data Explorer, the new database appears in the Collections pane. Expand the Tasks database > expand the Items collection > click Documents > and then click New Documents (see Figure 8).
Figure 8: Cosmos DB Collection and Settings
Now, to add a new document to the collection with the following JSON structure, I have copied the JSON, pasted it in the Data Explorer, and saved.
You can add more documents by inserting a unique value for the ID property. Your new documents can have any structure (see Figure 9).
Figure 9: Cosmos DB Collection—Added a New Document
Developers now can use queries in Data Explorer to retrieve and filter data added in Cosmos DB.
By default, the query is set to SELECT * FROM c. that the default query retrieves and displays all documents in the collection.
You can modify the query to list documents in descending or ascending order.
Cosmos DB Connection String
To get your Cosmos DB connection string information and copy it into the application configuration settings, go to the Azure portal > Azure Cosmos DB account > The left navigation selects Keys, and then selects Read-write Keys, as shown in Figure 10. A developer can use the copy buttons on the right side of the screen to copy the URI and Primary Key into the web.config file in the next step.
Figure 10: Cosmos DB Connection String
Cleaning Up Resources—Deleting Cosmo DB
To delete a Cosmos DB resource created in previous step, we have to follow these steps.
- From the Azure portal, select Resource groups on the far left, and then select the resource group created for this example. This is shown in Figure 11.
Figure 11: Cosmos DB—Delete Resource
- Left menu > click the Expand button to expand it.
- In the new window, select the resource group, and click Delete resource group.
- In the Delete Resource window, type the name of the resource group to delete, and then click Delete (see Figure 12).
Figure 12: Cosmos DB—Delete Resource Confirmation
I hope the preceding details and screen shots mentioned in this article will help you to understand the basic features of Cosmos DB.
Azure provides a Cosmos DB Data Migration tool for importing data in DB. Refer to the previous URL for more details.
That's all for today. Happy coding!