In Neo4J, how to set the label as a parameter in a cypher query from Java? -
In Neo4J, how to set the label as a parameter in a cypher query from Java? -
i have problems parameter in cypher in neo4j java. run the database embedded.
the code should (graphdb.cypher goes straight executionengine)
hashmap<string, object> parameter = new hashmap<>(); parameter.put("thelabel1", "group"); parameter.put("therelation", "ismemberof"); parameter.put("thelabel2", "person"); graphdb.cypher("match (n1:{thelabel1})-[r:{therelation}]->(n2:{thelabel2}) homecoming n1, r, n2", parameter); but ends in exception
exception in thread "main" invalid input '{': expected whitespace or label name (line 1, column 11) "match (n1:{thelabel1})-[r:{therelation}]->(n2:{thelabel2}) homecoming n1, r, n2" the documentation (and tutorial) tells utilize { } cover parameters, used cypher json notation properties. @see http://docs.neo4j.org/chunked/milestone/tutorials-cypher-parameters-java.html
is there way solve issue rather building query string (or other template methods)
graphdb.cypher("match (n:" + labelname + ")-[r:" + relationname + "]->... this needed because target label can alter , want reuse code completly.
thanks in advance.
[[edited after getting (sigh) no answer]]
since form of parameter (2014.6) not supported, run little replacer right before sending query.
hashmap<string, object> parameter = new hashmap<>(); parameter.put("thelabel1", "group"); parameter.put("therelation", "ismemberof"); parameter.put("thelabel2", "person"); parameter.put("aname", "donald duck"); graphdb.cypher("match (n1:#thelabel1#)-[r:#therelation#]->(n2:#thelabel2#) n2.name = {aname} homecoming n1, r, n2", parameter); ... ... public static executionresult cypher(string query, map<string, object> params) { (string key : params.keyset()) { query = query.replaceall("#" + key + "#", string.valueof(params.get(key))); } homecoming params == null ? cypherengine.execute(query) : cypherengine.execute(query, params); } there can more readble
i afraid not supported @ moment.
and might same reason 1 explained in issue: https://github.com/neo4j/neo4j/pull/1542.
the thought behind parametrized queries re-use (cache) execution plans. if node label or relationship type varies, execution plan wouldn't same @ all, ruining usefulness of execution plan caching.
java neo4j cypher
Comments
Post a Comment