将gremlin-groovy转换为java退出关键字/ sum()步骤的用法

问题描述 投票:1回答:1

https://groups.google.com/forum/#!topic/gremlin-users/UZMD1qp5mfg使用脚本通过示例讨论了一个组:

gremlin> g.V.outE.groupBy{it.inV.next().name}{it.weight}{it.sum().doubleValue()}.cap.orderMap(T.decr)

基于现代示例图:tinkerpop modern example graph

我想用Java尝试这个,但是:

  1. 我不知道如何在java中实现“it”关键字。
  2. cap步骤需要java中的两个参数
  3. 从Tinkerpop 3.4.0开始似乎没有orderMap

首先,我只有:

    // g.V.outE.groupBy{it.inV.next().name}{it.weight}{it.sum().doubleValue()}.cap.orderMap(T.decr)
    GraphTraversalSource g = TinkerFactory.createModern().traversal();
    Map<Object, Object> map = g.V().outE().group().by().next();
    if (debug) {
      System.out.println(map.values().size());
      for (Entry<Object, Object> entry : map.entrySet()) {
        System.out
            .println(String.format("%s=%s", entry.getKey(), entry.getValue()));
      }
    }

给出调试输出:

6
e[7][1-knows->2]=[e[7][1-knows->2]]
e[8][1-knows->4]=[e[8][1-knows->4]]
e[9][1-created->3]=[e[9][1-created->3]]
e[10][4-created->5]=[e[10][4-created->5]]
e[11][4-created->3]=[e[11][4-created->3]]
e[12][6-created->3]=[e[12][6-created->3]]

然后我尝试了一下这样的事情:

import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.*;

 /**
   * show the given map entries
   * 
   * @param map
   */
  public void showMap(String title, Map<Object, Object> map) {
    System.out.println(title + ":" + map.values().size());
    for (Entry<Object, Object> entry : map.entrySet()) {
      System.out
          .println(String.format("\t%s=%s", entry.getKey(), entry.getValue()));
    }
  }

  public void showObject(String title, Object object) {
    System.out.println(title+":"+object.toString());
  }

  @Test
  /**
   * https://groups.google.com/forum/#!topic/gremlin-users/UZMD1qp5mfg
   * https://stackoverflow.com/questions/55771036/it-keyword-in-gremlin-java
   */
  public void testGroupBy() {
    // gremlin:
    // g.V.outE.groupBy{it.inV.next().name}{it.weight}{it.sum().doubleValue()}.cap.orderMap(T.decr)
    GraphTraversalSource g = TinkerFactory.createModern().traversal();
    debug=true;
    if (debug) {
      g.V().outE().group().by().forEachRemaining(m -> showMap("by()", m));
      g.V().outE().group().by(inV().id())
          .forEachRemaining(m -> showMap("by(inV().id())", m));
      g.V().outE().group("edges").by(inV().id()).cap("edges")
      .forEachRemaining(o -> showObject("cap", o));
}

结果:

by():6
    e[7][1-knows->2]=[e[7][1-knows->2]]
    e[8][1-knows->4]=[e[8][1-knows->4]]
    e[9][1-created->3]=[e[9][1-created->3]]
    e[10][4-created->5]=[e[10][4-created->5]]
    e[11][4-created->3]=[e[11][4-created->3]]
    e[12][6-created->3]=[e[12][6-created->3]]
by(inV().id()):4
    2=[e[7][1-knows->2]]
    3=[e[9][1-created->3], e[11][4-created->3], e[12][6-created->3]]
    4=[e[8][1-knows->4]]
    5=[e[10][4-created->5]]
cap:{2=[e[7][1-knows->2]], 3=[e[9][1-created->3], e[11][4-created->3], e[12][6-created->3]], 4=[e[8][1-knows->4]], 5=[e[10][4-created->5]]}

所以看起来“it”可以简单地省略,而对于cap步骤,必须命名“group”。 gremlin-groovy到Java翻译的其他部分我还是不明白。

上面的脚本如何完全翻译成Java?

java gremlin
1个回答
1
投票

在TinkerPop 3中,它很简单:

g.V().outE().
  group().
    by(inV().values("name")).
    by(values("weight").sum()).
  order(local).
    by(values, desc)

或者以完整的java语法:

import org.apache.tinkerpop.gremlin.structure.Column;
import org.apache.tinkerpop.gremlin.process.traversal.Order;
import org.apache.tinkerpop.gremlin.process.traversal.Scope;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.*;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;

g.V().outE().
 group().
   by(inV().values("name")).
   by(values("weight").sum()).
 order(Scope.local).
   by(Column.values, Order.desc)

结果:

总和:{ripple = 1.0,josh = 1.0,lop = 1.0,vadas = 0.5}

UPDATE

要回答关于使用两个顶点的评论中的问题,它将是这样的:

g.E().
  group().
    by(bothV().values("name").fold()).
    by(values("weight").sum()).
  order(Scope.local).
    by(Column.values, Order.desc)
© www.soinside.com 2019 - 2024. All rights reserved.