Reputation: 3051
The idea behind parametrized queries is to re-use (cache) execution plans.
If a node label or a relationship type do not vary, the execution plan would be the same at all, thus benefits can be achieved of execution plan caching.
Currently, I'm my complete Cypher Query is built using Java String Building. Instead of creating the entire Cypher Query using String building I want to pass the values of the Properties as Parameter Values along with Property Names as Parameters or not. I need sample code, guidance for the same.
import org.neo4j.driver.v1.AuthTokens;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.Session;
public class ForStackoverflowQuestion {
public static void main(String[] args) {
Driver driver = GraphDatabase.driver(
"bolt://localhost:7687", AuthTokens.basic("neo4j", "12345"));
Session session = driver.session();
String Node1 = "Software_Engineer";
String Node2 = "Programming_Language";
String relationBetweenNode1andNode2 = "LEARNS";
String PersonNameAttribute = "name";
String PersonNameValue = "Jaykant";
String ProgrammingLanguageAttribute = "version";
String ProgrammingLanguageValue = "Neo4j";
String t = "MERGE(n1:"+Node1+"{"+PersonNameAttribute+":\""+PersonNameValue+"\"})"+"-[:"+relationBetweenNode1andNode2+"]->(n2:" + Node2 +" {"+ProgrammingLanguageAttribute+":'"+ProgrammingLanguageValue+"'})";
System.out.println(t);
session.run(t);
session.close();
driver.close();
}
}
I understand that my above code is not using Parameterized Cypher Query; so it will not generate any Query Plan in neo4j.
In order to use and benefit from the Query Plan, I need to use the Parametrized Query.
If not Node1, Node2, and relationBetweenNode1andNode2 as Parameters then at least following values can be passed as parameters.
PersonNameAttribute = "name";
PersonNameValue = "Jaykant";
ProgrammingLanguageAttribute = "version";
ProgrammingLanguageValue = "Neo4j";
Any sample code? Tutorial?
Upvotes: 0
Views: 942
Reputation: 4052
You can pass parameters along with query in session.run()
method.
Ex.
session.run(query, parameters)
Parameters should be a Map.
HashMap<String, Object> parameters = new HashMap<String, Object>();
parameters.put("PersonNameValue", "Jaykant");
parameters.put("ProgrammingLanguageValue", "Neo4j");
Query can be modified as:
String t = "MERGE (n1:"+Node1+"{"+PersonNameAttribute+":{PersonNameValue}})"+"-[:"+relationBetweenNode1andNode2+"]->(n2:" + Node2 +" {"+ProgrammingLanguageAttribute+": {ProgrammingLanguageValue}})";
Finally run
statement:
session.run(t, parameters);
Upvotes: 1