Azure CosmosDB (12) 建立Cosmos DB並執行查詢語句 Windows Azure Platform 系列文章目錄

  《Windows Azure Platform 系列文章目錄html

 

  The SQL API supports the following aggregate functions. SUM and AVG operate on numeric values, and COUNT, MIN, and MAX work on numbers, strings, Booleans, and nulls.網絡

 

  在本章節,咱們將使用Azure Portal,建立1個Azure Cosmos DB。post

  1.首先,咱們登陸Azure China Portal: https://portal.azure.cnui

  2.選擇Azure Cosmos DBurl

  

  

  3.建立新的資源組,命名爲cosmos-rgspa

  建立新的account name:leicosmos3d

  API設置爲Core(SQL)code

  

 

  4.CosmosDB暫時不選擇虛擬網絡,圖略。orm

  注意:建立完畢後,cosmos DB並不會開始收費,由於咱們沒有分配RUhtm

 

  5.建立完畢後,咱們選擇該資源,點擊Data Explorer

  點擊New Database,咱們輸入新的database id

  

  如上圖所示,RU能夠分配在Database級別。這樣Database下面的全部Collection都共享這個Database的RU

  咱們這裏暫時不勾選Provision throughput。

  注意:建立Database完畢後,cosmos DB並不會開始收費,由於咱們沒有分配RU

 

  6.在上一步操做完畢後,咱們點擊Products,點擊右鍵,New Collection

  

 

  7.在右側彈出的窗口中,輸入下面的信息:

  

  咱們定義了Partition Key分區鍵爲/country,

  Throughput最小爲400

  定義了Unique Keys爲/productionId

  這個操做完畢後,cosmos DB已經開始收費,由於咱們已經分配RU

 

  8.建立完畢後,界面展現以下:

  

  Database Level爲Products

  Collection Level爲clothing

 

  9.咱們能夠在界面上,插入新的Document。以下圖:

  

 

  10.咱們插入第一條數據

{
   "id": "1",
   "productId": "33218896",
   "country":"China",
   "category": "Women's Clothing",
   "manufacturer": "Contoso Sport",
   "description": "Quick dry crew neck t-shirt",
   "price": "14.99",
   "shipping": {
       "weight": 1,
       "dimensions": {
       "width": 6,
       "height": 8,
       "depth": 1
      }
   }
}

  點擊Save保存。

 

  執行成功後,截圖以下:

   

 

 

  10.而後咱們重複步驟9中的New Document,插入第二條數據

  請注意:咱們在步驟7中,建立新的Collection的時候,定義了Unique Key爲productId

  表示在同一個Partition Key中,productId不能重複

  

  觀察下面的數據中,"country":"China",是與步驟9中的數據,保存在同一個Partition Key裏

  "productId": "33218896",是與上面步驟9中的數據相同

{
   "id": "2",
   "productId": "33218896",
   "country":"China",
   "category": "Women's Outerwear",
   "manufacturer": "Contoso",
   "description": "Black wool pea-coat",
   "price": "49.99",
   "shipping": {
       "weight": 2,
       "dimensions": {
       "width": 8,
       "height": 11,
       "depth": 3
      }
   }
}

 

  執行Save操做,會出現約束錯誤

  

  

  11.咱們在修改productId爲33218897。插入下面的值

{
   "id": "2",
   "productId": "33218897",
   "country":"China",
   "category": "Women's Outerwear",
   "manufacturer": "Contoso",
   "description": "Black wool pea-coat",
   "price": "49.99",
   "shipping": {
       "weight": 2,
       "dimensions": {
       "width": 8,
       "height": 11,
       "depth": 3
      }
   }
}

  執行成功

 

  12.咱們還能夠插入其餘值:

{
   "id": "3",
   "productId": "33218898",
   "country":"US",
   "category": "Women's Outerwear",
   "manufacturer": "Contoso",
   "description": "Black wool pea-coat",
   "price": "29.99",
   "shipping": {
       "weight": 1,
       "dimensions": {
       "width": 9,
       "height": 4,
       "depth": 9
      }
   }
}

  這樣一共就插入了三個數據

 

 

  Query

  13.咱們執行下面的語句:

SELECT * FROM Products c where c.id="1"

  查詢結果

[
    {
        "id": "1",
        "productId": "33218896",
        "country": "China",
        "category": "Women's Clothing",
        "manufacturer": "Contoso Sport",
        "description": "Quick dry crew neck t-shirt",
        "price": "14.99",
        "shipping": {
            "weight": 1,
            "dimensions": {
                "width": 6,
                "height": 8,
                "depth": 1
            }
        },
        "_rid": "BekdAKyBIekBAAAAAAAAAA==",
        "_self": "dbs/BekdAA==/colls/BekdAKyBIek=/docs/BekdAKyBIekBAAAAAAAAAA==/",
        "_etag": "\"00001a00-0000-4200-0000-5cb6e4dd0000\"",
        "_attachments": "attachments/",
        "_ts": 1555490013
    }
]

 

  14.咱們執行下面的語句

SELECT
    p.id,
    p.manufacturer,
    p.description
FROM Products p
WHERE p.id ="1"

  執行結果:

[
    {
        "id": "1",
        "manufacturer": "Contoso Sport",
        "description": "Quick dry crew neck t-shirt"
    }
]

 

  FROM

  15.咱們執行下面的語句:

SELECT * FROM Products.shipping

  執行結果:

[
    {
        "weight": 1,
        "dimensions": {
            "width": 6,
            "height": 8,
            "depth": 1
        }
    },
    {
        "weight": 2,
        "dimensions": {
            "width": 8,
            "height": 11,
            "depth": 3
        }
    },
    {
        "weight": 1,
        "dimensions": {
            "width": 9,
            "height": 4,
            "depth": 9
        }
    }
]

 

  16.咱們執行下面的語句

SELECT * FROM Products.shipping.weight

  執行結果

[
    1,
    2,
    1
]

 

  WHERE

  17.咱們執行下面的語句

SELECT p.description
FROM Products p
WHERE p.id = "1"

  執行結果

[
    {
        "description": "Quick dry crew neck t-shirt"
    }
]

 

  Order By clause

  18.咱們執行下面的語句

SELECT p.price, p.description, p.productId
FROM Products p
ORDER BY p.price ASC

  執行結果:

[
    {
        "price": "14.99",
        "description": "Quick dry crew neck t-shirt",
        "productId": "33218896"
    },
    {
        "price": "29.99",
        "description": "Black wool pea-coat",
        "productId": "33218898"
    },
    {
        "price": "49.99",
        "description": "Black wool pea-coat",
        "productId": "33218897"
    }
]

 

  JOIN

  19.咱們執行下面的語句:

SELECT p.productId
FROM Products p
JOIN p.shipping

  執行結果:

[
    {
        "productId": "33218896"
    },
    {
        "productId": "33218897"
    },
    {
        "productId": "33218898"
    }
]

 

  

  查詢MAX

  20.咱們執行下面的語句

SELECT value MAX(c.productId) FROM clothing c

  執行結果

[
    "33218898"
]
相關文章
相關標籤/搜索