Microsoft Azure For Dummies
Book image
Explore Book Buy On Amazon
When it comes to Microsoft Azure, Cosmos DB is the data store solution. Relational databases are all about schema, or structure. Many say that to be a relational database administrator, you should be a micromanager, because every data row needs to be heavily constrained to fit the table, relationship, and database schema.

Although relational databases offer excellent data consistency, they tend to fall down in the scalability department because of all the schema overhead. Enter the nonrelational database.

Rather than call a nonrelational database schemaless, it’s more accurate to say that NoSQL databases have a flexible schema. At first, you may think that a flexible schema would make querying nearly impossible. But NoSQL databases can overcome this hurdle by partitioning the data set across multiple clustered nodes and apply raw compute power to the query execution.

NoSQL is generally understood to mean not only SQL.

Azure Cosmos DB features

Cosmos DB (originally called Document DB) is Azure’s multimodel, georeplicated, nonrelational data store. You can implement Cosmos DB in conjunction or instead of a relational database system.

Look below for a tour of Cosmos DB’s features.

Cosmos DB is Multimodel

Originally Document DB was a JavaScript Object Notation (JSON) document-model NoSQL database. Microsoft wanted to embrace a wider customer pool, however, so it introduced Cosmos DB, which supports five data models/application programming interfaces (APIs):
  • Core (SQL): This API is the successor to the original Document DB. The data store consists of JSON documents, and Core API provides a SQL-like query language that should be immediately comfortable for relational database administrators and developers.
  • Azure Cosmos DB for MongoDB API: This API supports the MongoDB wire protocol. MongoDB, also a JSON document store, allows you to query Cosmos DB as though it were a MongoDB instance.
  • Cassandra: This API is compatible with the Cassandra wide column store database and supports Cassandra Query Language.
  • Azure Table: This API points to the Azure storage account’s table service. It’s a key/value data store that you can access with ARM’s representational state transfer (REST) APIs.
  • Gremlin (graph): This API supports a graph-based data view and the Apache Gremlin query language.
Do you see a theme? The idea is that just about any developer who needs a NoSQL data store should be able to use Cosmos DB without sacrificing original source code or client-side tooling.

Azure Cosmos DB has turnkey global distribution

With a couple of mouse clicks, you can instruct Azure to replicate your Cosmos DB database to however many Azure regions you need to put your data close to your users.

Cosmos DB uses a multimaster replication scheme with a 99.999 percent availability service level agreement for both read and write operations.

Azure Cosmos DB has multiple consistency levels

Relational databases always offer strong consistency at the expense of speed and scale. Cosmos DB offers flexibility in this regard, allowing you to select (dynamically) any of five data consistency levels:
  • Strong: Reads are guaranteed to return the most recently committed version of an item. This level is the slowest-performing but most accurate.
  • Bounded Staleness, Session, and Consistent Prefix: These consistency levels offer balance between performance and consistent query results.
  • Eventual: Reads have no ordering guarantee. This choice is the fastest but least accurate.

Data consistency refers to the requirement that any database transaction change affected data only in allowed ways. With regard to read consistency specifically, the goal is to prevent two users from seeing different results from the same query due to incomplete database replication.

How to Create a Cosmos DB account

Now, you get down to business. The first task is getting Cosmos DB off the ground to create a Cosmos DB account. After you have the account, you can define one or more databases.

Follow these steps to create a Cosmos DB account:

  1. In the Azure portal, browse to the Azure Cosmos DB blade, and click Add.

    The Create Azure Cosmos DB Account blade appears.

  2. On the Create Azure Cosmos DB Account blade, complete the Basics page, using the following settings:
    • Account Name: This name needs to be unique in its resource group.
    • API: Select Core (SQL).
    • Geo-Redundancy: Don’t enable this option. If you do, Azure replicates your account to your region’s designated pair.
    • Multi-Region Writes: Don’t enable this option. You can always enable it later if you need multiple read/write replicas of the account throughout the world.
  3. Review the Network page.
  4. Click Review + Create and then click Create to submit the deployment.

Running and debugging a sample Cosmos DB application

One of the best ways to gain general familiarity with a new Azure service is to visit its Quick Start page. Let’s do this now with Cosmos DB. The following image shows Cosmo DB Quick Start page.

The Cosmos DB Quick Start blade The Cosmos DB Quick Start blade.

If you follow the Cosmos DB Quick Start tutorial, you can accomplish the following goals:

  • Create a new Cosmos DB database and container.
  • Download a .NET Core web application that connects to the Cosmos DB database.
  • Interact with the container and data store on your workstation by using Visual Studio.
To run a sample application, follow these steps:
  1. On your Cosmos DB account’s Quick Start blade, choose the .NET Core platform, and click Create ‘Items’ Container.

    Azure creates a container named ‘Items’ with 10 GB capacity and 400 request units (RUs) per second. As you may rightly guess, the RU is the standardized Cosmos DB performance metric.

  2. Click Download to download the preconfigured .NET Core web application.
  3. Unzip the downloaded archive on your system, and double-click the quickstartcore.sln solution file to open the project in Visual Studio 2019 Community Edition.
  4. In Visual Studio, build and run the solution by choosing Build→Build Solution.
  5. Choose Debug→Start Debugging to open the application in your default web browser.
  6. In the To-Do App with Azure DocumentDB web application that is now running in your web browser, click Create New, and define several sample to-do items.

    When you add to-do items, you’re populating the Items container in your Azure Cosmos DB database.

  7. Close the browser to stop debugging the application.

For extra practice, use the Visual Studio publishing wizard (which you find by right-clicking your project in Solution Explorer and choosing Publish from the shortcut menu) to publish this app to Azure App Service. This exercise is a great test of the skills you’ve developed thus far.

Interacting with Cosmos DB

To practice interacting with Cosmos DB and your database, return to the Azure portal and look at your Cosmos DB account settings. Here, some key settings are highlighted to make sure you know where to find them:
  • Data Explorer: Perform browser-based query and database configuration. The following image shows Data Explorer.

    Cosmos DB data explorer Cosmos DB Data Explorer is available directly in the Azure portal.
  • Replicate Data Globally: Click the map to replicate your Cosmos DB account to multiple regions. (Additional costs apply.)
  • Default Consistency: Switch among the five data consistency levels.
  • Firewall and Virtual Networks: Bind your Cosmos DB account to specific virtual network(s).
  • Keys: View the Cosmos DB account endpoint Uniform Resource Identifier, access keys, and connection strings.
Now follow these steps to interact with your new Cosmos DB database directly from the Azure portal:
  1. In your Cosmos DB account, select Data Explorer.
  2. Click the Open Full Screen button on the Data Explorer toolbar.

    This button takes you to Azure's Cosmo DB page in a separate browser tab, giving you more screen real estate to run queries.

    The ToDoList planet icon represents the icon, and the Items icon represents the container. The container is the Cosmos DB replication unit.

  3. Right-click the Items container and choose New SQL Query from the shortcut menu.

    The Save Query button is handy for retaining particularly useful queries.

  4. View your existing items by running the default query.

    The default query provided by Azure in the Data Explorer, SELECT * FROM c, retrieves all documents from your Items container.

    c is an alias for container. You’ll find the alias to be super useful when you work with the Cosmos DB Core API.

    query Cosmos DB Use Azure Data Explorer and SQL to query your Cosmos DB Core API items.
  5. Experiment with different SELECT queries.

    The double dash (--) denotes a one-line comment in Data Explorer. Try entering these queries in Data Explorer.

    -- select two fields from the container documents
    SELECT, c.description from c
    -- show only incomplete todo items
    SELECT * from c WHERE c.isComplete = false
  6. Use Data Explorer to update a document.

    In the SQL API tree view on the left side of the image above, expand the Items container, and select Items. Click the document you want to edit, and update the isComplete field on at least one document from “false” to “true,” or vice-versa.

    editing document in Cosmos DB Editing a document in a Cosmos DB collection
  7. Click the Update toolbar button to save your changes.

Bookmark the Microsoft Azure DB page that you can easily access Cosmos DB Data Explorer in a full-window experience. Also, Microsoft offers excellent Cosmos DB SQL query cheat sheets.

About This Article

This article is from the book:

About the book author:

Glen E. Clarke, A+, Network+, Security+, is an independent trainer and consultant. Ed Tetz, A+, MCSE, MCT, has written several guides to MCSE and other certifications. Timothy Warner, MCSE, MCT, A+, is an IT professional, technical trainer, and author.

This article can be found in the category: