2

我正在运行 Neo4j 2.0.1。连同 2.0.1 的空间插件。连同 2.0.1 的 JDBC 驱动程序。我创建了一个简单的点图层和一个索引来进行空间查询。如果我在浏览器中输入以下查询,则会返回相应的节点:

START n=node:geom('withinDistance:[15.0,60.0, 200.0]') RETURN n

我在我的 servlet 中编写了一个方法来发送这个查询:

   //connect to database
   try {
            if(connection == null || connection.isClosed())
            {

                Class.forName("org.neo4j.jdbc.Driver"); // load jdbc driver
                connection = DriverManager.getConnection("jdbc:neo4j://localhost:7474"); 

            }
        } catch (SQLException e) {
            System.out.println("could not connect do DB: <br>" + e.getCause()
                    + "<br>" + e.getMessage());

        } catch (ClassNotFoundException e) {
            System.out.println("Could not load JDBC DRIVER");
            e.printStackTrace();
        }

然后,我尝试向服务器发送请求:

    @Path("findRange")
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String findRange(@QueryParam("lat") double lat,
             @QueryParam("lon") double lon,
             @QueryParam("d") double distance)
    {
        connectToDbIfNecessary();
        try(PreparedStatement p = connection.prepareStatement(FIND_WITHIN_RANGE))
        {
            p.setObject(1, lat); 
            p.setObject(2, lon); 
            p.setObject(3, distance);
            ResultSet rs = p.executeQuery();
            return getServerResponse(rs);


        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "failure during geospatial query:\n"+e.getErrorCode()+"<br>\n"+e.getMessage()+"<br>"+e.getCause();
        }

    }

我发送的准备好的声明是:

private static final String FIND_WITHIN_RANGE = "START n=node:geom('withinDistance:[{1},{2}, {3}]') RETURN n";

当我在 Tomcat 7.0.53 上执行此操作时,出现以下异常:

地理空间查询期间失败:0 执行查询时出错 START n=node:geom('withinDistance:[{1},{2}, {3}]') RETURN n with params {3=1.0E8, 2=60.5, 1= 14.9} java.lang.RuntimeException:执行密码语句时出错 [{code=Neo.DatabaseError.Statement.ExecutionFailure, message=null, stackTrace=java.lang.NullPointerException at org.neo4j.gis.spatial.indexprovider.LayerNodeIndex .query(LayerNodeIndex.java:249) at org.neo4j.gis.spatial.indexprovider.LayerNodeIndex.query(LayerNodeIndex.java:293) at org.neo4j.cypher.internal.spi.v2_0.TransactionBoundExecutionContext$NodeOperations.indexQuery(TransactionBoundExecutionContext .scala:166) 在 org.neo4j.cypher.internal.compiler.v2_0.spi.DelegatingOperations.indexQuery(DelegatingQueryContext.scala:113) 在 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) 在 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) 在 org.neo4j.cypher。internal.compiler.v2_0.spi.ExceptionTranslatingQueryContext$ExceptionTranslatingOperations.indexQuery(ExceptionTranslatingQueryContext.scala:142) 在 org.neo4j.cypher.internal.compiler.v2_0.spi.DelegatingOperations.indexQuery(DelegatingQueryContext.scala:113) 在 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)在 org.neo4j.cypher.internal.compiler.v2_0.executionplan.builders.EntityProducerFactory$$anon$1.apply(EntityProducerFactory.scala:34) 在 org.neo4j.cypher.internal.compiler.v2_0.pipes 的 org.neo4j.cypher.internal.compiler.v2_0.pipes.StartPipe$$anonfun$internalCreateResults$1.apply(StartPipe.scala:34)。 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:38) at org.neo4j.cypher.internal.compiler.v2_0.ClosingIterator$$anonfun$hasNext$1.apply(ClosingIterator.scala:37) at org. neo4j.cypher.internal.compiler.v2_0.ClosingIterator$$anonfun$hasNext$1.apply(ClosingIterator.scala:37) at org.neo4j.cypher.internal.compiler.v2_0.ClosingIterator.failIfThrows(ClosingIterator.scala:91) at org.neo4j.cypher.internal.compiler.v2_0.ClosingIterator.hasNext(ClosingIterator.scala:37) at org.neo4j.cypher.internal.compiler.v2_0.PipeExecutionResult.hasNext(PipeExecutionResult.scala:166) at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:327) at scala.collection .convert.Wrappers$IteratorWrapper.hasNext(Wrappers.scala:29) at org.neo4j.cypher.internal.compiler.v2_0.PipeExecutionResult$$anon$1.hasNext(PipeExecutionResult.scala:74) at org.neo4j.server.rest .transactional.ExecutionResultSerializer.writeRows(ExecutionResultSerializer.java:291) 在 org.neo4j.server.rest.transactional.ExecutionResultSerializer.statementResult(ExecutionResultSerializer.java:103) 在 org.neo4j.server.rest.transactional.TransactionHandle.executeStatements(TransactionHandle .java:251) 在 org.neo4j.server.rest.transactional.TransactionHandle.commit(TransactionHandle.java:189) 在 org.neo4j.server.rest。transactional.TransactionHandle.commit(TransactionHandle.java:109) at org.neo4j.server.rest.web.TransactionalService$2.write(TransactionalService.java:198) at com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider .writeTo(StreamingOutputProvider.java:71) 在 com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider.writeTo(StreamingOutputProvider.java:57) 在 com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse .java:306) 在 com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1437) 在 com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349) ) 在 com.sun.jersey.spi.container.servlet.WebComponent 的 com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)。com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537) 上的服务(WebComponent.java:416) com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer. java:699) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:848) 在 org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:698) 在 org.eclipse.jetty.servlet.ServletHandler $CachedChain.doFilter(ServletHandler.java:1506) 在 org.neo4j.server.rest.security.SecurityFilter.doFilter(SecurityFilter.java:112) 在 org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java :1477) 在 org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:211) 在 org.eclipse.jetty 的 org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:503)。服务器处理程序。org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:432) 的 org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:175) 的 ContextHandler.doHandle(ContextHandler.java:1096)在 org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1030) 在 org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136) 在 org.eclipse.jetty.server .handler.HandlerList.handle(HandlerList.java:52) 在 org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) 在 org.eclipse.jetty.server.Server.handle(Server.java :445)在 org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:229) 在 org.eclipse.jetty.io 的 org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:268)。 AbstractConnection$ReadCallback.run(AbstractConnection.java:358)在 org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:601) 在 org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:532) 在 java.lang。 Thread.run(Thread.java:722) }]

当我通过手动向文档中指定的 URL 发送 http 请求来执行查询时,我没有收到异常。我不知道为什么会发生此异常。谁能指出我正确的方向?

编辑:似乎索引查询无法参数化。但是,如果我按照 Stefan Armbruster 的建议进行操作,则会发生新的异常(也许这是 0.12 空间实现中的另一个问题)。

地理空间查询期间失败:0 执行查询时出错 START n=node:geom({1}) RETURN n with params {1='withinDistance:[14.9,60.5,1.0E8]'} java.lang.RuntimeException: Error execution cypher statement (s) [{code=Neo.DatabaseError.Statement.ExecutionFailure, message=only within, withinDistance 和 bbox 被实现。, stackTrace=java.lang.UnsupportedOperationException: only within, withinDistance 和 bbox 被实现。在 org.neo4j.gis.spatial.indexprovider.LayerNodeIndex.query(LayerNodeIndex.java:281) 在 org.neo4j.gis.spatial.indexprovider.LayerNodeIndex.query(LayerNodeIndex.java:293) 在 org.neo4j.cypher.internal .spi.v2_0.TransactionBoundExecutionContext$NodeOperations.indexQuery(TransactionBoundExecutionContext.scala:166) 在 org.neo4j.cypher.internal.compiler.v2_0.spi.DelegatingOperations.indexQuery(DelegatingQueryContext.scala:

这个问题似乎与2013 年年中的这个问题相似。

4

2 回答 2

3

不是 100% 肯定,但我猜你不能参数化索引查询的部分。所以用三个参数替换你的查询

START n=node:geom('withinDistance:[{1},{2}, {3}]') RETURN n

params 
1:15.0,
2:60.0, 
3:200.0

使用索引搜索词的单个参数:

START n=node:geom({1}) RETURN n

params 
1: 'withinDistance:[15.0, 60.0, 200.0]'
于 2014-05-26T10:00:18.573 回答
1

正如 Stefan Armbruster 指出的那样,索引查询似乎不可参数化。即使只有一个参数,也无法执行查询。所以有2个选项

1)old school 向空间文档中指定的 url 发送 HTTP POST 请求

2)手动创建发送到事务端点的字符串:

String pay = "START n=node:geom('withinDistance:["+lat+","+lon+","+distance+"]') RETURN n";

将查询发送到端点,无需执行任何操作或替换占位符。然后端点将接受查询。

于 2014-05-28T13:53:36.827 回答