80685

Error in inserting a string parameter to a cypher query in a java code

Question:

I want to insert a string parameter to a cypher query in Java. Below is the code I used and I have a person node named 'piyumi' and I want to make relationship with an activity node. The name of the activity node is 'walking'. When I execute the code I get http status code 400. Can anyone help me to modify the cypher query so that I can insert the string variable s without error.

import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; import javax.ws.rs.core.MediaType; public class NeoAPIClass { private final String baseUri = "http://localhost:7474/db/data/cypher"; private final String user = "neo4j"; private final String password = "12345"; public static void main(String args[]) throws JSONException { NeoAPIClass n=new NeoAPIClass(); n.runQuery(); } void runQuery() throws JSONException { Client client = Client.create(); client.addFilter(new HTTPBasicAuthFilter(user, password)); WebResource cypherResource = client.resource(baseUri); String s="walking"; JSONObject obj = new JSONObject(); obj.put("query", "Match(piyumi : Person{name:\"Piyumi\"}) create (piyumi)-[:doing{timestamp:4789}]->(n:activity) WHERE n.name=s"); String st = obj.toString(); ClientResponse cypherResponse = cypherResource.accept(MediaType.APPLICATION_JSON) .type(MediaType.APPLICATION_JSON_TYPE).entity(st).post(ClientResponse.class); System.out.println("Output from Server .... "+ cypherResponse.getStatus()); } }

Answer1:

It is impossible to combine the create and where. You need first match person and activity, and then create a relationship.

And also recommend to simplify the transfer parameters in a separate part of the request (<a href="https://neo4j.com/docs/rest-docs/current/#rest-api-use-parameters" rel="nofollow">https://neo4j.com/docs/rest-docs/current/#rest-api-use-parameters</a>):

JSONObject request = new JSONObject(); JSONObject params = new JSONObject(); String query = "MATCH (P:Person { name:{personName} }) \n"; query = query + "MATCH (A:activity { name:{activityName} }) \n"; query = query + "CREATE (P)-[rel:doing { timestamp:{activityTimestamp} }]->(A) \n"; query = query + "RETURN P, A, rel"; request.put("query", query); params.put("personName", "Piyumi"); params.put("activityName", "walking"); params.put("activityTimestamp", 4789); request.put("params", params); ClientResponse cypherResponse = cypherResource.accept(MediaType.APPLICATION_JSON) .type(MediaType.APPLICATION_JSON_TYPE) .entity(request.toString()) .post(ClientResponse.class); System.out.println("Response: " + cypherResponse.getEntity(String.class));

Answer2:

Your problem is that the cypher server can't see the variable s from your Java code. You need to pass a cypher query that has the String itself, rather than the name of a Java variable.

You could change the line with obj.put to this.

obj.put("query", "Match(piyumi : Person{name:\"Piyumi\"}) create (piyumi)-[:doing{timestamp:4789}]->(n:activity) WHERE n.name=\"" + s + "\"");

Answer3:

Problem occurs when you try to retrieve existing node. So, go to <a href="http://localhost:7474/browser/" rel="nofollow">http://localhost:7474/browser/</a> and execute below query,

MATCH (n:activity) WHERE n.name="walking" CREATE (piyumi:Person{name:"Piyumi"}) - [r1:doing{timestamp:4789}]-> (n)

If it works fine, then retrieve it. MATCH (piyumi:Person)-[r1:doing]->(n:activity) RETURN r1

So your query code should looks like this,

"MATCH (n:activity)\nWHERE n.name=\"walking\"\nCREATE (piyumi:Person{name:\"Piyumi\"}) - [r1:doing{timestamp:4789}]-> (n)"

Recommend

  • Tomcat with Rest - HTTP Status 500 - Servlet execution threw an exception
  • 404 response on simple RESTeasy example with Tomcat
  • File download through ajax
  • How to insert alt tags dynamically in jsp
  • Is it possible to use an http url as your source location of a Source Filter in DirectShow .Net?
  • Skipping certain files/classes when using asdoc
  • How do I bind multiple properties in an Android layout element
  • Unzip archive in .Net CORE 1.0
  • Telegram bot API - Inline bot getting Error 400 while trying to answer inline query
  • Select value from xtype selection type checkbox CQ5
  • Get a trait object reference from a vector
  • Javascript CORS - No 'Access-Control-Allow-Origin' header is present
  • Exception creating JSON with LINQ
  • Linq Merge lists
  • Angular2 Response for preflight is invalid (redirect) from some GET requests
  • Angular2 component view does not update on value change via method
  • CakePHP ACL tutorial initDB function warnings
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • chrome.tabs.executeScript only fires when the Developer Console is open
  • Play WS (2.2.1): post/put large request
  • how to adjust image in a panel in Java swing?
  • Different response to non-authenticated users and AJAX calls
  • Possible to stop flickering java tooltip in heavyweight mode?
  • output of program is not same as passed argument
  • Javascript + PHP Encryption with pidCrypt
  • Display Images one by one with next and previous functionality
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Cannot Parse HTML Data Using Android / JSOUP
  • SetUp method failed while running tests from teamcity
  • JTable with a ScrollPane misbehaving
  • Understanding cpu registers
  • Getting Messege Twice Using IMvxMessenger
  • Java static initializers and reflection
  • Is it possible to post an object from jquery to bottle.py?
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?