Quick Start With Azure Cosmos DB - Day Five

Quick Start With Azure Cosmos DB - Day Five

In this article, we will see how we can execute the SQL queries on JSON documents and get filtered results.

Please go through my previous articles of this series to get a complete understanding of the topic.

So far, we have created Cosmos DB and documents under collection. Now, we need to check how we can fetch the records using SQL queries.

  • Log into the Azure portal.
  • Select Azure Cosmos DB Service and select the database, i.e., Recharge in our case.
  • Expand the Prepaid collection and click on Documents, which will load the first 100 documents. In order to load more documents, you should click on the "Load More" link which will load another 100 documents.
  • On top of the documents list, we can see this SQL query - "SELECT * FROM c".
  • Click on the "Edit Filter" button.

image.png

Once we click the "Edit Filter" button, it will enable the textbox to write the query and convert it into the "Apply Filter" button.

Write your filter query in the textbox and click on the "Apply Filter" button.

image.png

The filtered result will be displayed. Just click on id to explore the document.

image.png

Now, let's see how we can download the complete dump or our filtered result in the form of JSON document. For this, we need to download a free migration tool from the Microsoft website.

image.png

In order to connect this tool to our Cosmos DB, a connection string is required which we can get from the Azure portal.

Under Settings, click on Keys where we get the URI, two keys, and two connection strings. You can use any of the connection strings to connect to Cosmos DB. The question may arise why two sets of keys and connection strings are given. The reason behind is that you can use one connection string while changing the other in production.

image.png

Select "DocumentDB" for the "Import from" field.

Copy and paste the connection string and append the "Database=Recharge" separated by a semi colon (;) at last.

Enter Collection as 'Prepaid'.

Enter your SQL query and click on the "Next" button.

image.png

Select JSON file as the "Export" option to export them as JSON document.

Select "Local file" radio button and select the path where you want to save the .json document.

Check the "Prettify JSON" checkbox and click the "Next" button.

image.png

Select the Error Log file path to capture the error info if any error occurrs and click the "Next" button.

image.png

Verify the provided information and click on "Import".

image.png

The imported results will be displayed with elapsed time and total transferred documents.

image.png

Explore to the given local path of the .json file and open the file.

You can find all the documents in JSON array format.

Transaction.json

    [  
      {  
        "id": "3d3e9785-66ff-4560-8a16-99fa80c69401",  
        "Operator": "JIO",  
        "Provider": "CyberPlat",  
        "Region": "Telengana",  
        "Mobile": "9769496026",  
        "Amount": "500",  
        "Status": "Pending",  
        "ExecutionTime": "500 ms",  
        "CreatedDate": "2018-06-23T22:01:44.3895961+05:30"  
      },  
      {  
        "id": "ef200d4d-e3ae-4437-9b4c-c017b21f80fa",  
        "Operator": "Idea",  
        "Provider": "Paytm",  
        "Region": "Gujarat",  
        "Mobile": "9879752926",  
        "Amount": "100",  
        "Status": "Success",  
        "ExecutionTime": "200 ms",  
        "CreatedDate": "2018-06-23T22:01:43.670247+05:30"  
      },  
      {  
        "id": "7da59e87-bb7a-4663-b28c-5fcaf5e78eb2",  
        "Operator": "Airtel",  
        "Provider": "Paytm",  
        "Region": "Maharashtra",  
        "Mobile": "9879752926",  
        "Amount": "500",  
        "Status": "Fail",  
        "ExecutionTime": "500 ms",  
        "CreatedDate": "2018-06-23T22:01:43.4771099+05:30"  
      },  
      {  
        "id": "2098aa87-09f6-4284-9794-06619a3f26fd",  
        "Operator": "Airtel",  
        "Provider": "Paytm",  
        "Region": "Telengana",  
        "Mobile": "9825134178",  
        "Amount": "500",  
        "Status": "Pending",  
        "ExecutionTime": "200 ms",  
        "CreatedDate": "2018-06-23T22:01:43.322699+05:30"  
      },  
      {  
        "id": "153211d2-8227-46b9-8f4f-7e7bedbddd87",  
        "Operator": "Airtel",  
        "Provider": "Paytm",  
        "Region": "Gujarat",  
        "Mobile": "9879752926",  
        "Amount": "200",  
        "Status": "Fail",  
        "ExecutionTime": "500 ms",  
        "CreatedDate": "2018-06-23T22:01:43.1086247+05:30"  
      },  
      {  
        "id": "236ca011-7e7a-423e-9b4e-7ab1ec771f1e",  
        "Operator": "Airtel",  
        "Provider": "CyberPlat",  
        "Region": "Telengana",  
        "Mobile": "9879752926",  
        "Amount": "200",  
        "Status": "Fail",  
        "ExecutionTime": "200 ms",  
        "CreatedDate": "2018-06-23T22:01:42.9233247+05:30"  
      },  
      {  
        "id": "f0a34736-1f44-4ce2-b65d-85993a9dfa8c",  
        "Operator": "Airtel",  
        "Provider": "Euronet",  
        "Region": "Telengana",  
        "Mobile": "9879752926",  
        "Amount": "500",  
        "Status": "Pending",  
        "ExecutionTime": "200 ms",  
        "CreatedDate": "2018-06-23T22:01:42.7441271+05:30"  
      },  
      {  
        "id": "0864f091-213b-4f3e-8e5d-a1be181c347a",  
        "Operator": "JIO",  
        "Provider": "Euronet",  
        "Region": "Telengana",  
        "Mobile": "9769496026",  
        "Amount": "200",  
        "Status": "Fail",  
        "ExecutionTime": "500 ms",  
        "CreatedDate": "2018-06-23T22:01:42.5671958+05:30"  
      },  
      {  
        "id": "21069cf0-4736-44c8-b90b-4f877614cc04",  
        "Operator": "Vodafone",  
        "Provider": "Paytm",  
        "Region": "Telengana",  
        "Mobile": "9825134178",  
        "Amount": "500",  
        "Status": "Pending",  
        "ExecutionTime": "200 ms",  
        "CreatedDate": "2018-06-23T22:01:42.3832615+05:30"  
      },  
      {  
        "id": "e060a604-b1e6-4f8e-bbce-44204922db8c",  
        "Operator": "Vodafone",  
        "Provider": "Euronet",  
        "Region": "Telengana",  
        "Mobile": "9879752926",  
        "Amount": "200",  
        "Status": "Success",  
        "ExecutionTime": "200 ms",  
        "CreatedDate": "2018-06-23T22:01:36.8072284+05:30"  
      },  
      {  
        "id": "4bd8a242-55bf-416e-a50c-e7c1a9439d7c",  
        "Operator": "Airtel",  
        "Provider": "CyberPlat",  
        "Region": "Gujarat",  
        "Mobile": "9879752926",  
        "Amount": "200",  
        "Status": "Success",  
        "ExecutionTime": "500 ms",  
        "CreatedDate": "2018-06-23T21:57:39.665767+05:30"  
      },  
      {  
        "id": "ee5aec6d-d403-4b46-a918-84eeed910b9b",  
        "Operator": "Idea",  
        "Provider": "Paytm",  
        "Region": "Maharashtra",  
        "Mobile": "9879752926",  
        "Amount": "100",  
        "Status": "Pending",  
        "ExecutionTime": "500 ms",  
        "CreatedDate": "2018-06-23T21:55:40.2549335+05:30"  
      },  
      {  
        "id": "91b762c7-01bc-472d-90eb-cbb1bda06949",  
        "Operator": "Vodafone",  
        "Provider": "Euronet",  
        "Region": "Gujarat",  
        "Mobile": "9769496026",  
        "Amount": "500",  
        "Status": "Success",  
        "ExecutionTime": "200 ms",  
        "CreatedDate": "2018-06-23T17:27:10.2003496+05:30"  
      }]