![](/img/trans.png)
[英]How to query for multiple vertices and counts of their relationships in Gremlin/Tinkerpop 3?
[英]How to perform cross join on different vertices in Gremlin/Tinkerpop
我是圖數據庫的新手。 這可能是一個非常基本的問題,但我們非常感謝您的幫助。 我正在使用Gremlin / Tinkerpop 3查詢存儲在TitanDB中的圖形。
以下是我的數據集。
User | associated_ids
---------------------------------
A | [ 1, 2 ]
B | [ 3, 4 ]
c | [ 5 ]
D | [ 4, 2 ]
現在,要獲取使用任何associated_id彼此鏈接的所有用戶組。 這里[A]和[D]直接鏈接,因為它們都具有公共實體[2],而[B]和[D]直接鏈接是因為它們都具有公共實體[4]。 因此,間接地將[A],[B]和[D]連接在一起。
預期結果類似。
Users | associated_ids
----------------------------
A,B,D | [ 1, 2 ,3, 4]
c | [ 5 ]
以下是我執行的生成圖的步驟。
a = graph.addVertex(label, "person", "user", "A")
b = graph.addVertex(label, "person", "user", "B")
c = graph.addVertex(label, "person", "user", "C")
d = graph.addVertex(label, "person", "user", "D")
one = graph.addVertex('rec_id')
one.property('ids', '1')
two = graph.addVertex('rec_id')
two.property('ids', '2')
three = graph.addVertex('rec_id')
three.property('ids', '3')
four = graph.addVertex('rec_id')
four.property('ids', '4')
five = graph.addVertex('rec_id')
five.property('ids', '5')
a.addEdge('part_of',one)
a.addEdge('part_of',two)
b.addEdge('part_of', three)
b.addEdge('part_of',four)
c.addEdge('part_of',five)
d.addEdge('part_of',four)
d.addEdge('part_of',two)
現在想知道如何通過任何rec_id
獲取彼此關聯的所有用戶。
為了獲得所需的輸出,Gremlin查詢將是什么?
另外,請告訴我是否需要對圖形結構進行任何更改以滿足我的要求。
您基本上在尋找的是連接組件 。
gremlin> g.V().
emit(cyclicPath().or().not(both())).
repeat(both()).
until(cyclicPath()).
aggregate("p").by(path()).cap("p").
unfold().limit(local, 1).dedup().
map(__.as("v").select("p").unfold().
filter(unfold().where(eq("v"))).
unfold().dedup().order().by(id).fold()).dedup().
project("Users","associated_ids").
by(unfold().hasLabel("person").values("user").fold()).
by(unfold().hasLabel("rec_id").values("ids").fold())
==>[Users:[A,B,D],associated_ids:[1,2,3,4]]
==>[Users:[C],associated_ids:[5]]
這是獲得所需答案的一種方法:
gremlin> g.V().has('rec_id','ids','1').
......1> in().aggregate('s').
......2> repeat(out().in().where(without('s')).aggregate('s')).
......3> cap('s').unfold().valueMap()
==>[user:[A]]
==>[user:[D]]
==>[user:[B]]
gremlin> g.V().has('rec_id','ids','5').
......1> in().aggregate('s').
......2> repeat(out().in().where(without('s')).aggregate('s')).
......3> cap('s').unfold().valueMap()
==>[user:[C]]
因此,遍歷圖形時,基本上將用戶頂點組保持為“ s”。 標記為“ 1>”的行顯示了獲得您所請求的“ rec_id”中的初始用戶的位置。 在“ 2>”行中,您遞歸地遍歷那些用戶並找到他們的“ rec_ids”,並忽略存儲在“ s”中的所有已遇到的用戶。 最后一行“ 3>”從遍歷中提取“ s”副作用,為方便起見,擴展了頂點列表並在其上運行valueMap()
,因此我們很好地看到了“ user”屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.