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

懵懂的女人 提交于 2020-02-07 07:09:04

问题


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


    }

}

回答1:


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 (https://neo4j.com/docs/rest-docs/current/#rest-api-use-parameters):

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



回答2:


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 + "\"");



回答3:


Problem occurs when you try to retrieve existing node. So, go to http://localhost:7474/browser/ 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)"



来源:https://stackoverflow.com/questions/41646451/error-in-inserting-a-string-parameter-to-a-cypher-query-in-a-java-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!