在gremlin查询中显示子级别 [英] Displaying sub-levels in gremlin query

查看:105
本文介绍了在gremlin查询中显示子级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下图所示的图形.数字表示作为gremlin查询的输出所需的节点的级别格式以及节点的属性. 图的结构可以更改,因为可以将更多的节点添加到图中.对于其他节点,必须以类似的格式返回该级别. 例如,1.1.1的子级应将级别返回为1.1.1.1,1.1.1.2 ... 以下查询,但级别为连续格式1,2,3 ...

I have the graph as shown in the figure below. The numbers represent the level format of the node which is required as output from the gremlin query along with the properties of the nodes. The graph structure can change that is more nodes can be added to the graph. The level must be returned in a similar format for additional nodes. For example, children of 1.1.1 should return the level as 1.1.1.1,1.1.1.2 ... The following query but the level is in continuous format 1,2,3 ...

g.withSack(0).
  V().
  hasLabel('A').
  has('label_A','A').
  emit().
  repeat(sack(sum).by(constant(1)).out()).
    project('depth', 'properties').
      by(sack()).
      by(valueMap())

A正在启动根节点.

A is starting root node.

我知道它太复杂了.如果不可能的话,我们至少可以使用多个麻袋变量获得子深度和深度.以下是示例:

I know its too complicated. If not possible can we at least get the sub depth along with depth using multiple sack variables. Following is the example:

深度:0子深度:0 深度:1次深度:1.1 深度:1次深度:1.2 深度:2次深度:2.1 深度:2次深度:2.2 深度:2次深度:2.3 深度:2次深度:2.4

depth:0 sub-depth:0 depth:1 sub-depth:1.1 depth:1 sub-depth:1.2 depth:2 sub-depth:2.1 depth:2 sub-depth:2.2 depth:2 sub-depth:2.3 depth:2 sub-depth:2.4

推荐答案

执行所需操作的一种简单方法是利用index步骤.如果我们创建一个简单的二叉树,如下所示:

A simple way to do what you are looking for is to take advantage of the index step. If we create a simple binary tree as follows:

g.addV('root').property('data',9).as('root').   
  addV('node').property('data',5).as('b').   
  addV('node').property('data',2).as('c').   
  addV('node').property('data',11).as('d').   
  addV('node').property('data',15).as('e').   
  addV('node').property('data',10).as('f').   
  addV('node').property('data',1).as('g').   
  addV('node').property('data',8).as('h').   
  addV('node').property('data',22).as('i').   
  addV('node').property('data',16).as('j').   
  addE('left').from('root').to('b').
  addE('left').from('b').to('c').
  addE('right').from('root').to('d').
  addE('right').from('d').to('e').
  addE('right').from('e').to('i').
  addE('left').from('i').to('j').
  addE('left').from('d').to('f').
  addE('right').from('b').to('h').
  addE('left').from('c').to('g').iterate()    

我们可以如下组合loopsindex(我添加了unfold以提高可读性):

We can combine loops and index as follows (I added the unfold to improve readability):

gremlin> g.V().hasLabel('root').
......1>       emit().
......2>       repeat(group('x').by(loops()).by(values('data').fold().index()).out()).
......3>       cap('x').unfold()   

==>0=[[9, 0]]
==>1=[[5, 0], [11, 1]]
==>2=[[2, 0], [8, 1], [10, 2], [15, 3]]
==>3=[[1, 0], [22, 1]]
==>4=[[16, 0]]

考虑到您对可接受的更简单形式的评论,我认为以上内容已经很接近了.您应该能够对该查询进行调整,以对所需的输出格式进行任何更改.

Given your comment about a simpler form being acceptable I think the above gets pretty close. You should be able to tweak this query to make any changes in the output formatting that you require.

您可以更进一步,并按照以下步骤使用父顶点进行分组.由此,您可以构建所需的最终结果的任何预测.

You can go one step further and group using the parent vertex as follows. From this you can build whatever projections of the final results you require.

gremlin> g.V().hasLabel('root').
......1>       repeat(outE().group('x').
......2>         by(loops()).
......3>         by(group().
......4>           by(outV()).
......5>           by(inV().values('data').fold().index())).
......6>         inV()).
......7>         times(4).
......8>       cap('x').
......9>       unfold() 

==>0={v[0]=[[5, 0], [11, 1]]}
==>1={v[2]=[[2, 0], [8, 1]], v[6]=[[10, 0], [15, 1]]}
==>2={v[4]=[[1, 0]], v[8]=[[22, 0]]}
==>3={v[16]=[[16, 0]]}       

这篇关于在gremlin查询中显示子级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆