在 Azure Cosmos DB for Apache Gremlin 中執行圖形資料的查詢

Important

您是否正在尋找一種適用於高擴展性場景的資料庫解決方案,且具有 99.999% 可用性的服務等級協定(SLA)、即時自動擴展,以及跨多個區域的自動容錯切換? 考慮使用Azure Cosmos DB作為NoSQL的選擇

您是否想要實作線上分析處理 (OLAP) 圖表或移轉現有的 Apache Gremlin 應用程式? 考慮Microsoft Fabric中的圖表。

Azure Cosmos DB for Apache Gremlin 支援查詢的 Gremlin TinkerPop 語法。 本指南會逐步解說可以使用此服務執行的常見查詢。 您可以使用 Gremlin 主控台或您最愛的 Gremlin 驅動程式執行本指南中的下列查詢。

Prerequisites

  • Azure 訂用帳戶

    • 如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶
  • Azure Cosmos DB for Apache Gremlin 帳戶
  • 範例資料的存取權以進行測試

計算圖形中的頂點數

計算圖表中產品頂點的總數。 此作業有助於了解產品目錄的大小或驗證資料載入數量。

g.V().hasLabel('product').count()

計算圖形中具有特定標籤的頂點數目

計算圖表中包含特定標籤的產品頂點總數。 在此範例中,標籤為 product

g.V().hasLabel('product').count()

依標籤和屬性篩選產品

擷取符合特定標籤和屬性值的產品。 此查詢有助於將結果縮小到感興趣的子集,例如價格大於 800 美元的產品。

g.V().hasLabel('product').has('price', gt(800))

從產品投射特定屬性

僅傳回相符產品中選取的屬性。 此查詢減少了傳回的資料量,並專注於相關欄位,例如產品名稱。

g.V().hasLabel('product').values('name')

透過周遊圖表來尋找相關產品。 例如,透過周遊傳出的「取代」邊緣,然後周遊至連接的產品頂點,以尋找被特定產品取代的所有產品。

g.V(['gear-surf-surfboards', 'bbbbbbbb-1111-2222-3333-cccccccccccc']).outE('replaces').inV().hasLabel('product')

使用此查詢來尋找取代鏈結中兩個躍點外的產品:

g.V(['gear-surf-surfboards', 'bbbbbbbb-1111-2222-3333-cccccccccccc']).outE('replaces').inV().hasLabel('product').outE('replaces').inV().hasLabel('product')

使用執行設定檔分析查詢執行

使用 executionProfile() 步驟分析 Gremlin 查詢的效能和執行詳細資料。 此步驟會傳回一個 JSON 物件,其中包含查詢中每個步驟的計量,這有助於進行疑難排解和最佳化。

g.V(['gear-surf-surfboards', 'bbbbbbbb-1111-2222-3333-cccccccccccc']).out().executionProfile()
[
  {
    "gremlin": "g.V('mary').out().executionProfile()",
    "totalTime": 28,
    "metrics": [
      {
        "name": "GetVertices",
        "time": 24,
        "annotations": { "percentTime": 85.71 },
        "counts": { "resultCount": 2 },
        "storeOps": [ { "fanoutFactor": 1, "count": 2, "size": 696, "time": 0.4 } ]
      },
      {
        "name": "GetEdges",
        "time": 4,
        "annotations": { "percentTime": 14.29 },
        "counts": { "resultCount": 1 },
        "storeOps": [ { "fanoutFactor": 1, "count": 1, "size": 419, "time": 0.67 } ]
      },
      {
        "name": "GetNeighborVertices",
        "time": 0,
        "annotations": { "percentTime": 0 },
        "counts": { "resultCount": 1 }
      },
      {
        "name": "ProjectOperator",
        "time": 0,
        "annotations": { "percentTime": 0 },
        "counts": { "resultCount": 1 }
      }
    ]
  }
]

如需 executionProfile() 步驟的詳細資訊,請參閱執行設定檔參考

小提示

executionProfile() 步驟會執行 Gremlin 查詢。 此查詢包含 addVaddE 步驟,這會導致建立和認可查詢中指定的變更。 Gremlin 查詢產生的要求單位也會收費。

識別盲目展開查詢模式

當查詢存取超過必要的分割區時,就會發生盲目展開,通常是因為缺少分割區索引鍵述詞。 這種反模式可能會增加延遲和成本。 執行設定檔會透過顯示高 fanoutFactor 來協助識別此類模式。

g.V(['gear-surf-surfboards', 'aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb']).executionProfile()
[
  {
    "gremlin": "g.V('tt0093640').executionProfile()",
    "totalTime": 46,
    "metrics": [
      {
        "name": "GetVertices",
        "time": 46,
        "annotations": { "percentTime": 100 },
        "counts": { "resultCount": 1 },
        "storeOps": [ { "fanoutFactor": 5, "count": 1, "size": 589, "time": 75.61 } ]
      },
      {
        "name": "ProjectOperator",
        "time": 0,
        "annotations": { "percentTime": 0 },
        "counts": { "resultCount": 1 }
      }
    ]
  }
]

最佳化展開查詢

fanoutFactor (例如 5) 表示查詢已存取多個分割區。 若要最佳化,請在查詢述詞中包含分割區索引鍵:

g.V(['gear-surf-surfboards', 'aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb'])

未篩選的查詢模式

未經篩選的查詢可能會處理大型初始資料集,從而增加成本和延遲。

g.V().hasLabel('product').out().executionProfile()
[
  {
    "gremlin": "g.V().hasLabel('tweet').out().executionProfile()",
    "totalTime": 42,
    "metrics": [
      {
        "name": "GetVertices",
        "time": 31,
        "annotations": { "percentTime": 73.81 },
        "counts": { "resultCount": 30 },
        "storeOps": [ { "fanoutFactor": 1, "count": 13, "size": 6819, "time": 1.02 } ]
      },
      {
        "name": "GetEdges",
        "time": 6,
        "annotations": { "percentTime": 14.29 },
        "counts": { "resultCount": 18 },
        "storeOps": [ { "fanoutFactor": 1, "count": 20, "size": 7950, "time": 1.98 } ]
      },
      {
        "name": "GetNeighborVertices",
        "time": 5,
        "annotations": { "percentTime": 11.9 },
        "counts": { "resultCount": 20 },
        "storeOps": [ { "fanoutFactor": 1, "count": 4, "size": 1070, "time": 1.19 } ]
      },
      {
        "name": "ProjectOperator",
        "time": 0,
        "annotations": { "percentTime": 0 },
        "counts": { "resultCount": 20 }
      }
    ]
  }
]

篩選的查詢模式

在周遊之前新增篩選條件可以減少工作集並改善效能。 執行設定檔會顯示篩選的效果。 篩選的查詢會處理較少的頂點,進而降低延遲和成本。

g.V().hasLabel('product').has('clearance', true).out().executionProfile()
[
  {
    "gremlin": "g.V().hasLabel('tweet').has('lang', 'en').out().executionProfile()",
    "totalTime": 14,
    "metrics": [
      {
        "name": "GetVertices",
        "time": 14,
        "annotations": { "percentTime": 58.33 },
        "counts": { "resultCount": 11 },
        "storeOps": [ { "fanoutFactor": 1, "count": 11, "size": 4807, "time": 1.27 } ]
      },
      {
        "name": "GetEdges",
        "time": 5,
        "annotations": { "percentTime": 20.83 },
        "counts": { "resultCount": 18 },
        "storeOps": [ { "fanoutFactor": 1, "count": 18, "size": 7159, "time": 1.7 } ]
      },
      {
        "name": "GetNeighborVertices",
        "time": 5,
        "annotations": { "percentTime": 20.83 },
        "counts": { "resultCount": 18 },
        "storeOps": [ { "fanoutFactor": 1, "count": 4, "size": 1070, "time": 1.01 } ]
      },
      {
        "name": "ProjectOperator",
        "time": 0,
        "annotations": { "percentTime": 0 },
        "counts": { "resultCount": 18 }
      }
    ]
  }
]