Work with Azure Cosmos DB Data Using Two Azure SDKs

45 minutes
  • 4 Learning Objectives

About this Hands-on Lab

In this lab, you get a chance to practice with four fundamental Cosmos DB classes representing the Cosmos client, database, container, and item. You will author code in either the .NET C# SDK or the Python SDK — or both if you want an additional challenge. The focus of the lab is to get you comfortable with these key classes, so you will:

– Connect to an existing Cosmos DB account.
– Add a database to the account.
– Add a container to the database.
– Add at least one item to the container.
– Confirm that you successfully created the required objects using the Azure portal.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Housekeeping
  1. Open an incognito or in-private window and log in to the Azure portal using the user name and password provided in the lab environment.
  2. From within the portal, initiate the Cloud Shell to select Bash (versus PowerShell) and set up with new backing storage — do not use the existing storage account deployed into the lab environment. You will need both storage accounts to complete the lab.

    Note: You are free to write the code for this lab in Visual Studio Code or another IDE, if you have experience in that environment. Just make sure you download the GitHub project file to ensure you have instructions and hints. Be aware that the lab guide and the solution video are based on working in the Cloud Shell editor, but it won’t substantially change the code you write.

Prepare Environment

At this point, you should be at the command prompt in the Cloud Shell environment.

Clone the Lab Project

From the Bash command prompt, execute the git clone command using the URL provided in the Additional Information and Resources section of the lab, followed by AZ204_Labs to alias the downloaded folder to a friendly name.

.NET C# Prep

  • Once the project is downloaded, use the Cloud Editor to open the program.cs file in the AZ204_Labs/work_with_cosmos/dotnet folder.
  • From the Bash command prompt, change to the working directory: cd AZ204_Labs/work_with_cosmos/dotnet.

Python Prep

  • Once the project is downloaded, use the Cloud Editor to open the samplecode.py file in the AZ204_Labs/work_with_cosmos/python folder.
  • From the Bash command prompt, change to the working directory: cd AZ204_Labs/work_with_cosmos/python.
  • Run this command to install the library we will be working with in the lab: pip install azure-cosmos.

Get Connection String — Regardless of SDK

Minimize (do not close) the Cloud Shell pane and navigate to the Azure Cosmos DB for NoSQL account that is already set up for you. The name of the account starts with "pluralsightlab."
Navigate to the location of the primary connection string and copy it. Be sure to copy the connection string and not just the key. Return to your Cloud Shell window.

Use .NET to Create a Cosmos DB Database, Container, and Item

You should have either program.cs (for the .NET SDK) open in the Cloud Shell editor.

Follow the instructions and hints in the code file to modify and author additional code to:

  1. Populate the my_connection_string variable with the primary connection string you retrieved from the Cosmos DB account in the portal.
  2. Instantiate the Cosmos client object.
  3. Create a new database.
  4. Create a new container.
  5. Add an item to the container.

When you are ready, be sure to save your file in the code editor and then excute it at the command line with: dotnet build, which will surface any errors, and then: dotnet run.

Navigate to the Cosmos DB account in the Azure portal to confirm the creation of a new container and a blob in that container.

Tips:

  • If the instructions in the code file tells you to use an exact name for a deployed resource, be sure to follow those instructions, as the lab grading tool may be looking specifically for that name. Also, you don’t want your resource names to conflict if you decide to complete the lab using both SDKs.
  • See the Additional Information and Resources section of the lab for links to SDK reference material to help you author the code.
  • If you still need help after considering these tips, you can copy-paste the code from the lab guide and/or watch the solution video.
Use Python to Create a Cosmos DB Database, Container, and Item

You should have samplecode.py (for the Python SDK) open in the Cloud Shell editor.

Follow the instructions and hints in the code file to modify and author additional code to complete the following actions:

  1. Populate the my_connection_string variable with the primary connection string you retrieved from the Cosmos DB account in the portal.
  2. Instantiate the Cosmos client object.
  3. Create a new database.
  4. Create a new container.
  5. Add an item to the container.

When you are ready, be sure to save your file in the code editor and then execute it at the command line with: python samplecode.py.

Navigate to the Cosmos DB account in the Azure portal to confirm the creation of a new container and a blob in that container.

Tips:

  • If the instructions in the code file tells you to use an exact name for a deployed resource, be sure to follow those instructions, as the lab grading tool may be looking specifically for that name. Also, you don’t want your resource names to conflict if you decide to complete the lab using both SDKs.
  • See the Additional Information and Resources section of the lab for links to SDK reference material to help you author the code.
  • If you still need help after considering these tips, you can copy-paste the code from the lab guide and/or watch the solution video.

Additional Resources

Imagine you are leading a newly formed team of developers, with a mix of both C# and Python expertise among the team members. The solution architect is keen to investigate Azure Cosmos DB as a potential database solution for a number of upcoming projects and has asked you for a quick demonstration to show that you can leverage the existing coding expertise on your team without a lot of re-training.

You have partnered with the lead developer to do a live, side-by-side demo of base operations over an Azure Cosmos DB implementation using the Python SDK and .NET SDK for Azure Cosmos DB for NoSQL.

You will start with a simple, skeletal code file for each language, with plenty of comments and instructions provided so that others can follow the same exercise on their own, later.

Use this GitHub link to clone the required files when you are prompted in the lab objectives: https://github.com/pluralsight-cloud/content-az-204-developing-solutions-for-microsoft-azure.git.

This is a reference to the .NET SDK for Azure Cosmos DB for NoSQL: https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/how-to-dotnet-get-started.

These are references to the Python SDK for Azure Cosmos DB for NoSQL:

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?