Azure Cosmos DB Tutorial: A Comprehensive Guide

Author

Reads 979

Computer server in data center room
Credit: pexels.com, Computer server in data center room

Azure Cosmos DB is a globally distributed, multi-model database service designed to support a wide range of data models and query languages.

It provides a flexible and scalable solution for building modern applications that require high performance and low latency.

Azure Cosmos DB supports five different data models: document, key-value, graph, column-family, and relational.

This flexibility makes it a popular choice for developers building cloud-native applications.

By using Azure Cosmos DB, you can simplify your database architecture and focus on building a great user experience.

Azure Cosmos DB is designed to handle large amounts of data and scale horizontally, making it a great choice for big data applications.

Getting Started

To get started with Azure Cosmos DB, you'll first need to create a 30-day free trial account using your Microsoft Account. This will give you access to the Azure portal and allow you to create a Cosmos DB account.

You can also use the Azure Cosmos DB Local Emulator, which doesn't require an internet connection and is a great way to play around with the service without committing to a paid subscription.

For your interest: Azure Function C

Credit: youtube.com, Getting Started with Azure Cosmos DB

To create a Cosmos DB account, you'll need to have an Azure subscription. Don't worry if you don't have one already, as you can create a free trial account if you don't have one.

Once you have an Azure subscription, you can create a Cosmos DB account using the Azure portal. This will involve selecting a resource group, account name, location, and API option (in this case, Core (SQL)).

Here are the basic settings you'll need to provide when creating a Cosmos DB account:

Make sure to review and validate your settings before creating your account. The process may take a few minutes, but you'll know it's complete when you see the "Go to resource" button.

Database Overview

A Cosmos DB database can have one or multiple containers, and each container can have one or multiple partitions. Each partition has documents with the same partition key.

You can have up to 25 containers in a database with standard (manual) provisioned throughput, and each container has a minimum of 400 RU/s. With auto-scale provisioned throughput, you can have up to 25 containers in a database with a maximum of 4000 RU/s.

Here's a breakdown of the structure of a Cosmos DB database:

  • Cosmos DB Account
  • Subscription
  • Cosmos DB Resource
  • Cosmos DB Database
  • Container
  • Partition
  • Document (with same partition key)

This structure is important to understand when working with Cosmos DB, as it affects how you design and query your databases.

Data Model and Structure

Credit: youtube.com, Data Modelling in Azure Cosmos DB

When selecting an API and data model for Azure Cosmos DB, you have several options to choose from. The available options include SQL, MongoDB, Table, Graph, and Cassandra, as well as Microsoft Fabric.

To create your models, you'll need to add a file for each data model in your Models folder. For example, you might add a file called ScrubResult.cs and include the following code:

```

public class ScrubResult

{

[JsonProperty("id")]

public string Id { get; set; }

// other properties...

}

```

The JsonProperty attribute is used to explicitly give a name to each property, which can help keep your C# code consistently Pascal case while keeping your Cosmos DB property names camel case. However, it's not strictly necessary to use this attribute.

One thing to note is that the API is using the Newtonsoft.Json package to serialize the objects into JSON strings. This means you can use any of the features that come with this package to manipulate your data.

Data Operations

Credit: youtube.com, Azure Cosmos DB Tutorial | Globally distributed NoSQL database

Data operations in Azure Cosmos DB are a breeze. To query data, simply click on the New SQL Query button and execute the default query, SELECT * FROM c, to retrieve all documents.

You can filter records by modifying the query to include a WHERE predicate. For example, to retrieve a document with an ID value of 2, simply add a WHERE clause to the query.

To add new records, click on the New Item button and insert a sample record in JSON document form. Once saved, you'll see additional metadata columns like _rid, _self, _etag, _attachments, and _ts.

Deleting an item is just as easy, with the DeleteItemAsync method deleting a item from the Azure Cosmos service as an asynchronous operation.

To perform a query over multiple items in a container, define a query in the repository's interface and use the Query method decorator to execute the parameterized query. This will fetch all the results of the query using repository.getItemsByCategory.

LINQ queries can also be used to query items, with the GetItemLinkQueryable method creating a LINQ query for items under a container in an Azure Cosmos DB service.

See what others are reading: Azure Service Bus Topic Filter Example

Security and Access

Credit: youtube.com, Enhance security through role-based access control for Azure Cosmos DB - overview and demo

Azure Cosmos DB provides two account keys: PRIMARY KEY and SECONDARY KEY. These keys provide unlimited access to your account, so keep them in a safe and secure location.

The PRIMARY KEY can be used to perform operations (read, write, delete) on any data within your account, while the SECONDARY KEY serves as a backup to replace the PRIMARY KEY if it's compromised or rotated for security purposes.

You can use the PRIMARY KEY and the endpoint to create a complete connection string to your Cosmos DB account, which your application will use to connect and perform CRUD operations. The secondary connection string works the same way but cannot be used to write data into your account.

Restricting Databases

You need to restrict database access to ensure that users can't view or modify sensitive information they shouldn't have access to.

Iterating through all databases will fail if a user only has read access to a single container and no Admin rights. They simply cannot access anything outside of that container.

To grant full access, you must provide PermissionMode.All, which allows users to view and modify all database resources.

Restricting database access is crucial for maintaining data security and integrity.

Secure PII Application

Credit: youtube.com, What is PII (Personally Identifiable Information)? PII and Data Security

Building a secure application that handles sensitive personal information, like personal identifiable information (PII), is crucial for maintaining user trust and complying with regulations.

You'll need to remove sensitive information, such as social security numbers, from your data model before storing it in a database like Cosmos DB.

In this process, you'll use a scrubbing service to verify the accuracy of the provided information.

The scrubbing service will return a result, which you'll then import into your software to determine the validity of the account.

To build this application, you'll use ASP.NET Core and .NET 6 to create a web application that accepts a CSV file containing a person's name and social security number.

You'll then send this data to a dummy scrub service, and finally store the results in a Cosmos DB account.

To get started, you'll need to find your Okta values in the .okta.env file produced by the CLI when you initialized your Okta application.

You'll also need to navigate to your Cosmos DB account page and open the Settings > Keys tab to find the URI, PRIMARY KEY, and other necessary values.

By following these steps, you'll create a secure application that handles PII with care and attention to detail.

On a similar theme: Azure Service Bus Tutorial

Account Keys Access

Credit: youtube.com, Manage access keys of an Azure Storage Account

Accessing your Azure Cosmos DB account keys is a crucial step in working with the service. You'll need these keys to connect to your account from your application code.

To access your account keys, scroll down on the left navigation panel and click on the Keys blade. You'll see two keys: PRIMARY KEY and SECONDARY KEY.

The PRIMARY KEY provides unlimited access to your account and can be used to perform operations like read, write, and delete on any data within your account. Your app typically uses this key to make authenticated requests against your Cosmos DB account.

The SECONDARY KEY is a backup key that can be used if you believe your primary key has been compromised or if you like to rotate your keys to increase security.

You should keep your account keys in a safe and secure location, as anyone with access to these keys can access any data within your Cosmos DB account without any permissions or restrictions.

Discover more: The Azure Key

Credit: youtube.com, Secure your Azure Storage account Security Recommendations

Here are the main differences between PRIMARY and SECONDARY keys:

  • PRIMARY KEY: Used for read, write, and delete operations on any data within your account.
  • SECONDARY KEY: Used as a backup to replace your primary key if it's compromised or rotated for security reasons.

You can also see two connection strings (PRIMARY and SECONDARY) at the bottom of the Keys blade. These connection strings, combined with the key, provide the complete connection string to your Cosmos DB account.

Authenticate the Client

To authenticate the client, you'll need to create a new instance of the CosmosClientBuilder class in your configuration class. This is where the magic happens, and you get to configure authentication using a DefaultAzureCredential instance.

The DefaultAzureCredential instance is a key part of the process, as it allows the client to authenticate with Azure using various methods, such as environment variables, Azure CLI, or a managed identity.

You can configure the authentication settings in the CosmosClientBuilder instance, which will then be used to create the Cosmos client. This is a crucial step, as it ensures that your client is properly authenticated and authorized to access the Cosmos database.

Here's a brief rundown of the required steps:

  1. Configure the CosmosClientBuilder instance with the DefaultAzureCredential instance.
  2. Use the CosmosClientBuilder instance to create the Cosmos client.

By following these steps, you'll be able to authenticate your client and access the Cosmos database securely.

Deployment and Distribution

Credit: youtube.com, What Is Azure Cosmos DB? Plus Tutorial | How to Deploy an Azure Cosmos DB Instance

To deploy Azure Cosmos DB, you'll need an Azure subscription, which you can obtain for free through the "Try Azure" portal or by using the Azure Cosmos DB free tier, which offers 1000 RU/s and 25 GB of storage.

You can also deploy Azure Cosmos DB without any cost or commitment by using the "Try Azure" portal.

Once you have an Azure subscription, you can create a Cosmos DB account by clicking on the "Create" button, selecting the SQP API, and logging in with your Microsoft account.

To enable global distribution in Azure Cosmos DB, you can associate multiple regions with your Cosmos DB account, which has some limitations on geo-fencing policies.

Here are the key benefits of Azure Cosmos DB's global distribution feature:

To implement a global distribution strategy, you can enable Geo-Replication in a few clicks by selecting the region where you'll replicate your data and clicking on the "Enable Multi-region writes" button.

With Azure Cosmos DB's Geo-Replication feature, you can ensure that your data is accessible regardless of where your customers are around the globe, with an SLA of less than 10ms for writes to be available in all replicas.

Development Tools

Credit: youtube.com, Desktop tools for developing with Azure Cosmos DB | Azure Friday

You can develop and manage Azure Cosmos DB databases using a range of development tools.

Visual Studio Code (VS Code) is a popular choice among developers, and for good reason. You can install the Azure Databases Vs Code Extension, which allows you to browse through your databases available in your subscriptions directly within VS Code.

This extension enables you to inspect your databases and even update them from within VS Code, making it a convenient and efficient way to manage your data.

Development

In the world of development, having the right tools can make all the difference. You can start by authenticating the client, which is a crucial step in many development processes.

To get started with Azure Cosmos DB, you'll need to create a new class that inherits from AbstractCosmosConfiguration to configure the connection to Azure Cosmos DB for NoSQL.

Getting a database and a container is also a fundamental step in development, as it allows you to store and manage data efficiently. The sample code in the template uses a database named cosmicworks and container named products.

Recommended read: Azure Databricks Tutorial

Credit: youtube.com, 7 Amazing Developer Tools that you're not using yet

Creating an item and getting an item are also essential development tasks, especially when working with databases like Azure Cosmos DB. The products container contains details such as name, category, quantity, a unique identifier, and a sale flag for each product.

To query items, you can use the /category property as a logical partition key, which is a useful feature in Azure Cosmos DB. This allows you to efficiently retrieve data based on specific criteria.

Here are the development steps outlined in the code examples:

  • Authenticate the client
  • Get a database
  • Get a container
  • Create an item
  • Get an item
  • Query items

Vs Code Extension

You can install Azure Databases Vs Code Extensions to access your databases directly within VSCode.

This extension allows you to browse through your databases available in your subscriptions.

You can inspect your databases and make updates from within VSCode.

This level of integration can save you a lot of time and effort in your development workflow.

Create Application Services

Creating application services is a crucial step in developing a robust and scalable application. This process involves breaking down the application into smaller, independent services that can be developed, tested, and deployed separately.

Credit: youtube.com, Create & deploy app services

You'll need to create a new folder in the project root and name it Services. This is where you'll add the four files that will make up your application services: ICosmosService.cs, CosmosService.cs, IScrubService.cs, and ScrubService.cs.

The ICosmosService.cs file is where you'll define the interface for your Cosmos service. This interface will specify the methods that the service must implement. You can replace the code in this file with the following code snippet.

Here's a brief overview of what each service does:

The CosmosService.cs file is where you'll implement the Cosmos service. This service will store data in Cosmos DB using the SaveResultAsync method. You can update the code in this file with the following code snippet.

The ScrubService.cs file is where you'll implement the Scrub service. This service will mock a true scrub service by simulating an async method. You can update the code in this file with the following code snippet.

The InitializeCosmosClientInstanceAsync method in the CosmosService.cs file is what makes this service so powerful. It ensures that your database and container are created before passing the instance of the CosmosService to the consumer to be used. This method will call the container you're operating on and create an item in that container.

Concurrency in

Credit: youtube.com, 14: Azure Cosmos DB Concurrency Control | eTag In Azure Cosmos DB | Azure Cosmos DB Tutorial

Concurrency in Azure Cosmos DB is a crucial aspect to consider when working with this database. All items in Cosmos DB have an _etag field that gets set on the server every time a item is updated.

This _etag field is used to control if a replace should succeed, or not, based on whether the ETag on the request matches the current ETag value of the persisted Item. To achieve this, you must pass the ETag to your cosmos DB when doing a replace of a item.

Using the ETag in this way helps to prevent data loss by allowing the server to reject a replace operation if someone did an update to the same item since you read it. This is known as optimistic concurrency.

Here's a summary of how ETag works in Azure Cosmos DB:

By using ETag in this way, you can ensure that your data remains consistent and up-to-date, even in the face of concurrent updates.

Creating and Managing

Credit: youtube.com, Getting Started with Azure Cosmos DB: A Beginner's Guide to Creating Databases and Collections

To create a new container in Azure Cosmos DB, you need to create a database under an existing account and add data containers. This step creates an 'Items' container with 400 Request Units per second (RU/s) throughput capacity for up to 400 reads/sec.

A container is a logical component of an Azure Cosmos DB database, which can be thought of as a table in a relational database or as a collection in MongoDB. Every container must have a partition key so that the service can distribute data evenly across partitions and offer high availability and performance guarantees.

To create a new container, click on Data Explorer on the left navigation panel to open the Data Explorer blade, then click on New Container to open a new container. You will need to specify some essential information about your container, including database id, share throughput across containers, container id, partition key, and container throughput (autoscale).

Credit: youtube.com, What is Azure Cosmos DB?

Here are the essential settings for creating a new container:

Creating the container might take a few minutes, depending on your account size and your chosen throughput settings. Once the process completes, your container will be automatically added to the Data Explorer blade on the left.

Thomas Goodwin

Lead Writer

Thomas Goodwin is a seasoned writer with a passion for exploring the intersection of technology and business. With a keen eye for detail and a knack for simplifying complex concepts, he has established himself as a trusted voice in the tech industry. Thomas's writing portfolio spans a range of topics, including Azure Virtual Desktop and Cloud Computing Costs.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.