【问题标题】:Gremlin.compile() cannot execute query that works in gremlin consoleGremlin.compile() 无法执行在 gremlin 控制台中有效的查询
【发布时间】:2014-12-21 12:47:58
【问题描述】:

当我在 gremlin 控制台中执行以下命令时,我得到了预期的结果。

g.V('name', 'a').next().query().has('b', GREATER_THAN_EQUAL, 100).orderBy('timestamp', Order.DESC).edges()

现在我正在尝试从 Java 执行相同的操作(遵循 this 指南)但是我无法让它工作。

我试过了

Pipe pipe = Gremlin.compile("_().query().has('b', GREATER_THAN_EQUAL, 100).orderBy('timestamp', Order.DESC).edges()");
pipe.setStarts(new SingleIterator<Vertex>(graph.getVertices("name", 'a').iterator().next()));
for(Object name : pipe) {

}

javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException:没有方法签名: com.tinkerpop.gremlin.groovy.GremlinGroovyPipeline.query() 是 适用于参数类型:() 值:[] 可能的解决方案: 每个(),每个(groovy.lang.Closure),grep(), 树([Lcom.tinkerpop.pipes.PipeFunction;), 树([Lgroovy.lang.Closure;), 树(com.tinkerpop.pipes.util.structures.Tree)

还有这个

Pipe pipe = Gremlin.compile("_().next().query().has('b', GREATER_THAN_EQUAL, 100).orderBy('timestamp', Order.DESC).edges()");
pipe.setStarts(new SingleIterator<Vertex>(graph.getVertices("name", 'a').iterator().next()));
for(Object name : pipe) {

}

javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException:没有方法签名: com.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.query() 适用于参数类型:() 值:[] 可能的解决方案: 每个(),每个(groovy.lang.Closure),grep(),grep(java.lang.Object), 任何(),转储()

有什么想法吗?

【问题讨论】:

    标签: gremlin titan


    【解决方案1】:

    我觉得这条线很可疑:

    Pipe pipe = Gremlin.compile("_().next().query().has('b', GREATER_THAN_EQUAL, 100).orderBy('timestamp', Order.DESC).edges()");
    

    您正试图从无法评估为 Pipeline 的东西中编译出 Pipe。换句话说,您从标识管道 (_()) 开始,然后将其 next() 并下拉到返回 edges() edges() 的顶点查询中返回 Iterator 而不是 Pipeline。如果您查看Gremlin.compile 的示例,Gremlin 字符串的评估代码将返回一个管道。

    Pipe pipe = Gremlin.compile("_().out('knows').name");
    

    我的猜测是,如果您改为将代码更改为(未经测试):

    Pipe pipe = Gremlin.compile("_().outE.has('b', GREATER_THAN_EQUAL, 100).order{it.b.timestamp <=> it.a.timestamp}");
    pipe.setStarts(new SingleIterator<Vertex>(graph.getVertices("name", 'a').iterator().next()));
    for(Object name : pipe) {
    
    }
    

    你可能会取得一些成功。我想如果这行得通,那么你会想弄清楚如何重新优化你的查询,因为我猜一些后端可以优化 Vertex 查询的orderBy,而order Gremlin 步骤只是一个 in -内存排序。

    【讨论】:

      【解决方案2】:

      好的,所以我决定使用 GremlinGroovyScriptEngine 而不是 Gremlin.compile()。这种方法也在同一个guide 上进行了描述,我实际上更喜欢这种方法,因为它给了我参数化并且我不需要修改原始查询(将 g. 替换为 _())。

      ScriptEngine engine = new GremlinGroovyScriptEngine();
      
      Bindings bindings = engine.createBindings();
      bindings.put("g", graph);
      bindings.put("value", 100); 
      bindings.put("DESC", Order.DESC);
      
      engine.eval("g.V('name', 'a').next().query().has('b', Compare.GREATER_THAN_EQUAL, value).orderBy('timestamp', DESC).edges()", bindings);
      

      我仍然想知道为什么 Gremlin.compile 不起作用,希望以上内容对其他人有所帮助。

      【讨论】:

        猜你喜欢
        • 2018-04-22
        • 2017-09-18
        • 2019-07-27
        • 2012-07-27
        • 1970-01-01
        • 2018-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多