使用来自 Java 和 Tinkerpop 的 Neo4j 算法
Use Neo4j algorithms from Java and Tinkerpop
我使用 Neo4J 作为我的底层 GraphDB,在我的 Java 应用程序中使用 Tinkerpop 3.3.1。我使用 Neo4JGraph.open ("/tmp/filename") 在 Java 中创建图表,然后从那里我只使用 Tinkerpop Java API 创建节点、边和 运行 遍历。
我想在我的 Tinkerpop 图上执行 Dijkstra 最短路径。作为我的底层 GraphDB 的 Neo4J 内置了这个算法,所以我想使用它。我使用 make this call 来获取 Neo4J 图,并从中获取一些 Neo4JNode 类型。
Neo4jGraphAPI neo4J = this.graphPV.getBaseGraph();
Neo4jNode start = neo4J.getNodeById(1);
Neo4jNode end = neo4J.getNodeById(2);
这行得通,但现在我不确定下一步该怎么做。我想我会将这些节点传递给 Neo4J 探路者,但是 findSinglePath 方法采用 Node 类型,而不是基于 Tinkerpop 的 Neo4JNode。我试过铸造,抛出了一个例外。我可以将 Tinkerpop Neo4JNode 转换为底层 Neo4J API 将接受的 Neo4J 节点类型吗?
dijkstraPathFinder = GraphAlgoFactory.dijkstra(expander, costEvaluator );
WeightedPath path = dijkstraPathFinder.findSinglePath((Node)start, (Node)end );
结果:
WARNING: service exception java.lang.ClassCastException:
org.neo4j.tinkerpop.api.impl.Neo4jNodeImpl cannot be cast to
org.neo4j.graphdb.Node
Neo4jNodeImpl
和 Neo4jRelationshipImpl
都扩展了 Neo4jEntityImpl
,它有一个 public getEntity()
方法 returns 原生 neo4j Node
/Relationship
.
不幸的是,getEntity()
不在 Neo4jEntity
界面中(因此不在 Neo4jNode
和 Neo4jRelationship
界面中)。因此,要使用 getEntity()
,您的项目必须添加对 impl module 的依赖并使用 class 转换。例如:
Neo4jGraphAPI neo4J = this.graphPV.getBaseGraph();
Neo4jNode start = neo4J.getNodeById(1);
Node nativeStart = ((Neo4jNodeImpl)start).getEntity();
或者,如果您不想依赖 impl 模块,还有一个性能较低的选项。您可以使用 Neo4jEntity.getId()
方法获取底层 neo4j Node/Relationship 的本机 ID,然后执行 neo4j 查询以获取 Node/Relationship.
我使用 Neo4J 作为我的底层 GraphDB,在我的 Java 应用程序中使用 Tinkerpop 3.3.1。我使用 Neo4JGraph.open ("/tmp/filename") 在 Java 中创建图表,然后从那里我只使用 Tinkerpop Java API 创建节点、边和 运行 遍历。
我想在我的 Tinkerpop 图上执行 Dijkstra 最短路径。作为我的底层 GraphDB 的 Neo4J 内置了这个算法,所以我想使用它。我使用 make this call 来获取 Neo4J 图,并从中获取一些 Neo4JNode 类型。
Neo4jGraphAPI neo4J = this.graphPV.getBaseGraph();
Neo4jNode start = neo4J.getNodeById(1);
Neo4jNode end = neo4J.getNodeById(2);
这行得通,但现在我不确定下一步该怎么做。我想我会将这些节点传递给 Neo4J 探路者,但是 findSinglePath 方法采用 Node 类型,而不是基于 Tinkerpop 的 Neo4JNode。我试过铸造,抛出了一个例外。我可以将 Tinkerpop Neo4JNode 转换为底层 Neo4J API 将接受的 Neo4J 节点类型吗?
dijkstraPathFinder = GraphAlgoFactory.dijkstra(expander, costEvaluator );
WeightedPath path = dijkstraPathFinder.findSinglePath((Node)start, (Node)end );
结果:
WARNING: service exception java.lang.ClassCastException: org.neo4j.tinkerpop.api.impl.Neo4jNodeImpl cannot be cast to org.neo4j.graphdb.Node
Neo4jNodeImpl
和 Neo4jRelationshipImpl
都扩展了 Neo4jEntityImpl
,它有一个 public getEntity()
方法 returns 原生 neo4j Node
/Relationship
.
不幸的是,getEntity()
不在 Neo4jEntity
界面中(因此不在 Neo4jNode
和 Neo4jRelationship
界面中)。因此,要使用 getEntity()
,您的项目必须添加对 impl module 的依赖并使用 class 转换。例如:
Neo4jGraphAPI neo4J = this.graphPV.getBaseGraph();
Neo4jNode start = neo4J.getNodeById(1);
Node nativeStart = ((Neo4jNodeImpl)start).getEntity();
或者,如果您不想依赖 impl 模块,还有一个性能较低的选项。您可以使用 Neo4jEntity.getId()
方法获取底层 neo4j Node/Relationship 的本机 ID,然后执行 neo4j 查询以获取 Node/Relationship.