0
votes

I am trying to create nodes and relationships between them. Below is the code which I have used.

String cypherUri = "http://localhost:7474/" + "cypher";        
JSONObject Neo4jQueryJSON = new JSONObject();
HashMap<String, String> params = new HashMap<String, String>();

params.put("value","H090");
params.put("color","red"); 

String query = "CREATE (n:color {color: {name} }), (m:value {value: {name} }), (n)-[:hasVALUE]->(m)";
Neo4jQueryJSON.put("query", query);
Neo4jQueryJSON.put("params", params);                   


WebResource resource = Client.create().resource( cypherUri );
ClientResponse response =
                   resource.accept(MediaType.APPLICATION_JSON_TYPE)
                           .type(MediaType.APPLICATION_JSON_TYPE)                               
                           .header("X-Stream","true")
                           .post(ClientResponse.class, Neo4jQueryJSON.toJSONString());  
           String result = response.getEntity(String.class);               
           response.close();
           int status = response.getStatus();      

           System.out.printf("POST %s %nstatus code [%d] %nresult %s%n", Neo4jQueryJSON, status, result);
 } 

I am trying to check is the node exists, if-not, only then create one aswell as the relationship between them.

String query = "CREATE (n:color {color: {name} }), (m:value {value: {name} }), (n)-[:hasVALUE]->(m)";
1

1 Answers

1
votes

It was pretty simple, I found the solution based on this SO answer: Check whether a node exists, if not create

   String query = "MERGE (n:color {color: {name} })MERGE (m:value {value: {name} }) MERGE (n)-[:hasVALUE]->(m)";