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"
]
相关文章
相关标签/搜索