简体   繁体   中英

Neo4j Executing cypher query via rest

Good Morning, I set up a local Neo4j database and want to model a graph of maven dependencies. 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 "

In my java application i have the following function:

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

{"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.

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

The problem is you have invalid JSON. You repeat query twice. 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):

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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