简体   繁体   English

Neo4j通过Rest执行密码查询

[英]Neo4j Executing cypher query via rest

Good Morning, I set up a local Neo4j database and want to model a graph of maven dependencies. 早上好,我建立了本地Neo4j数据库,并希望对Maven依赖关系图进行建模。 When I execute the following statement via the webconsole, everything works fine: 当我通过网络控制台执行以下语句时,一切正常:

start root = node(1)
create unique root -[:ROOT]-> (n{groupId:'fancyStuff',artifactId:'somewhat', version:'1.4'})
return n

(note: rootnode is there for debugging purposes, will be replaced by actual structure later) So, here everything works fine, no matter of how much whitespaces I take or replacing ' with " (注意:rootnode用于调试目的,稍后将被实际结构替换)因此,无论我占用多少空格还是将'替换为“

In my java application i have the following function: 在我的Java应用程序中,我具有以下功能:

private static URI getOrCreate(Artifact artifact){
        String cypherUri = SERVER_ROOT_URI + "cypher";

        String cypherStatement="{\"query\" : \"start x  = node(1) " +
                "create unique x -[:ROOT]-> (artifact{groupId:\"" + artifact.getGroupID() +
                "\", artifactId:\"" + artifact.getArtifactID() +
                "\", version: \"" + artifact.getVersion() +
                "\"}) return artifact ,\"params\" : {}}";

        WebResource resource = Client.create()
                .resource( cypherUri );
        ClientResponse response = resource.accept( MediaType.APPLICATION_JSON_TYPE )
                .type( MediaType.APPLICATION_JSON_TYPE )
                .entity( cypherStatement )
                .post( ClientResponse.class );

        System.out.println( String.format( "POST to [%s], status code [%d]",
                cypherUri, response.getStatus() ) );

        response.close();
        return response.getLocation();
    }

so basically I post a json file looking like 所以基本上我发布了一个json文件,看起来像

{"query" : "start root = node(1) create unique root-[:ROOT]->(artifact{groupId:'{"query" : "start root = node(1) create unique root-[:ROOT]->(artifact{groupId:'lol',artifactId:'somewhat',version:'1.4'}) return artifact","params" : {}}

also no matter what whitespacing or "/' I use I get an http 500 error, saying the first - of the relationship -[:ROOT]-> is invalid. 同样,无论使用什么空格或“ /”,我都会看到一个HTTP 500错误,即关系的第一个-[:ROOT]->无效。

Posting new nodes directly via 通过直接发布新节点

final String nodeEntryPointUri = SERVER_ROOT_URI + "node";
WebResource resource = Client.create().resource( nodeEntryPointUri );
ClientResponse response = resource.accept( MediaType.APPLICATION_JSON_TYPE )
         .type( MediaType.APPLICATION_JSON_TYPE )
         .entity( /*some json*/)
         .post(ClientResponse.class);

(Disclaimer: I will move the params to the right place asap this version works ;) ) (免责声明:在此版本有效的情况下,我将把参数移到正确的位置;))

I could bet it's some totally trivial error, but I'm staring at this for over half a workday right now and none of my variations want to work. 我敢打赌这完全是个小错误,但是我现在盯着这个工作了半个多天,而且我的所有版本都不想工作。 Would be awesome if somebody knows an answer. 如果有人知道答案,那将会很棒。

Greetings, Florian Rohm 弗洛里安·罗门(Florian Rohm)的问候

The problem is you have invalid JSON. 问题是您的JSON无效。 You repeat query twice. 您重复query两次。 If you delete the part between stars, does it work? 如果删除星号之间的部分,是否有效?

**{"query" : 
    "start root = node(1) 
    create unique root-[:ROOT]->(artifact{groupId:'**
{"query" : "start root = node(1) 
  create unique root-[:ROOT]->(artifact{groupId:'lol',artifactId:'somewhat',version:'1.4'}) 
  return artifact",
 "params" : {}
}

Ok, i don't know what's different with this statement but this works (I tried the param split in the above code, too): 好的,我不知道这条语句有什么不同,但这行得通(我也尝试了上面代码中的param split):

String cypherUri = SERVER_ROOT_URI + "cypher";        
JSONObject jObject = new JSONObject();
        try {
            Map<String, String> params = new HashMap<String, String>();
            params.put("groupId", artifact.getGroupID());
            params.put("artifactId", artifact.getArtifactID());
            params.put("version", artifact.getVersion());
            String query = "start x  = node(1) create unique x-[:ROOT]->(n{groupId:{groupId},artifactId:{artifactId},version:{version} }) return n";
            jObject.put("query", query);
            jObject.put("params", params);
        } catch (Exception e) {
            e.printStackTrace();
        }

        WebResource resource = Client.create()
                .resource( cypherUri );
        ClientResponse response = resource.accept( MediaType.APPLICATION_JSON_TYPE )
                .type(MediaType.APPLICATION_JSON_TYPE)
                .entity(jObject.toString())
                .post(ClientResponse.class);

But anyways, the second attempt is nicer and I won't complain :D It just bugs me not to know what's going on there... 但是无论如何,第二次尝试更好,而且我不会抱怨:D它使我不知道那里发生了什么……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM