Examples of openCypher parameterized queries
Neptune supports parameterized openCypher queries. This lets you use the same query structure multiple times with different arguments. Since the query structure doesn't change, Neptune can cache its abstract syntax tree (AST) rather than having to parse it multiple times.
Example of an openCypher parameterized query using the HTTPS endpoint
Below is an example of using a parameterized query with the Neptune openCypher HTTPS endpoint. The query is:
MATCH (n {name: $name, age: $age}) RETURN n
The parameters are defined as follows:
parameters={"name": "john", "age": 20}
You can submit the parameterized query like this:
In these examples, replace your-neptune-endpoint with your
cluster endpoint and port with the Neptune port (the default
is 8182).
The plain curl form works only when you disable IAM database
authentication on your cluster.
When you enable IAM database authentication, Neptune rejects unsigned requests.
Sign each request with Amazon Signature Version 4. You can use a tool such as the
awscurl command in the preceding example.
For more information about signing requests with Amazon Signature Version 4, see Connecting with IAM authentication.
Examples of openCypher parameterized queries using Bolt
Here is a Python example of an openCypher parameterized query using the Bolt protocol:
from neo4j import GraphDatabase uri = "bolt://[neptune-endpoint-url]:8182" driver = GraphDatabase.driver(uri, auth=("", "")) def match_name_and_age(tx, name, age): # Parameterized Query tx.run("MATCH (n {name: $name, age: $age}) RETURN n", name=name, age=age) with driver.session() as session: # Parameters session.read_transaction(match_name_and_age, "john", 20) driver.close()
Here is a Java example of an openCypher parameterized query using the Bolt protocol:
Driver driver = GraphDatabase.driver("bolt+s://(your cluster endpoint URL):8182"); HashMap<String, Object> parameters = new HashMap<>(); parameters.put("name", "john"); parameters.put("age", 20); String queryString = "MATCH (n {name: $name, age: $age}) RETURN n"; Result result = driver.session().run(queryString, parameters);