【问题标题】:Gremlin Groovy ClassCastException with Frames带框架的 Gremlin Groovy ClassCastException
【发布时间】:2015-09-30 16:29:50
【问题描述】:

我在使用与 tinkerpop 框架关联的 @GremlinGroovy 注释时收到以下错误。

java.lang.ClassCastException: com.thinkaurelius.titan.graphdb.relations.CacheEdge cannot be cast to com.tinkerpop.blueprints.Vertex
    at com.tinkerpop.frames.structures.FramedVertexIterable$1.next(FramedVertexIterable.java:36)
    at com.tinkerpop.frames.annotations.gremlin.GremlinGroovyAnnotationHandler.processVertex(GremlinGroovyAnnotationHandler.java:75)
    at com.tinkerpop.frames.annotations.gremlin.GremlinGroovyAnnotationHandler.processElement(GremlinGroovyAnnotationHandler.java:114)
    at com.tinkerpop.frames.annotations.gremlin.GremlinGroovyAnnotationHandler.processElement(GremlinGroovyAnnotationHandler.java:30)
    at com.tinkerpop.frames.FramedElement.invoke(FramedElement.java:83)
    at com.sun.proxy.$Proxy81.getProxyCandidateEdgeFromPersonUuid(Unknown Source)
    at com.company.prod.domain.Person$Impl.toImpl(Person.java:100)
    ....

以下行导致错误:

FooEdge fe = foo.getFooEdgeFromUuid(this.getUuid());

调用这个方法的是什么:

@GremlinGroovy("it.outE('has').filter{it.inV().has('uuid', T.eq, uuid).hasNext()}")
FooEdge getFooEdgeFromUuid(@GremlinParam("uuid") String uuid);

我还尝试了以下遍历(导致相同的错误):

@GremlinGroovy("it.out('has').has('uuid', T.eq, uuid).inE('has')")

但是,当我打开一个 gremlin shell 来测试相同的精确遍历时 - 一切正常。关于可能导致问题的任何想法?

【问题讨论】:

    标签: java groovy gremlin tinkerpop-frames


    【解决方案1】:

    这与其说是一种解决方法,不如说是一种解决方法。除了使用@GremlinGroovy 注释,还可以将gremlin 与@JavaHandler 注释一起使用。

    @JavaHandler
    void getFooEdgeFromUuid(String uuid);
    
    abstract class Impl implements JavaHandlerContext<Vertex>, Foo {
        public FooEdge getFooEdgeFromUuid(String uuid) {
            return frameEdges(
                    gremlin().out("has")
                            .has("person-uuid", Tokens.T.eq, uuid)
                            .inE("has"),
                    FooEdge.class).iterator().next();
        }
    }
    

    【讨论】:

      【解决方案2】:

      我认为您没有正确使用该注释的 Frames 文档:

      https://github.com/tinkerpop/frames/wiki/Gremlin-Groovy

      首先,请注意两者:

      @GremlinGroovy("it.outE('has').filter{it.inV().has('uuid', T.eq, uuid).hasNext()}")
      FooEdge getFooEdgeFromUuid(@GremlinParam("uuid") String uuid);
      

      和:

      @GremlinGroovy("it.out('has').has('uuid', T.eq, uuid).inE('has')")
      

      返回一个Iterator,所以这也不是很有帮助,因为您需要在getFooEdgeFromUuid() 中返回某种形式的List。假设您知道该查询将仅且始终返回一个边这一事实,那么可能要做的适当的事情是:

      @GremlinGroovy("it.out('has').has('uuid', T.eq, uuid).inE('has').next()")
      

      我说“总是”,因为没有它,否则你会得到一个NoSuchElementException。这样,要正确对齐类型,您可以这样做:

      @GremlinGroovy("it.outE('has').filter{it.inV().has('uuid', T.eq, uuid)}")
      Iterable<FooEdge> getFooEdgesFromUuid(@GremlinParam("uuid") String uuid);
      

      当然,所有这些都可能行不通,因为我在文档中看到了这句话:

      可以使用 Gremlin 路径表达式作为 通过 GremlinGroovyModule 确定顶点邻接。

      换句话说,@GremlinGroovy 的使用是为了返回框架顶点(而不是你试图做的边)。如果我上面建议的方法不起作用,那么使用 @JavaHandler 的解决方法可能是您的最佳选择。

      【讨论】:

      • 我之所以使用 hasNext() 是因为过滤器闭包需要一个布尔结果来确定特定顶点是否通过。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-31
      • 2016-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多