azure api 管理_使用API使用Azure Cosmos DB进行图形数据库实现
azure api 管理
In my previous article, I’ve discussed a lot about the Graph database implementation with SQL Server 2017. In this case, we’ll see a walk-through of Graph API integration with Azure Cosmos DB.
在上一篇文章中,我讨论了很多有关使用SQL Server 2017实现Graph数据库的问题 。 在这种情况下,我们将看到Graph API与Azure Cosmos DB集成的演练。
Before we jump into the concepts though, let’s take a high-level overview of NoSQL databases. A NoSQL database is designed in such a way that no extra efforts are needed for the database to be distributed because NoSQL Database designed that way.
在进入概念之前,让我们对NoSQL数据库进行一个高级概述。 NoSQL数据库的设计方式是,由于NoSQL数据库是通过这种方式设计的,因此无需付出额外的精力即可分配数据库。
Note: In one of my previous article, I talked about differences between SQL vs NoSQL. I would recommend reading it for the better understanding of NoSQL concepts.
注意:在上一篇文章中,我谈到了 SQL与NoSQL 之间的区别 。 我建议阅读它,以更好地理解NoSQL概念。
什么是Azure Cosmos DB? (What is Azure Cosmos DB?)
Having explained the basic characteristics of a NoSQL database, we can now take a look at what Azure Cosmos DB is all about. In sort, it is an extension of Document DB, which has been Microsoft’s NoSQL document database running on Microsoft Azure since 2015. Cosmos DB capabilities started with Document DB, which was already delivering low latency and high availability for schema-free JSON documents.
在说明了NoSQL数据库的基本特征之后,我们现在可以看看Azure Cosmos DB的全部含义。 从某种意义上说,它是Document DB的扩展,Document DB是自2015年以来在Microsoft Azure上运行的Microsoft NoSQL文档数据库。CosmosDB功能始于Document DB,它已经为无模式的JSON文档提供了低延迟和高可用性。
Azure Cosmos DB is Microsoft’s globally distributed, fully managed, multi-model database service with global distribution of the data, elastic scaling, automatic tuning, and provides querying capabilities, and it also support Gremlin standard. You can quickly create and query document, key/value, and graph databases, all of which benefit from the global distribution and horizontal scale capabilities at the core of Azure Cosmos DB. It also provides the ability to use multiple models like document and graph over the same data. For example, you can use a document collection to store graph data side by side with documents, and use both SQL queries over JSON and Gremlin queries to query the collection.
Azure Cosmos DB是Microsoft的全球分布式,完全托管的多模型数据库服务,具有数据的全局分布,弹性伸缩,自动调整,并提供查询功能,并且还支持Gremlin标准。 您可以快速创建和查询文档,键/值和图形数据库,所有这些都受益于Azure Cosmos DB核心的全局分布和水平缩放功能。 它还提供对同一数据使用多个模型(如文档和图形)的功能。 例如,您可以使用文档集合与文档并排存储图形数据,并同时使用基于JSONSQL查询和Gremlin查询来查询集合。
先决条件 (Prerequisites)
- Requires an Azure subscription 需要Azure订阅
- If you don’t have one, create a free Azure subscription account 如果您没有,请创建一个免费的Azure订阅帐户
- Basic knowledge of Graph databases 图数据库的基本知识
- Gremlin query Gremlin查询
- Basic understanding of JSON documents 基本了解JSON文档
入门 (Getting started)
Let’s jump in and get started.
让我们开始吧。
The following step by step details gives you the required information to understand the concepts of the design of Azure Cosmos DB. In this section, let us a take a look at the ways to create, query, and traverse the Graph database models.
以下分步详细信息为您提供了必要的信息,以了解Azure Cosmos DB设计的概念。 在本节中,让我们看一下创建,查询和遍历Graph数据库模型的方法。
- To login to the Azure portal, browse portal.azure.com and enter the required credentials 若要登录到Azure门户,请浏览portal.azure.com并输入所需的凭据
-
To create the Azure Cosmos DB, on the left part of the pane, click + New button and type the search string “Cosmos” to lookup for Azure Cosmos DB component
+新建按钮,然后键入搜索字符串“ Cosmos”以查找Azure Cosmos DB组件
-
Click the Create button at the bottom of the screen
创建按钮
-
In the Azure Cosmos DB New account form, enter the required configuration for the Azure Cosmos DB account
-
The supported programming models are:
- Key-Value
- Column family
- Documents
- Graph
- 支持的编程模型是:
- 核心价值
- 列族
- 文件资料
- 图形
-
In the New Account page, enter the settings for the new Azure Cosmos DB account
ID
unique name
The unique name graphdemo will be used to identify this Azure Cosmos DB account. The URI, graphdemodb.documents.azure.com is a new unique identifiable ID.
API
Gremlin (graph)
The GraphAPI, Gremlin (graph) is selected out of five APIs suite.
Subscription
Your subscription
Select Azure subscription. In this case its pay-as-you-go subscription is selected. This is type that is used for Azure Cosmos DB account.
Resource group
Enter the Resource Group
Enter the new or existing resource group. In this case, the new resource group called graphresource is created.
Location
Select the nearest location
Use the closest location that gives an optimal performance gain to access the data.
Enable geo-redundancy
Check/un-check
To create a replicated version of the database over a second (paired) region.
Pin-to-dashboard
Select
This option adds the database to the dashboard for easy access.
ID
唯一名称
唯一名称graphdemo将用于标识此Azure Cosmos DB帐户。 URI graphdemodb.documents.azure.com是一个新的唯一可识别ID。
API
格林姆林(图)
从五个API套件中选择了GraphAPI Gremlin(图形)。
订阅
您的订阅
选择Azure订阅。 在这种情况下,将选择其按需付费订阅。 该类型用于Azure Cosmos DB帐户。
资源组
输入资源组
输入新的或现有的资源组。 在这种情况下,将创建名为graphresource的新资源组。
位置
选择最近的位置
使用最能提高性能的最接近位置访问数据。
启用地理冗余
选中/取消选中
在第二个(配对)区域上创建数据库的复制版本。
针到仪表板
选择
此选项将数据库添加到仪表板以便于访问。
- Click Create 点击创建
-
The account creation step may take a few minutes
- Use the Data Explorer tool in the Azure portal to create a Graph database. Let us select the Quick start option and click the Create ‘Persons’ container
-
Follow the below steps to create and query vertices in a Graph database under the ‘Persons’ container. This can be done in two ways:
-
The first, using GUI. Click the New Vertex button; this will open up a form to enter the properties of the vertices. Once done, click Ok button to create the vertices. The interface interprets the entered values as Gremlin commands. This you can see in the message pane at the bottom
-
The second, using Gremlin command. Let’s enter the following command in the Query filter section and click Apply filter to run the Gremlin query
g.addV(‘person’).property(‘firstName’, ‘Prashanth’).property(‘lastName’, ‘Jayaram’).property(‘age’, 35).property(‘skillset’, ‘SQL’)
You can run g.V().count() command to list all the vertices
-
Next, let’s create two more vertices for the input values Brian Lockwood with SQL as his skillset and Samir Behara with. Net as his skillset
- g.addV(‘person’).property(‘firstName’, ‘Brian’).property(‘lastName’, ‘Lockwood’).property(‘age’, 50).property(‘skillset’, ‘SQL’)
- g.addV(‘person’).property(‘firstName’, ‘Samir’).property(‘lastName’, ‘Behara’).property(‘age’, 35).property(‘skillset’, ‘.Net’)
-
Let’s run a simple command to list all the vertices. Type the following g.V() and click Apply filter
-
Let’s try simple queries to list the only person using the following query. You can also view the JSON document by selecting the JSON tab
g.V().hasLabel(‘person’).has(‘firstName’, ‘Prashanth’)
-
To get the count of graphs use the simple g.V().count() command
- 首先,使用GUI。 单击“新建顶点”按钮; 这将打开一个表格以输入顶点的属性。 完成后,单击“确定”按钮以创建顶点。 界面将输入的值解释为Gremlin命令。 您可以在底部的消息窗格中看到此内容
- 第二,使用Gremlin命令。 让我们在“查询过滤器”部分中输入以下命令,然后单击“ 应用过滤器”以运行Gremlin查询
g.addV('person')。property('firstName','Prashanth')。property('lastName','Jayaram')。property('age',35).property('skillset','SQL')
您可以运行gV()。count()命令列出所有顶点
- 接下来,让我们为SQL技能和Samir Behara的输入值Brian Lockwood创建两个顶点。 网作为他的技能
- g.addV('person')。property('firstName','Brian')。property('lastName','Lockwood')。property('age',50).property('skillset','SQL')
- g.addV('person')。property('firstName','Samir')。property('lastName','Behara')。property('age',35).property('skillset','.Net' )
- 让我们运行一个简单的命令列出所有顶点。 键入以下gV()并单击“ 应用过滤器”
- 让我们尝试简单的查询,以使用以下查询列出唯一的人。 您还可以通过选择JSON标签来查看JSON文档
gV()。hasLabel('person')。has('firstName','Prashanth')
- 要获取图形计数,请使用简单的gV()。count()命令
-
The first, using GUI. Click the New Vertex button; this will open up a form to enter the properties of the vertices. Once done, click Ok button to create the vertices. The interface interprets the entered values as Gremlin commands. This you can see in the message pane at the bottom
-
Steps to create edges in a graph database. In this section outlines the steps that define the relationship between the vertices. The relationship is “knows”. Let’s define a relationship that Brain knows Prashanth and Samir using firstName properties of the person label
g.V().hasLabel(‘person’).has(‘firstName’,’Brian’).addE(‘knows’).to(g.v().hasLabel(‘person’).has(‘firstName’,’Prashanth’))
g.V().hasLabel(‘person’).has(‘firstName’,’Brian’).addE(‘knows’).to(g.v().hasLabel(‘person’).has(‘firstName’,’Samir’))
On clicking each vertex, we can see the relationship between all three
g.V().hasLabel(‘person’).has(‘firstName’, ‘Brian’)
知道”。 我们使用人员标签的firstName属性来定义Brain知道Prashanth和Samir的关系gV()。hasLabel('person')。has('firstName','Brian')。addE('knows')。to(gv()。hasLabel('person')。has('firstName','Prashanth '))
gV()。hasLabel('person')。has('firstName','Brian')。addE('knows')。to(gv()。hasLabel('person')。has('firstName','Samir '))
单击每个顶点,我们可以看到所有三个之间的关系
gV()。hasLabel('person')。has('firstName','Brian')
- Let’s see an example to update a vertex Prashanth with an age 34. This can be done in two ways:
g.V().hasLabel(‘person’).has(‘firstName’, ‘Prashanth’).property(‘age’, 34)
Let us query to find the persons who have SQL as his skillset
g.V().hasLabel(‘person’).has(‘skillset’, ‘SQL’)
SQL Query
Now, let’s try to run a query against the dataset to understand some of the key aspects of Azure Cosmos DB’s SQL query language. For example, the following query returns the documents where the id field matches “1”. The output of the query is the complete JSON document.
To run a query against a Persons collection
- Click New Query
-
Type the following SQL in the query space which is marked as 2 in the below image
SELECT { “id”:p.id, “Name”:p.name, “Age”: p.age} FROM Persons p where p.id=”1″
- To run the query, press the execute icon which is marked 3 in the below image
-
The output is a JSON document is displayed in the result pane
That’s all for now…
Summary
Thus far, we’ve covered a lot of ground in this first article. We started with NoSQL; comparison between SQL vs NoSQL. We then introduced Microsoft Azure Cosmos DB as Microsoft’s distributed NoSQL database. You also witnessed how to get started using the Azure portal to create a Cosmos DB account and integration of the Graph database API with Cosmos DB. Then we’ve created the sub-documents in the collection and then query the collection. In my next article, I will include a few complex models to give more insight into Cosmos DB integration. Thanks for reading this article. Feel free to comment below…
让我们看一个使用34岁更新顶点Prashanth的示例。这可以通过两种方式完成:gV()。hasLabel('person')。has('firstName','Prashanth')。property('age',34)
让我们查询以掌握SQL为技能的人
gV()。hasLabel('person')。has('skillset','SQL')
SQL查询
现在,让我们尝试对数据集运行查询,以了解Azure Cosmos DBSQL查询语言的一些关键方面。 例如,以下查询返回id字段与“ 1”匹配的文档。 查询的输出是完整的JSON文档。
对人员集合运行查询
- 点击新查询
- 在查询空间中键入以下SQL,在下图中将其标记为2。
SELECT {“ id”:p.id,“ Name”:p.name,“ Age”:p.age} FROM Persons其中p.id =“ 1”
- 要运行查询,请按下下图中标记为3的执行图标。
- 输出是在结果窗格中显示的JSON文档
目前为止就这样了…
摘要
到目前为止,我们在第一篇文章中已经涵盖了很多基础。 我们从NoSQL开始; SQL与NoSQL之间的比较。 然后,我们引入了Microsoft Azure Cosmos DB作为Microsoft的分布式NoSQL数据库。 您还见证了如何开始使用Azure门户创建Cosmos DB帐户以及Graph数据库API与Cosmos DB的集成。 然后,我们在集合中创建了子文档,然后查询集合。 在我的下一篇文章中,我将包括一些复杂的模型,以提供对Cosmos DB集成的更多见解。 感谢您阅读本文。 请在下面随意评论...

翻译自: https://www.sqlshack.com/graph-database-implementation-with-azure-cosmos-db-using-the-api/
azure api 管理