教程:使用 Gremlin 查询 Azure Cosmos DB Gremlin APITutorial: Query Azure Cosmos DB Gremlin API by using Gremlin

Azure Cosmos DB Gremlin API 支持 Gremlin 查询。The Azure Cosmos DB Gremlin API supports Gremlin queries. 本文包含示例文档和查询,提供入门指导。This article provides sample documents and queries to get you started. Gremlin 支持一文提供了详细的 Gremlin 引用。A detailed Gremlin reference is provided in the Gremlin support article.

本文涵盖以下任务:This article covers the following tasks:

  • 使用 Gremlin 查询数据Querying data with Gremlin

必备条件Prerequisites

若要使这些查询生效,必须拥有 Azure Cosmos DB 帐户,且容器中必须包含图数据。For these queries to work, you must have an Azure Cosmos DB account and have graph data in the container. 没有这些内容?Don't have any of those? 请学习 5 分钟快速入门开发人员教程,创建 帐户并填充数据库。Complete the 5-minute quickstart or the developer tutorial to create an account and populate your database. 可以使用 Gremlin 控制台或你最喜爱的 Gremlin 驱动程序运行以下查询。You can run the following queries using the Gremlin console, or your favorite Gremlin driver.

计算图中的顶点数量Count vertices in the graph

以下代码段演示如何对图中的顶点进行计数:The following snippet shows how to count the number of vertices in the graph:

g.V().count()

筛选器Filters

可通过 Gremlin 的 hashasLabel 步骤执行筛选,并使用 and``ornot 将其组合,创建更复杂的筛选器。You can perform filters using Gremlin's has and hasLabel steps, and combine them using and, or, and not to build more complex filters. Azure Cosmos DB 提供顶点和度数内所有属性的架构无关索引,以进行快速查询:Azure Cosmos DB provides schema-agnostic indexing of all properties within your vertices and degrees for fast queries:

g.V().hasLabel('person').has('age', gt(40))

投影Projection

可使用 values 步骤对查询结果中的某些属性进行投影:You can project certain properties in the query results using the values step:

g.V().hasLabel('person').values('firstName')

目前为止,我们仅介绍了适用于任何数据库的查询运算符。So far, we've only seen query operators that work in any database. 如需导航到相关边缘和顶点,可以使用图快速高效地进行遍历操作。Graphs are fast and efficient for traversal operations when you need to navigate to related edges and vertices. 查找 Thomas 的所有朋友。Let's find all friends of Thomas. 为此,可使用 Gremlin 的 outE 步骤从 Thomas 中查找所有外缘,并使用 Gremlin 的 inV 步骤从这些边缘遍历至内顶点:We do this by using Gremlin's outE step to find all the out-edges from Thomas, then traversing to the in-vertices from those edges using Gremlin's inV step:

g.V('thomas').outE('knows').inV().hasLabel('person')

下一查询通过调用 outEinV 两次,执行两个跃点查找 Thomas 所有的“朋友的朋友”。The next query performs two hops to find all of Thomas' "friends of friends", by calling outE and inV two times.

g.V('thomas').outE('knows').inV().hasLabel('person').outE('knows').inV().hasLabel('person')

可使用 Gremlin 构建更加复杂的查询和实现功能强大图遍历逻辑,包括混合筛选表达式、使用 loop 步骤执行循环以及使用 choose 步骤实现条件导航。You can build more complex queries and implement powerful graph traversal logic using Gremlin, including mixing filter expressions, performing looping using the loop step, and implementing conditional navigation using the choose step. 深入了解通过 Gremlin 支持可实现的操作!Learn more about what you can do with Gremlin support!

后续步骤Next steps

在本教程中,已完成以下内容:In this tutorial, you've done the following:

  • 已了解如何使用 Graph 进行查询Learned how to query using Graph

现在可以转到“概念”部分详细了解 Cosmos DB。You can now proceed to the Concepts section for more information about Cosmos DB.