Gremlin用例-Int属性和coalesce() [英] Gremlin use case - Int properties and coalesce()

查看:211
本文介绍了Gremlin用例-Int属性和coalesce()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 存储为整数的顶点属性可以递增还是递减?如果是这样,怎么办?

  1. Can vertex properties stored as integers be incremented and decremented? If so, how?

对于固定数据集,coalcece()是否总是返回相同的项目?是否可以将其随机化或以其他任何方式进行?例如,对于所有传入的顶点,即使数据集本身没有更改,也要每次都选择一个随机的顶点.

For a fixed dataset, does coalesce() always return the same item? Is it possible to randomise it or any other way to do it? For instance of all the incoming vertices, choose a random one every time even if the dataset itself is not changed.

推荐答案

存储为整数的顶点属性可以递增还是递减?

Can vertex properties stored as integers be incremented and decremented?

您可以使用sack():

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().property('counter',0).iterate()
gremlin> g.V().
......1>   sack(assign).
......2>     by('counter').
......3>   sack(sum).
......4>     by(constant(1)).
......5>   property('counter', sack()).
......6>   valueMap()
==>[name:[marko],counter:[1],age:[29]]
==>[name:[vadas],counter:[1],age:[27]]
==>[name:[lop],counter:[1],lang:[java]]
==>[name:[josh],counter:[1],age:[32]]
==>[name:[ripple],counter:[1],lang:[java]]
==>[name:[peter],counter:[1],age:[35]]
gremlin> g.V().
......1>   sack(assign).
......2>     by('counter').
......3>   sack(sum).
......4>     by(constant(1)).
......5>   property('counter', sack()).
......6>   valueMap()
==>[name:[marko],counter:[2],age:[29]]
==>[name:[vadas],counter:[2],age:[27]]
==>[name:[lop],counter:[2],lang:[java]]
==>[name:[josh],counter:[2],age:[32]]
==>[name:[ripple],counter:[2],lang:[java]]
==>[name:[peter],counter:[2],age:[35]]

现在您已经在上面的操作中看到了遍历,让我们更仔细地研究遍历:

Let's look at the traversal more closely now that you've seen it in action above:

g.V().
  sack(assign).
    by('counter').
  sack(sum).
    by(constant(1)).
  property('counter', sack()).
  valueMap()

因此,对于每个顶点,您都可以使用assign在其袋"中放置一个值-by('counter')调制器将该赋值定义为"counter"属性的值(在本例中之前已初始化为零) .然后使用sack(sum)定义如何将计数器by(constant(1))或加1"(即,将值加到麻袋中并将其与1相加并将其存储在麻袋中).最后,我们将值从袋子中取出,并用property('counter', sack())的新值覆盖原始的"counter"属性.

So for each vertex, you place a value in its "sack" with assign - the by('counter') modulator defines that assignment as the value of the "counter" property (which was initialized to zero earlier in my example). Then with sack(sum) we define how we increment the counter by(constant(1)) or "by 1" (i.e. take the value in the sack and sum it together with 1 and store that value in the sack). Finally we take the value out of the sack and overwrite the original "counter" property with the new value with property('counter', sack()).

对于固定数据集,coalcece()是否总是返回相同的项目?

For a fixed dataset, does coalesce() always return the same item?

与元素顺序"有关的大多数问题必须推迟到基础图形系统中.如果您的图形数据库按确定的顺序返回元素,那么您的Gremlin应该会.如果需要确定订单并具有完全可移植的查询,则应包括在Gremlin中使用order()步骤.

Most questions that pertains to "element order" must be deferred to the underlying graph system. If your graph database returns elements in a deterministic order then your Gremlin should. If you need to be sure of an order and have a completely portable query then you should include use of order() step in Gremlin.

我相信合并将始终伴随第一个遍历一起返回值,所以在:

I believe that coalesce will always go with the first traversal to return a value, so in:

g.V().coalesce(outE(), inE())

如果当前顶点具有出线边,您将始终得到outE()的结果.要获得随机选择,您可以执行以下操作:

you will always get the result of outE() if the current vertex has outgoing edges. To get a random choice out maybe you could do something like:

g.V().coalesce(outE().fold().coin(0.5), inE().fold()).unfold()

那有点用...换句话说,对于outE()返回某事的情况,它有50%的时间不返回任何内容,因此允许inE()选项起作用.我不确定您到底要做什么,但是也许您可以从等式中摆脱出coalesce(),而只需使用简单的coin()sample()等来解决您的问题.

That kinda works...in other words, for cases where outE() returns something, 50% of the time it would return nothing and thus allow the inE() option to work. I'm not sure exactly what you're after but perhaps you can get coalesce() out of the equation and just use simple coin(), sample(), etc to solve your problem.

这篇关于Gremlin用例-Int属性和coalesce()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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