Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Azure icon
Labs

Import JSON into Cosmos DB

Azure Cosmos DB is Microsoft's globally distributed, multi-model database service. With a click of a button, Cosmos DB enables you to elastically and independently scale throughput and storage across any number of Azure regions worldwide. In this hands-on lab, we will export a table from our sample Azure SQL database to a JSON-formatted file. We will then use MongoImport to import this data into our Cosmos DB account as a new collection using PowerShell.

Azure icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 2h 0m
Published
Clock icon Nov 04, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Log into the Azure Portal and Prepare Azure Resources

    1. Log in to the Azure Portal with the credentials provided.

    2. Click on All Resources in the navigation hub menu and familiarize yourself with the Azure resources that have been provisioned, including:

      • An Azure virtual network and network security group allowing SSH access to virtual machines
      • One CentOS 7.5 virtual machine, along with supporting components (NIC, public IP, disk, etc.)
      • One Azure SQL database and Azure SQL server
      • One CosmosDB account

    Obtain Public IP Address for VM Access

    1. Click on the virtual machine named lab-VM
    2. Inside the virtual machine blade, note the public IP address for this virtual machine.

    Enable SQL Firewall Access to the Virtual Network

    1. Click on the SQL server named sqls-XXXXX, where XXXXX is a five-character unique lab ID for this lab.
    2. Inside the SQL server blade, click on Firewall and virtual networks.
    3. Click the + Add client IP button to add the IP of your local workstation to the SQL server firewall.
    4. Click Save to save the changes.

    Log On to the Virtual Machine

    1. Open up a terminal window on your local machine.

      • For MacOS and Linux workstations, you can use the Terminal application.
      • For Windows virtual machines, you may have to install an SSH client such as PuTTY.
    2. In the terminal, connect to lab-VM by typing in the following (replacing ip.address.of.vm with the IP you noted earlier):

      ssh [email protected]
      
    3. Type yes to confirm connection, and enter the password for the virtual machine. The password can be located in the credentials section of the lab window.

  2. Challenge

    Install Database Tools on the Virtual Machine

    Reference MongoDB repository

    The mongodb-org package does not exist within the default repositories for CentOS. However, MongoDB maintains a dedicated repository. Let’s add it to our server.

    sudo su 
    

    Enter the password for the virtual machine if prompted. The password can be located in the credentials section of the lab window.

    curl https://raw.githubusercontent.com/linuxacademy/content-azure-az301-labs/master/mongodb-org.repo > /etc/yum.repos.d/mongodb-org.repo
    
    exit
    

    Install MongoDB Tools

    Install the mongodb-org package from the third-party repository using the yum utility:

    sudo yum install mongodb-org-tools
    

    Enter the password for the virtual machine if prompted. The password can be located in the credentials section of the lab window.

    Enter y twice when prompted to confirm the installation and import the GPG key.

  3. Challenge

    Export the Customers Table from the SQL Database

    1. Back in the Azure Portal, click on All Resources, then on the SQL database named sqldb-XXXXX, where XXXXX is a five-character unique lab ID for this lab.
    2. Click on Query editor (preview) in the blade navigation menu.
    3. Sign in to the Azure database using the following credentials:
    Username: acgadmin
    Password: S5w3R9Lbu*GvgfB5WWHg
    
    1. Enter the following T-SQL query into the window:

      SELECT TOP (10) * FROM [SalesLT].[Customer]
      
    2. Click Run to execute the query.

    3. Click the Export data as .json button to download a JSON file to your local workstation.

    4. Open this file, named Query1.json, in a text editor on your local workstation.

    5. Press Control+A on your keyboard (Command+A on MacOS) and then press Control+C (Command+C on MacOS) to copy the contents of the file to the clipboard.

    6. Return to the virtual machine, and use the vi editor to create a new file:

      vi customer.json
      
    7. In the vi editor, press i to insert new text into the file, and then press Control+V (Command+V on MacOS) to paste the text into the file.

    8. Save the file by pressing Escape, then :wq, and then Enter. You will be returned to the shell.

  4. Challenge

    Import the JSON File into the CosmosDB Database as a New Collection

    1. In the Azure Portal, click on All Resources, then on the Cosmos DB account named cosmos-XXXXX, where XXXXX is a five-character unique lab ID for this lab.

    2. In the cosmos-XXXXX blade, click on Connection String. You will need information on this pane to complete this objective.

    3. Return to the virtual machine, and run the following command to import the JSON file into the Cosmos DB account using mongoimport:

      mongoimport -h exampledevto.documents.azure.com:10255 
      -d ImportedSQL -c Customer -u exampledevto 
      -p  YOURPASSWORDHERE --ssl --jsonArray --file customer.json 
      
      • Replace exampledevto.documents.azure.com with the HOST value under Connection String in the Azure Portal.
      • Replace exampledevto with the USERNAME value under Connection String in the Azure Portal.
      • Replace YOURPASSWORDHERE with the PRIMARY PASSWORD value under Connection String in the Azure Portal.
    4. Your command should resemble the following:

      mongoimport -h cosmos-dcru5.documents.azure.com:10255 -d Customer -c example -u cosmos-dcru5 
      -p  2vlxaaoaWcgKhGE1XAaNA1nBrvOnJzA299PyqyZDJxlo2PJFX9JNojxZZ5givkDUOsgR0KKwUvcjh16rmU9T5g== 
      --ssl --jsonArray --file customer.json
      
  5. Challenge

    View Your Imported Data in the Azure Portal

    Enable Cosmos Firewall Access to Your IP Address

    1. Click on the Cosmos DB account named cosmos-XXXXX, where XXXXX is a five-character unique lab ID for this lab.
    2. Inside the Cosmos DB blade, click on Firewall and virtual networks.
    3. Under the Firewall section of the pane, click the + Add my current IP button to add the IP of your local workstation to the Cosmos DB firewall.
    4. Click Save to save the changes.
    5. While remaining in the Cosmos DB blade, click on Data Explorer. Note the new collection named Customer (you may need to refresh the collections).
    6. Expand Customer, and then click on Documents.
    7. Click on several of the documents that appear in the list and verify that they contain customer data.

    CosmosDB results

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans