快速入门:在 Azure Cosmos DB 中使用 PHP 和 Azure 门户创建图形数据库Quickstart: Create a graph database in Azure Cosmos DB using PHP and the Azure portal

本快速入门介绍如何使用 PHP 和 Azure Cosmos DB Gremlin API,通过克隆 GitHub 中的示例来生成控制台应用。This quickstart shows how to use PHP and the Azure Cosmos DB Gremlin API to build a console app by cloning an example from GitHub. 此外,本快速入门还逐步讲解了如何使用基于 Web 的 Azure 门户创建 Azure Cosmos DB 帐户。This quickstart also walks you through the creation of an Azure Cosmos DB account by using the web-based Azure portal.

Azure Cosmos DB 是世纪互联提供的多区域分布式多模型数据库服务。Azure Cosmos DB is 21Vianet's multiple-regionally distributed multi-model database service. 可快速创建和查询文档、表、键-值和图形数据库,所有这些都受益于 Azure Cosmos DB 核心的多区域分布和水平缩放功能。You can quickly create and query document, table, key-value, and graph databases, all of which benefit from the multiple-region distribution and horizontal scale capabilities at the core of Azure Cosmos DB.

先决条件Prerequisites

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

此外:In addition:

创建数据库帐户Create a database account

在创建图形数据库之前,需通过 Azure Cosmos DB 创建 Gremlin (Graph) 数据库帐户。Before you can create a graph database, you need to create a Gremlin (Graph) database account with Azure Cosmos DB.

  1. 在新浏览器窗口中,登录到 Azure 门户In a new browser window, sign in to the Azure portal.

  2. 在左侧菜单中,选择“创建资源” 。In the left menu, select Create a resource.

    在 Azure 门户中创建资源

  3. 在“新建”页上,选择“数据库” > “Azure Cosmos DB”。 On the New page, select Databases > Azure Cosmos DB.

    Azure 门户“数据库”窗格

  4. 在“创建 Azure Cosmos DB 帐户”页中,输入新 Azure Cosmos DB 帐户的设置 。On the Create Azure Cosmos DB Account page, enter the settings for the new Azure Cosmos DB account.

    设置Setting Value 说明Description
    订阅Subscription 你的订阅Your subscription 选择要用于此 Azure Cosmos DB 帐户的 Azure 订阅。Select the Azure subscription that you want to use for this Azure Cosmos DB account.
    资源组Resource Group 新建Create new

    然后,输入与帐户名称相同的名称。Then enter the same name as Account Name
    选择“新建”。 Select Create new. 然后输入帐户的新资源组名称。Then enter a new resource group name for your account. 为简单起见,请使用与 Azure Cosmos DB 帐户名称相同的名称。For simplicity, use the same name as your Azure Cosmos DB account name.
    帐户名Account Name 输入唯一的名称Enter a unique name 输入标识此 Azure Cosmos DB 帐户的唯一名称。Enter a unique name to identify your Azure Cosmos DB account. 帐户 URI 将是追加到唯一帐户名称的“gremlin.cosmos.azure.cn” 。Your account URI will be gremlin.cosmos.azure.cn appended to your unique account name.

    帐户名称只能使用小写字母、数字及连字符 (-),必须为 3 到 31 个字符长。The account name can use only lowercase letters, numbers, and hyphens (-), and must be between 3 and 31 characters long.
    APIAPI Gremlin(图形)Gremlin (graph) API 确定要创建的帐户的类型。The API determines the type of account to create. Azure Cosmos DB 提供五种 API:Core(SQL)(适用于文档数据库)、Gremlin(适用于图数据库)、MongoDB(适用于文档数据库)、Azure 表和 Cassandra。Azure Cosmos DB provides five APIs: Core (SQL) for document databases, Gremlin for graph databases, MongoDB for document databases, Azure Table, and Cassandra. 必须为每种 API 创建单独的帐户。You must create a separate account for each API.

    选择“Gremlin (图)”,因为本快速入门将创建使用 Gremlin API 的表 。Select Gremlin (graph), because in this quickstart you are creating a table that works with the Gremlin API.

    详细了解 Gremlin APILearn more about the Gremlin API.
    位置Location 选择离用户最近的区域Select the region closest to your users 选择用于托管 Azure Cosmos DB 帐户的地理位置。Select a geographic location to host your Azure Cosmos DB account. 使用离用户最近的位置,使他们能够以最快的速度访问数据。Use the location that's closest to your users to give them the fastest access to the data.

    选择“查看 + 创建” 。Select Review+Create. 可以跳过“网络” 和“标记” 部分。You can skip the Network and Tags section.

    Azure Cosmos DB 的“新建帐户”页

  5. 创建帐户需要几分钟时间。The account creation takes a few minutes. 等待门户中显示“祝贺你! 已创建 Azure Cosmos DB 帐户”页。Wait for the portal to display the Congratulations! Your Azure Cosmos DB account was created page.

    “Azure Cosmos DB 帐户已创建”页

添加图形Add a graph

现在可以在 Azure 门户中使用数据资源管理器工具来创建图形数据库。You can now use the Data Explorer tool in the Azure portal to create a graph database.

  1. 依次选择“数据资源管理器” > “新建图” 。Select Data Explorer > New Graph.

    此时,最右侧显示“添加图形” 区域,可能需要向右滚动才能看到。The Add Graph area is displayed on the far right, you may need to scroll right to see it.

    Azure 门户“数据资源管理器”中的“添加图形”页

  2. 在“添加图形” 页上,输入新图形的设置。In the Add graph page, enter the settings for the new graph.

    设置Setting 建议的值Suggested value 说明Description
    数据库 IDDatabase ID sample-databasesample-database 输入“sample-database” 作为新数据库的名称。Enter sample-database as the name for the new database. 数据库名称的长度必须为 1 到 255 个字符,不能包含 / \ # ? 或尾随空格。Database names must be between 1 and 255 characters, and cannot contain / \ # ? or a trailing space.
    吞吐量Throughput 400 RU400 RUs 将吞吐量更改为每秒 400 个请求单位 (RU/s)。Change the throughput to 400 request units per second (RU/s). 如果想要减少延迟,以后可以增加吞吐量。If you want to reduce latency, you can scale up the throughput later.
    图形 IDGraph ID sample-graphsample-graph 输入“sample-graph” 作为新集合的名称。Enter sample-graph as the name for your new collection. 图形名称与数据库 ID 的字符要求相同。Graph names have the same character requirements as database IDs.
    分区键Partition Key /pk/pk 所有 Cosmos DB 帐户都需要一个分区键才能进行水平缩放。All Cosmos DB accounts need a partition key to horizontally scale. 图形数据分区一文中了解如何选择适当的分区键。Learn how to select an appropriate partition key in the Graph Data Partitioning article.
  3. 填写表单后,请选择“确定” 。Once the form is filled out, select OK.

克隆示例应用程序Clone the sample application

现在,让我们转到如何使用代码上来。Now let's switch to working with code. 从 GitHub 克隆 Gremlin API 应用,设置连接字符串,并运行应用。Let's clone a Gremlin API app from GitHub, set the connection string, and run it. 会看到以编程方式处理数据是多么容易。You'll see how easy it is to work with data programmatically.

  1. 打开命令提示符,新建一个名为“git-samples”的文件夹,然后关闭命令提示符。Open a command prompt, create a new folder named git-samples, then close the command prompt.

    md "C:\git-samples"
    
  2. 打开诸如 git bash 之类的 git 终端窗口,并使用 cd 命令更改为相应的示例应用程序安装文件夹。Open a git terminal window, such as git bash, and use the cd command to change to a folder to install the sample app.

    cd "C:\git-samples"
    
  3. 运行下列命令以克隆示例存储库。Run the following command to clone the sample repository. 此命令在计算机上创建示例应用程序的副本。This command creates a copy of the sample app on your computer.

    git clone https://github.com/Azure-Samples/azure-cosmos-db-graph-php-getting-started.git
    

查看代码Review the code

此步骤是可选的。This step is optional. 如果有意了解如何使用代码创建数据库资源,可以查看以下代码片段。If you're interested in learning how the database resources are created in the code, you can review the following snippets. 这些代码片段全部摘自 C:\git-samples\azure-cosmos-db-graph-php-getting-started\ 文件夹中的 connect.php 文件。The snippets are all taken from the connect.php file in the C:\git-samples\azure-cosmos-db-graph-php-getting-started\ folder. 否则,可以直接跳转到更新连接字符串Otherwise, you can skip ahead to Update your connection string.

  • 在使用 $db 对象的 connect.php 文件开头将 Gremlin connection 进行初始化。The Gremlin connection is initialized in the beginning of the connect.php file using the $db object.

    $db = new Connection([
        'host' => '<your_server_address>.graphs.azure.cn',
        'username' => '/dbs/<db>/colls/<coll>',
        'password' => 'your_primary_key'
        ,'port' => '443'
    
        // Required parameter
        ,'ssl' => TRUE
    ]);
    
  • 将使用 $db->send($query); 方法执行一系列 Gremlin 步骤。A series of Gremlin steps are executed using the $db->send($query); method.

    $query = "g.V().drop()";
    ...
    $result = $db->send($query);
    $errors = array_filter($result);
    }
    

更新连接信息Update your connection information

现在,返回到 Azure 门户,获取连接信息,并将信息复制到应用程序中。Now go back to the Azure portal to get your connection information and copy it into the app. 借助这些设置,应用程序可以与托管的数据库进行通信。These settings enable your app to communicate with your hosted database.

  1. Azure 门户中,单击“密钥”。In the Azure portal, click Keys.

    复制 URI 值的第一部分。Copy the first portion of the URI value.

    在 Azure 门户的“密钥”页中,查看并复制访问密钥

  2. 打开 connect.php 文件,并将 URI 值粘贴至第 8 行的 your_server_address 位置。Open the connect.php file and in line 8 paste the URI value over your_server_address.

    现在,连接对象初始化应该类似于下面的代码:The connection object initialization should now look similar to the following code:

    $db = new Connection([
        'host' => 'testgraphacct.gremlin.cosmos.azure.cn',
        'username' => '/dbs/<db>/colls/<coll>',
        'password' => 'your_primary_key'
        ,'port' => '443'
    
        // Required parameter
        ,'ssl' => TRUE
    ]);
    
  3. 将连接对象中的 username 参数更改为你的数据库和图形名称。Change username parameter in the Connection object with your database and graph name. 如果使用了建议值 sample-databasesample-graph,则它应如以下代码所示:If you used the recommended values of sample-database and sample-graph, it should look like the following code:

    'username' => '/dbs/sample-database/colls/sample-graph'

    此时,整个连接对象应类似于下面的代码片段:The entire Connection object should look like the following code snippet at this time:

    $db = new Connection([
        'host' => 'testgraphacct.gremlin.cosmos.azure.cn',
        'username' => '/dbs/sample-database/colls/sample-graph',
        'password' => 'your_primary_key',
        'port' => '443'
    
        // Required parameter
        ,'ssl' => TRUE
    ]);
    
  4. 在 Azure 门户中,使用“复制”按钮复制主密钥,并将它粘贴到密码参数中的 your_primary_key 位置。In the Azure portal, use the copy button to copy the PRIMARY KEY and paste it over your_primary_key in the password parameter.

    现在,连接对象初始化应该类似于下面的代码:The Connection object initialization should now look like the following code:

    $db = new Connection([
        'host' => 'testgraphacct.graphs.azure.cn',
        'username' => '/dbs/sample-database/colls/sample-graph',
        'password' => '2Ggkr662ifxz2Mg==',
        'port' => '443'
    
        // Required parameter
        ,'ssl' => TRUE
    ]);
    
  5. 保存 connect.php 文件。Save the connect.php file.

运行控制台应用Run the console app

  1. 在 git 终端窗口中,运行 cd 命令以转到 azure-cosmos-db-graph-php-getting-started 文件夹。In the git terminal window, cd to the azure-cosmos-db-graph-php-getting-started folder.

    cd "C:\git-samples\azure-cosmos-db-graph-php-getting-started"
    
  2. 在 git 终端窗口中,使用以下命令安装所需的 PHP 依赖项。In the git terminal window, use the following command to install the required PHP dependencies.

    composer install
    
  3. 在 git 终端窗口中,使用以下命令启动 PHP 应用程序。In the git terminal window, use the following command to start the PHP application.

    php connect.php
    

    终端窗口会显示添加到图形的顶点。The terminal window displays the vertices being added to the graph.

    如果遇到超时错误,请在更新你的连接信息中检查是否正确更新了连接信息,并尝试再次运行上一个命令。If you experience timeout errors, check that you updated the connection information correctly in Update your connection information, and also try running the last command again.

    一旦程序停止运行,按 Enter,然后在 Internet 浏览器中立即切换回 Azure 门户。Once the program stops, press Enter, then switch back to the Azure portal in your internet browser.

查看并添加示例数据Review and add sample data

现在可以回到数据资源管理器,查看添加到图形的顶点,并添加其他数据点。You can now go back to Data Explorer and see the vertices added to the graph, and add additional data points.

  1. 单击“数据资源管理器”,展开“sample-graph”,再依次单击“图形”和“应用筛选器”。Click Data Explorer, expand sample-graph, click Graph, and then click Apply Filter.

    在 Azure 门户的数据资源管理器中创建新文档

  2. 在“结果”列表中,请注意添加到图形的新用户。In the Results list, notice the new users added to the graph. 选择“ben”。请注意,他们已连接到 robin。Select ben and notice that they're connected to robin. 可以通过拖放操作来移动顶点,也可以通过滚动鼠标滚轮进行缩放,并能用双箭头放大图形。You can move the vertices around by dragging and dropping, zoom in and out by scrolling the wheel of your mouse, and expand the size of the graph with the double-arrow.

    在 Azure 门户数据资源管理器的图形中的新顶点

  3. 接下来,添加几个新用户。Let's add a few new users. 单击“新建顶点”按钮,向图形添加数据。Click the New Vertex button to add data to your graph.

    在 Azure 门户的数据资源管理器中创建新文档

  4. 输入标签“人员”。Enter a label of person.

  5. 单击“添加属性”,添加下列所有属性。Click Add property to add each of the following properties. 注意,可以在图形中为每个人创建唯一属性。Notice that you can create unique properties for each person in your graph. id 键是必需的。Only the id key is required.

    密钥Key Value 说明Notes
    idid ashleyashley 顶点的唯一标识符。The unique identifier for the vertex. 如果未指定 id,将为你生成一个。If you don't specify an id, one is generated for you.
    性别gender female
    技术tech javajava

    备注

    在本快速入门中,将创建未分区的集合。In this quickstart you create a non-partitioned collection. 但是,如果在创建集合过程中通过指定分区键创建了分区的集合,则需在每个新顶点中包括该分区键作为键。However, if you create a partitioned collection by specifying a partition key during the collection creation, then you need to include the partition key as a key in each new vertex.

  6. 单击“确定”。Click OK. 可能需要展开屏幕才能在屏幕底部看到“确定”。You may need to expand your screen to see OK on the bottom of the screen.

  7. 再次单击“新建顶点”,添加其他新用户。Click New Vertex again and add an additional new user.

  8. 输入标签“人员”。Enter a label of person.

  9. 单击“添加属性”,添加下列所有属性:Click Add property to add each of the following properties:

    密钥Key Value 说明Notes
    idid rakeshrakesh 顶点的唯一标识符。The unique identifier for the vertex. 如果未指定 id,将为你生成一个。If you don't specify an id, one is generated for you.
    性别gender male
    学校school MITMIT
  10. 单击“确定”。Click OK.

  11. 单击“应用筛选器”按钮(默认 g.V() 筛选器),显示图形中的所有值。Click the Apply Filter button with the default g.V() filter to display all the values in the graph. 所有用户此时会显示在“结果”列表中。All of the users now show in the Results list.

    添加更多数据时,可以使用筛选器来限制结果。As you add more data, you can use filters to limit your results. 默认情况下,数据资源管理器使用 g.V() 检索图形中的所有顶点。By default, Data Explorer uses g.V() to retrieve all vertices in a graph. 可以更改为其他图形查询(如 g.V().count()),以 JSON 格式返回图形中所有 顶点的计数。You can change it to a different graph query, such as g.V().count(), to return a count of all the vertices in the graph in JSON format. 如果更改了筛选器,请将筛选器更改回 g.V(),并单击“应用筛选器”,再次显示所有结果。If you changed the filter, change the filter back to g.V() and click Apply Filter to display all the results again.

  12. 现在可以连接 rakesh 与 ashley。Now you can connect rakesh and ashley. 确保“ashley”在“结果”列表中为选中状态,然后单击右下侧“目标”旁边的编辑按钮。Ensure ashley is selected in the Results list, then click the edit button next to Targets on lower right side. 可能需要扩大窗口才能看到“属性”区域。You may need to widen your window to see the Properties area.

    更改图形中某个顶点的目标

  13. 在“目标”框中键入“rakesh”,在“Edge 标签”框中键入“认识”,再选中复选框。In the Target box type rakesh, and in the Edge label box type knows, and then click the check.

    通过数据资源管理器在 ashley 和 rakesh 之间添加连接

  14. 现在,从结果列表中选择“rakesh”即可看到 ashley 和 rakesh 已连接。Now select rakesh from the results list and see that ashley and rakesh are connected.

    在数据资源管理器中连接的两个顶点

    这就完成了本快速入门的资源创建部分。That completes the resource creation part of this quickstart. 可以继续向图形添加顶点、修改现有顶点,也可以更改查询。You can continue to add vertexes to your graph, modify the existing vertexes, or change the queries. 现在,回顾一下 Azure Cosmos DB 提供的指标,然后清理资源。Now let's review the metrics Azure Cosmos DB provides, and then clean up the resources.

在 Azure 门户中查看 SLAReview SLAs in the Azure portal

Azure 门户监视 Cosmos DB 帐户吞吐量、存储、可用性、延迟和一致性。The Azure portal monitors your Cosmos DB account throughput, storage, availability, latency, and consistency. Azure Cosmos DB 服务级别协议 (SLA) 关联的指标的图表显示与实际性能相比的 SLA 值。Charts for metrics associated with an Azure Cosmos DB Service Level Agreement (SLA) show the SLA value compared to actual performance. 此套指标使得监视 SLA 十分透明。This suite of metrics makes monitoring your SLAs transparent.

若要查看指标和 SLA,请执行以下操作:To review metrics and SLAs:

  1. 在 Cosmos DB 帐户的导航菜单中选择“指标” 。Select Metrics in your Cosmos DB account's navigation menu.

  2. 选择一个选项卡,如“延迟” ,然后选择右侧的时间范围。Select a tab such as Latency, and select a timeframe on the right. 比较图表上的“实际” 和“SLA” 线。Compare the Actual and SLA lines on the charts.

    Azure Cosmos DB 指标套件

  3. 查看其他选项卡上的指标。Review the metrics on the other tabs.

清理资源Clean up resources

执行完应用和 Azure Cosmos DB 帐户的操作以后,可以删除所创建的 Azure 资源,以免产生更多费用。When you're done with your app and Azure Cosmos DB account, you can delete the Azure resources you created so you don't incur more charges. 若要删除资源,请执行以下操作:To delete the resources:

  1. 在 Azure 门户的“搜索”栏中,搜索并选择“资源组” 。In the Azure portal Search bar, search for and select Resource groups.

  2. 从列表中选择为本快速入门创建的资源组。From the list, select the resource group you created for this quickstart.

    选择要删除的资源组

  3. 在资源组“概览”页上,选择“删除资源组” 。On the resource group Overview page, select Delete resource group.

    删除资源组

  4. 在下一窗口中输入要删除的资源组的名称,然后选择“删除” 。In the next window, enter the name of the resource group to delete, and then select Delete.

后续步骤Next steps

在本快速入门教程中,已了解如何创建 Azure Cosmos DB 帐户、使用数据资源管理器创建图形和运行应用。In this quickstart, you've learned how to create an Azure Cosmos DB account, create a graph using the Data Explorer, and run an app. 现可使用 Gremlin 构建更复杂的查询,实现功能强大的图形遍历逻辑。You can now build more complex queries and implement powerful graph traversal logic using Gremlin.