教學課程:使用 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 步驟,然後使用 andornot 來結合它們以建置更複雜的篩選。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:

  • 了解如何使用「圖形」來進行查詢Learned how to query using Graph

您現在可以繼續進行<概念>一節了解有關 Cosmos DB 的詳細資訊。You can now proceed to the Concepts section for more information about Cosmos DB.