TinkerPop中的遍历:图的遍历中谓词、栅栏、范围和Lambda的说明

关于谓词的注意事项

PFunction<Object,Boolean>形式的谓词。也就是说,给定一些对象,返回true或false。所提供的谓词在下表中概述,并用于各种步骤,例如has()where()is()等。

Predicate Description
eq(object) Is the incoming object equal to the provided object?
neq(object) Is the incoming object not equal to the provided object?
lt(number) Is the incoming number less than the provided number?
lte(number) Is the incoming number less than or equal to the provided number?
gt(number) Is the incoming number greater than the provided number?
gte(number) Is the incoming number greater than or equal to the provided number?
inside(number,number) Is the incoming number greater than the first provided number and less than the second?
outside(number,number) Is the incoming number less than the first provided number or greater than the second?
between(number,number) Is the incoming number greater than or equal to the first provided number and less than the second?
within(objects…) Is the incoming object in the array of provided objects?
without(objects…) Is the incoming object not in the array of the provided objects?

除了表中的表达式外,还有比如not()test()等,如下:

gremlin> not(neq(2)) //1
==>eq(2)
gremlin> not(within('a','b','c')).test('d') //2
==>true
gremlin> not(within('a','b','c')).test('a')
==>false

Note
上述谓词表达式来自以下静态引入import static org.apache.tinkerpop.gremlin.process.traversal.P.*.

关于栅栏步骤的注意事项

Gremlin主要是一个懒惰的流处理语言。这意味着Gremlin在从遍历开始/头部获取更多数据之前,会尽其所能地完全处理当前在遍历管道中的任何遍历器。然而,在许多情况下,完全懒惰的计算是不可能的(或不切实际的)。当计算不是懒惰的时候,就存在一个“栅栏步骤”。有三种类型的障碍:

  • CollectingBarrierStep
    order(), sample(), aggregate(), barrier()等步骤。在步骤之前的所有遍历器被放入一个集合中,然后以某种方式(例如有序的)处理,然后将该集合一个一个地“流入(drained)”到下一个步骤。

  • ReducingBarrierStep
    fold(), count(), sum(), max(), min()等。在步骤之前的所有遍历器都被做“reduce”处理,并且一旦所有先前的遍历器被处理,一个单一的“reduced后的值(reduced value)”遍历器被发送到下一步。

  • SupplyingBarrierStep
    cap()。所有在这个步骤之前的遍历器被迭代(不处理),然后一些供应商(provided supplier)产生一个单一的遍历器来继续下一步。

在Gremlin OLAP中,在每个相邻顶点步骤的末尾可以引入一个栅栏。

关于范围的注意事项

范围枚举有两个常量:Scope.local和Scope.global。作用域决定作用域的特定步是与当前对象(本地)相关的,还是整个对象(全局)的整个对象流。
以下例子:

gremlin> g.V().has('name','marko').out('knows').count() //1
==>2
gremlin> g.V().has('name','marko').out('knows').fold().count() //2
==>1
gremlin> g.V().has('name','marko').out('knows').fold().count(local) //3
==>2
gremlin> g.V().has('name','marko').out('knows').fold().count(global) //4
==>1

解释上述例子:
1、Marko知道两个人。
2、Marko的朋友列表被创建,因此,一个对象被计数(即单个列表)。
3、Marko的朋友列表被创建,并且本地计数产生该列表中的对象的数量。
4、count(global)count()相同,因为大多数作用域步骤的默认行为是全局的。

  • 支持范围的步骤
    count()
    dedup()
    max()
    mean()
    min()
    order()
    range()
    limit()
    sample()
    tail()

Lambdas的注意事项

Gremlin支持Lambda表达式,但是并不推荐使用。以下使用Lambda表达式与不用Lambda表达式进行相同查询的语法比较:

gremlin> g.V().filter{it.get().value('name') == 'marko'}.
               flatMap{it.get().vertices(OUT,'created')}.
               map {it.get().value('name')} // 使用Lambda
==>lop
gremlin> g.V().has('name','marko').out('created').values('name') //不使用Lambda
==>lop

建议用户当且仅当所需功能只有lambda表达式时,才使用lambda表达式。原因是,lambda不能被Gremlin的编译策略优化,因为它们不能被程序检查(参见遍历策略)。

注意,目前(截止0.2.0版本)还不能在Gremlin-Server上执行远程发来的lambda,也没有支持远程执行的驱动程序。

原文地址:https://www.cnblogs.com/myitroad/p/7778500.html