unfold
功能:(map)將list<object>展開為objects
備注:
fold()
是相反的操作,可將objects聚合為list
g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").aggregate("x").by("name").cap("x").unfold()
==> "josh"
==> "marko"
==> "peter"
==> "vadas"
g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").values("name").fold().unfold()
==> "marko"
==> "josh"
==> "vadas"
==> "peter"
unoin
功能:(branch)合并子遍歷的結果
備注:單步串行地計算每個子遍歷,故性能較差。
g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").union(outE("knows"),outE("created"))
==> {"label""knows","pk":"1","sk":"4","weight":1.0}
==> {"label""knows","pk":"1","sk":"2","weight":0.5}
==> {"label""created","pk":"1","sk":"3","weight":0.4}
==> {"label""created","pk":"4","sk":"3","weight":0.4}
==> {"label""created","pk":"4","sk":"5","weight":1.0}
==> {"label""created","pk":"6","sk":"3","weight":0.2}
until
功能:(修飾符)指定循環遍歷的終止條件
備注:需要與
repeat()
聯合使用
V
功能:(實體)查詢頂點的信息。
格式:不同的pkey字符串用
;
分割備注:
圖訪問方式:通過
hasLabel()
指定要訪問的頂點對應的圖label
g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person")
==> {"label":"person","age":29,"name":"marko","pk":"1"}
==> {"label":"person","age":32,"name":"josh","pk":"4"}
==> {"label":"person","age":27,"name":"vadas","pk":"2"}
==> {"label":"person","age":35,"name":"peter","pk":"6"}
values
功能:(map)返回字段值
備注:
支持指定返回,默認返回所有
properties()
支持返回字段名和字段值
g("thinkerpop").V("1;2").hasLabel("person").values()
==> 29
==> "marko"
==> "1"
==> 27
==> "vadas"
==> "2"
g("thinkerpop").V("1;2").hasLabel("person").values("name","age")
==> 29
==> "marko"
==> 27
==> "vadas"
where
功能:(filter)過濾
備注:單步支持
P
作為過濾條件
g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").values("name").where(is("marko"))
==> "marko"
g("thinkerpop").V("1;2;3;4;5;6").hasLabel("person").where(values("name").is("marko"))
==> {"label""person","age":29,"name":"marko","pk":"1"}
// 查找人4創造的軟件,并且聯合作者不是人1
// 對實體進行遍歷
g("thinkerpop").E("1").hasLabel("created").inV().aggregate("x").fold().E("4").hasLabel("created").inV().where(P.without("x"))
==> {"label":"software","lang":"java","name":"ripple","pk":"5"}
// 等同于
g("thinkerpop").E("1").hasLabel("created").inV().aggregate("x").fold().E("4").hasLabel("created").inV().as("y").where("y",P.without("x"))
withSack
功能:(修飾符)為實體的sack值定義初始值、分裂規則和合并規則。
語法:
withSack(initialValueSupplier[,Splitter][,mergeOperator])
此單步與開源Gremlin不完全一致
initialValueSupplier
:指定sack的初始值。支持下列三種初始化方法:
kv
:為每個頂點單獨指定sack初始值kkv
:為每條邊單獨指定sack初始值normal
:為實體(頂點/邊)指定相同的sack初始值。支持為每個實體指定多種sack。
用JSON描述sack的初始值。
由于雙引號存在解析沖突,JSON中的雙引號需要轉義。
kv
中,JSON最外層的key對應于頂點的pkey。kkv
中,JSON最外層的key對應于邊的pkey,第二層的key對應于邊的skeySplitter
:指定sack的分裂規則,支持下列三種方法:Splitter.identity
:適用于數值或者string類型。性能優。Splitter.fastclone
:適用于迭代器類型(list、map)。性能優、風險低,推薦使用。僅在必要情況保證sack值唯一
Splitter.clone
:適用于上述兩種Splitter不適用的情況。性能良,無風險保證sack值唯一
mergeOperator
:指定barrier()
時sack的合并規則,支持下列方法:Operator.sum
:sack值相加Operator.assign
:為sack重新賦值Operator.mult
:sack值相乘Operator.div
:sack值相除Operator.minus
:sack值取最小值Operator.addall
:保留所有sack值備注:
如果沒有指定
mergeOperator
,barrier()
不會合并相同的objects。浮點類型的sack存在精度問題
__SACK__
作為內置關鍵字代表當前sack值,是的sack值可以通過表達式參與復雜計算。sack()
返回sack值withSack()
返回實體和sack值
// 初始設置sack=1.0。未指定合并規則,遇到barrier()時不做合并,sack保持初始值不變。
g("thinkerpop").withSack(supplier(normal, float,"1.0")).E("1;2;3;4;5;6").hasLabel("created").inV().barrier().sack()
==> 1.0
==> 1.0
==> 1.0
==> 1.0
// 返回結果前展開合并的結果
g("thinkerpop").withSack(supplier(normal, float,"1.0")).E("1;2;3;4;5;6").hasLabel("created").inV().hasLabel("software").barrier().withSack()
==> {"label""software","lang":"java","name":"lop","pk":"3"},1.0,
==> {"label""software","lang":"java","name":"lop","pk":"3"},1.0,
==> {"label""software","lang":"java","name":"lop","pk":"3"},1.0,
==> {"label""software","lang":"java","name":"ripple","pk":"5"},1.0
// 初始設置sack=1.0。barrier()按照Operator.sum規則合并sack值
g("thinkerpop").withSack(supplier(normal, float,"1.0"),Splitter.identity,Operator.sum).E("1;2;3;4;5;6").hasLabel("created").inV().barrier().sack()
==> 3.0
==> 3.0
==> 3.0
==> 1.0
// 返回結果前展開合并的結果
g("thinkerpop").withSack(supplier(normal, float,"1.0"),Splitter.identity,Operator.sum).E("1;2;3;4;5;6").hasLabel("created").inV().barrier().withSack()
==> {"label""software","lang":"java","name":"lop","pk":"3"},3.0,
==> {"label""software","lang":"java","name":"lop","pk":"3"},3.0,
==> {"label""software","lang":"java","name":"lop","pk":"3"},3.0,
==> {"label""software","lang":"java","name":"ripple","pk":"5"},1.0
// 初始設置sack=1.0。訪問created時,執行sack=sack*weight操作
g("thinkerpop").withSack(supplier(normal, float,"1.0"),Splitter.identity,Operator.sum).E("1;4;6").hasLabel("created").sack(Operator.mult).by("weight").withSack()
==> {"data":[{"label""created","pk":"1","sk":"3","weight":0.4},0.4
==> {"label""created","pk":"4","sk":"3","weight":0.4},0.4
==> {"label""created","pk":"4","sk":"5","weight":1.0},1.0
==> {"label""created","pk":"6","sk":"3","weight":0.2},0.2
// 等同于
g("thinkerpop").withSack(supplier(normal, float,"1.0"),Splitter.identity,Operator.sum).E("1;2;3;4;5;6").hasLabel("created").sack(Operator.assign).by("weight*__SACK__").withSack()
// 為每個頂點指定不同的sack初始值
g("thinkerpop").withSack(supplier(kv,"{\"1\":0.3,\"4\":0.7,\"6\":0.4}"),Splitter.identity,Operator.sum).E("1;4;6").hasLabel("created").sack(Operator.mult).by("weight").withSack()
==> {"label""created","pk":"1","sk":"3","weight":0.4},0.12
==> {"label""created","pk":"4","sk":"3","weight":0.4},0.27999999999999999
==> {"label""created","pk":"4","sk":"5","weight":1.0},0.7
==> {"label""created","pk":"6","sk":"3","weight":0.2},0.08000000000000002
// 為每個頂點指定多種sack,每種sack定義不同的修改規則
g("thinkerpop").withSack(supplier(normal,"{\"k1\":[\"abc\"],\"k2\":1}"),Splitter.fastclone,"{\"k1\":\"Operator.addall\",\"k2\":\"Operator.sum\"}").V("1;2;3;4;5;6").hasLabel("person").sack("k2",Operator.sum).by("to_int(age)").sack("k1",Operator.addall).by(__.values("name").fold()).sack()
==> {"\"k1\"":["abc","marko"],"\"k2\"":30}
==> {"\"k1\"":["abc","josh"],"\"k2\"":33}
==> {"\"k1\"":["abc","vadas"],"\"k2\"":28}
==> {"\"k1\"":["abc","peter"],"\"k2\"":36}
withStrategies
功能:(策略)為遍歷添加定制化策略
備注:目前支持下列策略
此單步與開源Gremlin不完全一致
PathRecordStrategy
:支持定制path()
記錄的信息類型PushDownStrategy
:定制獲取sideEffect的單步的執行位置