2017-03-20 15 views
0

ノードとノード間の関係を作成しようとしています。以下は私が使ったコードです。Neo4J REST APIのcypher-query MERGE構文を使用する

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); 
} 

私はノードが存在するかどうかを確認しようとしていますが、そうでなければ、それらの間の関係だけを作成します。

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

答えて

1

それは私がこのSO回答に基づいて解決策を見つけ、非常に単純でした:Check whether a node exists, if not create

String query = "MERGE (n:color {color: {name} })MERGE (m:value {value: {name} }) MERGE (n)-[:hasVALUE]->(m)"; 
関連する問題