当前位置: 首页 > 知识库问答 >
问题:

非托管扩展neo4j上的Cypher查询

袁耀
2023-03-14

我的neo4j服务器有一个非托管扩展。

代码如下。

@Path("/helloworld")
public class HelloWorldResource {

    private final GraphDatabaseService database;

    public HelloWorldResource(@Context GraphDatabaseService database) {
        this.database = database;
    }

   @GET
   @Produces(MediaType.TEXT_PLAIN)
   @Path("/{nodeId}")
   public Response hello(@PathParam("nodeId") long nodeId) {
        String res = ""; 

        try ( Transaction ignored = database.beginTx();)
        {
            //@@problem
            Result result = database.execute( "MATCH (n:KISI) where id(n)=1 return n" );

        } catch (Exception e) {
            res = "Error = " + e.getMessage();
        }

        return Response
            .status(Status.OK)
            .entity(("nodeId =" + nodeId + " " + res).getBytes(Charset
                    .forName("UTF-8"))).build();
    }
}

当我部署代码时,我得到了500个内部错误。如果我删除代码

Result result = database.execute( “MATCH (n:KISI) where id(n)=1 return n” );

然后一切都很好。

我检查了日志文件,错误如下

2015年8月13日3:34:36AM com . sun . jersey . SPI . container . container response mapMappableContainerException严重:MappableContainerException中包含的异常无法映射到响应,重新抛出到HTTP容器Java . lang . nosuchmethoderror:org . neo4j . graphdb . graphdatabaseservice . execute(Ljava/lang/String;)Lorg/neo4j/graphdb/Result;在org . neo4j . examples . server . unmanaged . hello world resource . hello(hello world resource . Java:55)在sun . reflect . nativemethodaccessorimpl . invoke 0(本机方法)在sun . reflect . nativemethodaccessorimpl . invoke(nativemethodaccessorimpl . Java:57)在sun . reflect . delegatingmethodaccessorimpl . invoke(delegatingmethodaccessorimpl . Java:43)在Java . reflect . Method . invoke(Method . Java:600_ dispatch(abstractresourcemoddispatchprovider . Java:205)at com . sun . jersey . server . impl . model . method . dispatch . resourcejavamethododdispatcher . dispatch(resourcejavamethododdispatcher . Java:75)at org . neo4j . server . rest . transactional . transactionalrequestdispatcher . dispatch(transactionalrequestdispatcher . Java:139)at com . sun . jersey . server . impl . uri . rules . httpmethodrule . accept(http methodrule . Java:288

那么我的代码有什么问题呢?

共有1个答案

丌官子安
2023-03-14

我猜你的Neo4j发行版本和pom.xml中的maven依赖版本不一样。

但有几件事需要检查:

1)您应该始终关闭结果对象。例:

try(Result result = database.execute( "MATCH (n:KISI) where id(n)=1 return n" )) {
    // do stuff here
}

```

2)异常不是发生在try-catch中,而是稍后发生。您应该将代码更改为:

try ( Transaction tx = database.beginTx()) {
    String query = "MATCH (n:KISI) where id(n)=1 return n";
    // use result with try-with-resource to ensure that it will be closed
    try(Result result = database.execute(query)) {
        // do stuff you need with result here
        return Response.ok("nodeId =" + nodeId).build();
    }

    tx.success(); // mark transaction as successful 
} catch (Exception e) {
    // If exception occurs - send exception message with 500 status code
    // It's good idea to write Exception stacktrace to log there
    return Response.serverError().entity(e.getMessage()).build()      
}

3)您应该检查如何构建非托管扩展. jar文件。

    < li >所有Neo4j依赖项都应在pom.xml中< code >提供(Neo4j发行版中已有)。 < li >检查您的数据库版本和pom.xml中的依赖关系版本是否相同。< code > GraphDatabaseService::execute 方法是最近发明的(没记错的话是2.2.3)。可能您的数据库发行版比您的maven依赖项更老。
 类似资料:
  • 问题内容: 我对项目有一些特定要求,因此决定实施不受管理的Neo4j扩展。 我在Neo4j文档中找到了以下信息: 非托管扩展 测试您的扩展 这看起来是一个好的开始。但是我无法为我工作。 是否有一些更完整的工作示例/模板可以用作参考? 问题答案: “手工”解决方案 TL; DR; - https://github.com/FylmTM/neo4j-unmanaged-extension- templ

  • 我部署了一个Neo4j非托管扩展。可以使用REST客户端调用非托管扩展并成功返回结果。问题是,当我尝试从另一个java类调用/调用非托管扩展时,它会继续抛出未经授权的401。 我使用Spring RestTemboard来调用非托管扩展。 我的代码: 完全错误:

  • 问题内容: 我有Neo4j非托管扩展。我希望将某些服务创建为单例并通过我的资源提供。 像这样: 如何做到这一点? 问题答案: Neo4j具有PluginLifecycle接口,这使我们有可能进入Neo4j服务器生命周期,并为注入博客文章提供我们自己的服务。 因此,我们有服务。让我们以这个为例: 现在,我们需要进行自己的实现: 如您所见,可注射列表目前为空。我们将很快在那里添加我们的服务。 重要提示

  • 键->字符串 principal_name->string 别名->字符串集合 ....... 我还在principal_name、别名和key上添加了索引。 当我试图导入Article类型节点和Author类型节点之间的关系时,问题就出现了。 有办法用Cypher做到这一点吗?

  • 有没有用Cypher编写递归查询的方法?我必须遍历从一组节点(带有标签)到另一组节点的所有路径。该图是定向的,并且有多个路径,如 递归查询必须概述此伪代码 这个问题的解决方案不一定是递归的。任何其他解决方案都将受到赞赏。 编辑:在查询开始之前,根据需要将设置为一些值。并且图中的所有其他节点(例如)都有它们的。 有多个定向路径,如。对于这些路径中的每个节点(m,n,o,..)-[*]->y-->x-

  • Cypher对我来说似乎比Gremlin要清楚得多,总的来说,Neo4j的家伙似乎都在和Cypher一起。但是--如果Cypher与Gremlin相比是有限的--我真的想提前知道这一点。