Azure

Azure CosmosDB Utilizing SQL API

Introduction

 

On this tutorial, we’re going to focus on Azure CosmosDB and run just a few queries utilizing one of many APIs that’s really useful when creating new functions. Earlier than this, let’s perceive what Azure CosmosDB is, and why it is wonderful.

 

Azure CosmosDB is a NoSQL Database. By NoSQL, we imply that it shops knowledge in a Non-Relational Format. It’s a document-based database system. Nevertheless, not like others, it’s Multi-Mannequin. Which means there are a number of methods to entry and retailer knowledge in it, relying on the enterprise use circumstances.

  1. You may migrate knowledge from Cassendra and MongoDB and use Azure CosmosDB as a backend retailer and run queries of Cassendra or MongoDB, whichever you used. Your backend will change and the applying will stay working with minimal adjustments.
  2. You need to use Azure Desk Storage API, which implies you possibly can change the backend from Azure Desk Storage to Azure CosmosDB by working the identical Queries.
  3. If the enterprise calls for a Graph Database-Primarily based Question Mannequin, we will use Gremlin API to get issues carried out.
  4. If we’re constructing an software from scratch, we will use SQL API that’s strongly really useful for working with Azure. The queries we run utilizing SQL API provide question construction much like SQL with some limitations.

Azure CosmosDB is cloud-based. It presents low-latency (<=10ms). It ensures excessive availability i.e. 99.99% Availability and 99.999% in Geo-Replication. It could possibly scale very giant and ensures limitless scaling with excessive workloads.

 

It has enterprise-level safety.

 

Anybody can do hands-on with out problem. This can enable builders to do simple on-boarding and enhance their abilities: https://www.documentdb.com/sql/demo

 

Extra Particulars: https://azure.microsoft.com/en-in/providers/cosmos-db/

 

Storage Construction

 

Right here is the diagram illustration of how a doc is organized internally:

 

Extra particulars: https://docs.microsoft.com/en-us/azure/cosmos-db/account-databases-containers-items

 

Palms-On

 

Now, we’re going to work on CosmosDB utilizing SQL API.

 

Comply with the steps under:

  1. SELECT * FROM Individuals  

 

To rely all data:

 

Technique 1

  1. SELECT COUNT(P) FROM Individuals P  
  1. [  
  2.     {  
  3.         “$1”: 4  
  4.     }  
  5. ]  

Technique 2

  1. SELECT VALUE COUNT(P) FROM Individuals P   
  1. [  
  2.     4  
  3. ]  

Notice

WON’T WORK – SELECTCOUNT[  

  •     {  
  •         “firstname”“亜妃子”,  
  •         “lastname”“NotSet”,  
  •         “age”: 5  
  •     },  
  •     {  
  •         “firstname”“Eva”,  
  •         “lastname”“NotSet”,  
  •         “age”: 44  
  •     },  
  •     {  
  •         “firstname”“John”,  
  •         “lastname”“NotSet”,  
  •         “age”: 23  
  •     },  
  •     {  
  •         “firstname”“Véronique”,  
  •         “lastname”“NotSet”,  
  •         “age”: 50  
  •     },  
  •     {  
  •         “firstname”“Varun”,  
  •         “lastname”“Setia”,  
  •         “age”: 30  
  •     }  
  • ] FROM Individuals

     

    Failed to question merchandise for container Individuals: Gateway Did not Retrieve Question Plan: Message: {“errors”:[{“severity”:”Error”,”location”,

    {“start”:13,”end”:14},”code”:”SC1001″,”message”:”Syntax error, incorrect syntax near ‘*’.”}]} ActivityId: 5d1842d2-d232-4948-a4c8-21599f2980c4, Microsoft.Azure.Paperwork.Frequent/2.11.0, Microsoft.Azure.Paperwork.Frequent/2.11.0

     

  • Deciding on Particular Columns

    1. SELECT P.firstname,P.age FROM Individuals P  
    1. [  
    2.     {  
    3.         “firstname”“亜妃子”,  
    4.         “age”: 5  
    5.     },  
    6.     {  
    7.         “firstname”“Eva”,  
    8.         “age”: 44  
    9.     },  
    10.     {  
    11.         “firstname”“John”,  
    12.         “age”: 23  
    13.     },  
    14.     {  
    15.         “firstname”“Véronique”,  
    16.         “age”: 50  
    17.     }  
    18. ]  
    1. SELECT P.firstname,P.age FROM Individuals P WHERE P.age>20 AND P.age<50  
    1. [  
    2.     {  
    3.         “firstname”“Eva”,  
    4.         “age”: 44  
    5.     },  
    6.     {  
    7.         “firstname”“John”,  
    8.         “age”: 23  
    9.     }  
    10. ]  

    Array Information

    1. SELECT [P.firstname,P.age] AS Array_fname_age FROM Individuals P  
    1. [  
    2.     {  
    3.         “Array_fname_age”: [  
    4.             “亜妃子”,  
    5.             5  
    6.         ]  
    7.     },  
    8.     {  
    9.         “Array_fname_age”: [  
    10.             “Eva”,  
    11.             44  
    12.         ]  
    13.     },  
    14.     {  
    15.         “Array_fname_age”: [  
    16.             “John”,  
    17.             23  
    18.         ]  
    19.     },  
    20.     {  
    21.         “Array_fname_age”: [  
    22.             “Véronique”,  
    23.             50  
    24.         ]  
    25.     }  
    26. ]  

    Return Data with Particular Key in JSON

    1. SELECT P.firstname,P.lastname,P.age FROM Individuals P WHERE IS_DEFINED(P.lastname)  
    1. [  
    2.     {  
    3.         “firstname”“Varun”,  
    4.         “lastname”“Setia”,  
    5.         “age”: 30  
    6.     }  
    7. ]  

    To Preserve Information Consistency

     

    Suppose one doc has lastname and others don’t. We use Coalesce

    1. SELECT P.firstname,P.lastname??“NotSet” AS lastname,P.age FROM Individuals P  
    1. [  
    2.     {  
    3.         “firstname”“亜妃子”,  
    4.         “lastname”“NotSet”,  
    5.         “age”: 5  
    6.     },  
    7.     {  
    8.         “firstname”“Eva”,  
    9.         “lastname”“NotSet”,  
    10.         “age”: 44  
    11.     },  
    12.     {  
    13.         “firstname”“John”,  
    14.         “lastname”“NotSet”,  
    15.         “age”: 23  
    16.     },  
    17.     {  
    18.         “firstname”“Véronique”,  
    19.         “lastname”“NotSet”,  
    20.         “age”: 50  
    21.     },  
    22.     {  
    23.         “firstname”“Varun”,  
    24.         “lastname”“Setia”,  
    25.         “age”: 30  
    26.     }  
    27. ]  

    To get all IDs as an array:

    1. SELECT * FROM Individuals.id   
    1. [  
    2.     “e141ad63-ea4b-4778-9788-1d39ec7956dd”,  
    3.     “9cbac020-2b79-460b-b037-9af18ab21f42”,  
    4.     “a82ae393-fdf4-4f78-a66e-d3b2a98d1696”,  
    5.     “20566083-a724-4e02-a035-c17691bf17ba”,  
    6.     “65313e59-9c7c-4fe5-bef8-e0891b4b7cd0”  
    7. ]   

    Mixed Question Array and JSON

    1. SELECT P.id,[P.firstname,P.lastname] AS fullname FROM Individuals P  
    1. [  
    2.     {  
    3.         “id”“e141ad63-ea4b-4778-9788-1d39ec7956dd”,  
    4.         “fullname”: [  
    5.             “亜妃子”  
    6.         ]  
    7.     },  
    8.     {  
    9.         “id”“9cbac020-2b79-460b-b037-9af18ab21f42”,  
    10.         “fullname”: [  
    11.             “Eva”  
    12.         ]  
    13.     },  
    14.     {  
    15.         “id”“a82ae393-fdf4-4f78-a66e-d3b2a98d1696”,  
    16.         “fullname”: [  
    17.             “John”  
    18.         ]  
    19.     },  
    20.     {  
    21.         “id”“20566083-a724-4e02-a035-c17691bf17ba”,  
    22.         “fullname”: [  
    23.             “Véronique”  
    24.         ]  
    25.     },  
    26.     {  
    27.         “id”“65313e59-9c7c-4fe5-bef8-e0891b4b7cd0”,  
    28.         “fullname”: [  
    29.             “Varun”,  
    30.             “Setia”  
    31.         ]  
    32.     }  
    33. ]  
    Wish to know extra? Go to right here.

     

    Thanks for studying. Additionally, please share your ideas and opinions.

    Show More

    Related Articles

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Back to top button