首页 > 解决方案 > Gremlin connected components prints one by one

问题描述

I'm newbie to the gremlin QL, My requirement to generate the connected components on huge graph. I tried the below query but it's printing as a group of values but I need to print one by one.

Connected components Query:

g.V().emit(cyclicPath().or().not(both())).repeat(both()).until(cyclicPath()).path().aggregate("p").unfold().dedup().map(__.as("v").select("p").unfold().filter(unfold().where(eq("v"))).unfold().dedup().order().by(id).fold()).dedup()
[v[89826185]]
[v[89826188], v[89826189], v[89826190], v[89826191], v[89826192], v[89826193], v[89826194]]
[v[89826195], v[89826196], v[89826198]]

I need to print the values like below way. min-id of group(list) to each element of the group(list).

Ex:

89826188 89826189
89826188 89826190
89826188 89826191
89826188 89826192
89826188 89826193
89826188 89826194
89826188 89826188 (self)

标签: gremlinconnected-components

解决方案


您可以在应用程序的代码中执行此操作。在查询级别执行此操作只会放大结果大小,但您可以这样做:

g.V().
  emit(cyclicPath().or().not(both())).
    repeat(both()).
    until(cyclicPath()).
  path().aggregate("p").
  unfold().dedup().
  map(__.as("v").select("p").unfold().
      filter(unfold().where(eq("v"))).
      unfold().dedup().
      order().
        by(id).
      fold()).
  dedup().as("list").
  unfold().
  map(union(select("list").
              by(limit(local, 1)),
            identity()).
      id().fold())

它基本上是相同的查询,我只添加了最后map()一步来重新格式化结果。


推荐阅读