以下代码生成以下奇怪的异常:
package com.bmt.contain.spatial.test;
import org.neo4j.cypher.javacompat.ExecutionEngine;
import org.neo4j.cypher.javacompat.ExecutionResult;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.graphdb.index.Index;
import org.neo4j.gis.spatial.indexprovider.SpatialIndexProvider;
import com.vividsolutions.jts.geom.Coordinate;
public class SpatialTest {
/**
* @param args
*/
public static void main(String[] args){
GraphDatabaseService graphDB = new GraphDatabaseFactory()
.newEmbeddedDatabaseBuilder("var/geo")
.setConfig(GraphDatabaseSettings.allow_store_upgrade, "true")
.newGraphDatabase();
Index<Node> index = null;
try (Transaction tx = graphDB.beginTx()){
index = createSpatialIndex(graphDB, "Coordinates");
tx.success();
}
Node ship = null;
try (Transaction tx = graphDB.beginTx();) {
ship = graphDB.createNode(MyLabels.Ship);
ship.setProperty("Name", new String("Phils Powerboat"));
tx.success();
}
routeGenerator(graphDB, ship, index);
try (Transaction tx = graphDB.beginTx()){
ExecutionEngine engine = new ExecutionEngine(graphDB);
ExecutionResult result = engine.execute("START n=node:Coordinates('withinDistance:[100, 9.3, 8.2]') RETURN n");
System.out.println(result.dumpToString());
}
try (Transaction tx = graphDB.beginTx();) {
ExecutionEngine engine = new ExecutionEngine(graphDB);
ExecutionResult result = engine.execute("MATCH (n) \n RETURN n");
System.out.println(result.dumpToString());
tx.success();
}
graphDB.shutdown();
}
/**
* This returns a new index unless an index with this name already exists, in which case it is returned.
* @param graphDb
* @param indexName
* @return
*/
private static Index<Node> createSpatialIndex(GraphDatabaseService graphDb, String indexName) {
return graphDb.index().forNodes(indexName, SpatialIndexProvider.SIMPLE_WKT_CONFIG);
}
private static void routeGenerator(GraphDatabaseService graphDb, Node startNode, Index<Node> index){
try (Transaction tx = graphDb.beginTx();) {
Coordinate start = new Coordinate(Math.random() * 10,
Math.random() * 10);
for (int i = 0; i < 20; i++) {
Node locationNode = graphDb.createNode(MyLabels.Location);
locationNode.setProperty("wkt", String.format("POINT(%s %s)", start.x, start.y));
Relationship rel = startNode.createRelationshipTo(locationNode, RelTypes.hasLocation);
start = new Coordinate((start.x + 0.1), start.x + Math.random()* 0.2);
}
tx.success();
}
}
}
该测试应该创建一个随机路由并返回起始点一定距离内的节点。但是,行
ExecutionResult result = engine.execute("START n=node:Coordinates('withinDistance:[100, 9.3, 8.2]') RETURN n");
System.out.println(result.dumpToString());
生成错误:&#34;线程中的异常&#34; main&#34; java.lang.ClassCastException:java.lang.Long无法强制转换为java.lang.Double&#34; - 我已经检查了这一点,注意到注释掉System.out行意味着它运行得很好。
请注意,我在运行之前清理了目录,因此目录中不应该有其他节点。完整的堆栈跟踪在
之下Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Double
at org.neo4j.gis.spatial.indexprovider.LayerNodeIndex.query(LayerNodeIndex.java:228)
at org.neo4j.gis.spatial.indexprovider.LayerNodeIndex.query(LayerNodeIndex.java:290)
at org.neo4j.cypher.internal.spi.v2_0.TransactionBoundQueryContext$NodeOperations.indexQuery(TransactionBoundQueryContext.scala:169)
at org.neo4j.cypher.internal.compiler.v2_0.spi.DelegatingOperations.indexQuery(DelegatingQueryContext.scala:116)
at org.neo4j.cypher.internal.compiler.v2_0.spi.ExceptionTranslatingQueryContext$ExceptionTranslatingOperations.org$neo4j$cypher$internal$compiler$v2_0$spi$ExceptionTranslatingQueryContext$ExceptionTranslatingOperations$$super$indexQuery(ExceptionTranslatingQueryContext.scala:142)
at org.neo4j.cypher.internal.compiler.v2_0.spi.ExceptionTranslatingQueryContext$ExceptionTranslatingOperations$$anonfun$indexQuery$1.apply(ExceptionTranslatingQueryContext.scala:142)
at org.neo4j.cypher.internal.compiler.v2_0.spi.ExceptionTranslatingQueryContext$ExceptionTranslatingOperations$$anonfun$indexQuery$1.apply(ExceptionTranslatingQueryContext.scala:142)
at org.neo4j.cypher.internal.compiler.v2_0.spi.ExceptionTranslatingQueryContext.org$neo4j$cypher$internal$compiler$v2_0$spi$ExceptionTranslatingQueryContext$$translateException(ExceptionTranslatingQueryContext.scala:149)
at org.neo4j.cypher.internal.compiler.v2_0.spi.ExceptionTranslatingQueryContext$ExceptionTranslatingOperations.indexQuery(ExceptionTranslatingQueryContext.scala:142)
at org.neo4j.cypher.internal.compiler.v2_0.spi.DelegatingOperations.indexQuery(DelegatingQueryContext.scala:116)
at org.neo4j.cypher.internal.compiler.v2_0.executionplan.builders.EntityProducerFactory$$anonfun$2$$anonfun$applyOrElse$2.apply(EntityProducerFactory.scala:66)
at org.neo4j.cypher.internal.compiler.v2_0.executionplan.builders.EntityProducerFactory$$anonfun$2$$anonfun$applyOrElse$2.apply(EntityProducerFactory.scala:64)
at org.neo4j.cypher.internal.compiler.v2_0.executionplan.builders.EntityProducerFactory$$anon$1.apply(EntityProducerFactory.scala:35)
at org.neo4j.cypher.internal.compiler.v2_0.executionplan.builders.EntityProducerFactory$$anon$1.apply(EntityProducerFactory.scala:34)
at org.neo4j.cypher.internal.compiler.v2_0.pipes.StartPipe$$anonfun$internalCreateResults$1.apply(StartPipe.scala:34)
at org.neo4j.cypher.internal.compiler.v2_0.pipes.StartPipe$$anonfun$internalCreateResults$1.apply(StartPipe.scala:33)
at scala.collection.Iterator$$anon$13.hasNext(Iterator.scala:371)
at org.neo4j.cypher.internal.compiler.v2_0.ClosingIterator$$anonfun$hasNext$1.apply$mcZ$sp(ClosingIterator.scala:35)
at org.neo4j.cypher.internal.compiler.v2_0.ClosingIterator$$anonfun$hasNext$1.apply(ClosingIterator.scala:34)
at org.neo4j.cypher.internal.compiler.v2_0.ClosingIterator$$anonfun$hasNext$1.apply(ClosingIterator.scala:34)
at org.neo4j.cypher.internal.compiler.v2_0.ClosingIterator.failIfThrows(ClosingIterator.scala:89)
at org.neo4j.cypher.internal.compiler.v2_0.ClosingIterator.hasNext(ClosingIterator.scala:34)
at scala.collection.Iterator$class.foreach(Iterator.scala:727)
at org.neo4j.cypher.internal.compiler.v2_0.ClosingIterator.foreach(ClosingIterator.scala:30)
at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:48)
at scala.collection.mutable.ListBuffer.$plus$plus$eq(ListBuffer.scala:176)
at scala.collection.mutable.ListBuffer.$plus$plus$eq(ListBuffer.scala:45)
at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:273)
at org.neo4j.cypher.internal.compiler.v2_0.ClosingIterator.to(ClosingIterator.scala:30)
at scala.collection.TraversableOnce$class.toList(TraversableOnce.scala:257)
at org.neo4j.cypher.internal.compiler.v2_0.ClosingIterator.toList(ClosingIterator.scala:30)
at org.neo4j.cypher.internal.compiler.v2_0.PipeExecutionResult.eagerResult(PipeExecutionResult.scala:101)
at org.neo4j.cypher.internal.compiler.v2_0.PipeExecutionResult.dumpToString(PipeExecutionResult.scala:107)
at org.neo4j.cypher.internal.compiler.v2_0.PipeExecutionResult.dumpToString$lzycompute(PipeExecutionResult.scala:152)
at org.neo4j.cypher.internal.compiler.v2_0.PipeExecutionResult.dumpToString(PipeExecutionResult.scala:149)
at org.neo4j.cypher.javacompat.ExecutionResult.dumpToString(ExecutionResult.java:102)
at com.bmt.contain.spatial.test.Initialise.main(Initialise.java:46)
答案 0 :(得分:1)
解决方案1 {如果您只想要节点的字符串表示}
ExecutionResult result = engine.execute("START n=node:Coordinates('withinDistance:[100, 9.3, 8.2]') RETURN str(n)");
System.out.println(result.dumpToString());
解决方案2 {如果您希望迭代节点及其属性}
ResourceIterator<Node> resultIterator;
ExecutionResult result = engine.execute("START n=node:Coordinates('withinDistance:[100, 9.3, 8.2]') RETURN n");
resultIterator = result.columnAs("n");
while(resultIterator.hasNext()){
System.out.println(resultIterator.next());
}
resultIterator.close();
如果以上两个查询失败,那么您可能想尝试在评论
中建议的内容