2017-02-17 71 views
3

我想使用單個gremlin查詢來確定滿足某個謂詞的頂點的百分比,但我在存儲和傳播計算值時遇到了問題。Gremlin-如何計算具有某個屬性的頂點的百分比

假設我想計算帶有標籤「A」的所有頂點的百分比,該頂點的出邊帶有標籤「B」。我可以打印出標籤爲「A」的頂點的號碼,以及與標籤「B」出局邊緣的頂點在同一個查詢號碼:

g.V().limit(1).project("total","withEdgeB") 
.by(g.V().hasLabel("A").count()) 
.by(g.V().hasLabel("A").match(__.as("a").outE("B").inV()).dedup().count()) 

這給了我兩個相關值: totalwithEdgeB。如何傳播和計算這些值?

理想情況下,我想是這樣的:

g.V().limit(1).project("total","withEdgeB","percentage") 
.by(g.V().hasLabel("A").count().as("totalA")) 
.by(g.V().hasLabel("A").match(__.as("a").outE("B").inV()).dedup().count().as("totalWithEdgeB")) 
.by(totalWithEdgeB/totalA) 

所以我的問題是,我怎麼能訪問在第三by()聲明價值totalAtotalWithEdgeB?或者,我是否全力以赴?

回答

1

我會用一些簡單的計算。使用現代圖形,找到所有person頂點與傳出created邊緣:

gremlin> g.V().hasLabel('person'). 
      choose(outE('created'), 
        constant(1), 
        constant(0)).fold(). 
      project('total','withCreated','percentage'). 
      by(count(local)). 
      by(sum(local)). 
      by(mean(local)) 
==>[total:4,withCreated:3,percentage:0.75]