快速入门:使用 Java SDK 和 Azure Cosmos DB Gremlin API 构建图形数据库Quickstart: Build a graph database with the Java SDK and the Azure Cosmos DB Gremlin API

适用于: Gremlin API

在本快速入门中,你将通过 Azure 门户创建和管理 Azure Cosmos DB Gremlin(图形)API 帐户,并使用从 GitHub 克隆的 Java 应用来添加数据。In this quickstart, you create and manage an Azure Cosmos DB Gremlin (graph) API account from the Azure portal, and add data by using a Java app cloned from GitHub. Azure Cosmos DB 是一种多模型数据库服务,可让你通过多区域分布和水平缩放功能快速创建和查询文档、表、键/值和图数据库。Azure Cosmos DB is a multi-model database service that lets you quickly create and query document, table, key-value, and graph databases with multiple-region distribution and horizontal scale capabilities.


创建数据库帐户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 门户“数据库”窗格


    如果在列表中没有看到“Azure Cosmos DB”,只需在页面顶部的搜索框中键入它,然后按 Enter 键即可。If you don't see Azure Cosmos DB in the list, just type it in the search box at the top of the page, and press Enter key.

  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.
    容量模式Capacity mode 预配吞吐量或无服务器Provisioned throughput or Serverless 选择“预配吞吐量”以在预配吞吐量模式下创建帐户。Select Provisioned throughput to create an account in provisioned throughput mode. 选择“无服务器”以在无服务器模式下创建帐户。Select Serverless to create an account in serverless mode.

    选择“查看 + 创建” 。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-java-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. 否则,可以直接跳转到更新连接字符串Otherwise, you can skip ahead to Update your connection string.

以代码片段全部摘自 C:\git-samples\azure-cosmos-db-graph-java-getting-started\src\GetStarted\Program.java 文件。The following snippets are all taken from the C:\git-samples\azure-cosmos-db-graph-java-getting-started\src\GetStarted\Program.java file.

此 Java 控制台应用使用 Gremlin API 数据库和 OSS Apache TinkerPop 驱动程序。This Java console app uses a Gremlin API database with the OSS Apache TinkerPop driver.

  • Gremlin Client 是通过 C:\git-samples\azure-cosmos-db-graph-java-getting-started\src\remote.yaml 文件中的配置初始化的。The Gremlin Client is initialized from the configuration in the C:\git-samples\azure-cosmos-db-graph-java-getting-started\src\remote.yaml file.

    cluster = Cluster.build(new File("src/remote.yaml")).create();
    client = cluster.connect();
  • 将使用 client.submit 方法执行一系列 Gremlin 步骤。Series of Gremlin steps are executed using the client.submit method.

    ResultSet results = client.submit(gremlin);
    CompletableFuture<List<Result>> completableFutureResults = results.all();
    List<Result> resultList = completableFutureResults.get();
    for (Result result : resultList) {

更新连接信息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 门户中,在你的 Azure Cosmos DB 帐户中,选择“密钥”。In your Azure Cosmos DB account in the Azure portal, select Keys.

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

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

  2. 打开 src/remote.yaml 文件,粘贴唯一 ID 值并用其覆盖 hosts: [$name$.graphs.azure.cn] 中的 $name$Open the src/remote.yaml file and paste the unique ID value over $name$ in hosts: [$name$.graphs.azure.cn].

    remote.yaml 的第 1 行现应如下所示Line 1 of remote.yaml should now look similar to

    hosts: [test-graph.graphs.azure.cn]

  3. endpoint 值中的 graphs 更改为 gremlin.cosmosdbChange graphs to gremlin.cosmosdb in the endpoint value. (如果是在 2017 年 12 月 20 日之前创建的图形数据库帐户,请勿更改终结点值,继续执行下一步即可。)(If you created your graph database account before December 20, 2017, make no changes to the endpoint value and continue to the next step.)

    终结点值现在应如下所示:The endpoint value should now look like this:

    "endpoint": "https://testgraphacct.gremlin.cosmos.azure.cn:443/"

  4. 在 Azure 门户中,使用“复制”按钮复制主密钥,并将它粘贴到 password: $masterKey$ 中的 $masterKey$In the Azure portal, use the copy button to copy the PRIMARY KEY and paste it over $masterKey$ in password: $masterKey$.

    remote.yaml 的第 4 行现应如下所示Line 4 of remote.yaml should now look similar to

    password: 2Ggkr662ifxz2Mg==

  5. remote.yaml 的第 3 行从Change line 3 of remote.yaml from

    username: /dbs/$database$/colls/$collection$


    username: /dbs/sample-database/colls/sample-graph

    如果为示例数据库或图形使用了唯一名称,请相应地更新这些值。If you used a unique name for your sample database or graph, update the values as appropriate.

  6. 保存 remote.yaml 文件。Save the remote.yaml file.

运行控制台应用Run the console app

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

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

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

    mvn exec:java -D exec.mainClass=GetStarted.Program

    终端窗口会显示添加到图形的顶点。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, select 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. 在 Azure 门户中,在你的 Azure Cosmos DB 帐户中,选择“数据资源管理器”,展开“sample-graph”,再依次选择“图形”和“应用筛选器”。In your Azure Cosmos DB account in the Azure portal, select Data Explorer, expand sample-graph, select Graph, and then select Apply Filter.

    屏幕截图显示了从 API 中选择了提供“应用筛选器”选项的“图形”。

  2. 在“结果”列表中,请注意添加到图形的新用户。In the Results list, notice the new users added to the graph. 选择“ben”,请注意,该用户已连接到 robin。Select ben and notice that the user is 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. 选择“新建顶点”,向图添加数据。Select New Vertex to add data to your graph.


  4. 在标签框中,输入 personIn the label box, enter person.

  5. 选择“添加属性”,以添加下列每个属性。Select 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.
    gendergender femalefemale
    techtech 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. 选择“确定” 。Select OK. 可能需要展开屏幕才能在屏幕底部看到“确定”。You may need to expand your screen to see OK on the bottom of the screen.

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

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

  9. 选择“添加属性”,以添加下列每个属性:Select 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.
    gendergender malemale
    schoolschool MITMIT
  10. 选择“确定” 。Select OK.

  11. 选择“应用筛选器”按钮(使用默认 g.V() 筛选器),显示图中的所有值。Select 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 select Apply Filter to display all the results again.

  12. 现在可以连接 rakesh 与 ashley。Now you can connect rakesh, and ashley. 确保“ashley”在“结果”列表中为选中状态,然后选择右下侧“目标”旁边的 “更改图中某个顶点的目标”。 可能需要扩大窗口才能看到该按钮。You may need to widen your window to see the button.

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

  13. 在“目标”框中输入“rakesh”,在“Edge 标签”框中输入“认识”,然后选中复选框。In the Target box enter rakesh, and in the Edge label box enter knows, and then select the check box.

    在数据资源管理器中添加连接 - Azure CosmosDB

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

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

这就完成了本教程的资源创建部分。That completes the resource creation part of this tutorial. 可以继续向图形添加顶点、修改现有顶点,也可以更改查询。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 帐户,使用数据资源管理器创建图形,以及运行 Java 应用来向图形添加数据。In this quickstart, you learned how to create an Azure Cosmos DB account, create a graph using the Data Explorer, and run a Java app that adds data to the graph. 现可使用 Gremlin 构建更复杂的查询,实现功能强大的图形遍历逻辑。You can now build more complex queries and implement powerful graph traversal logic using Gremlin.