Examples of openCypher parameterized queries - Amazon Neptune

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}

Using GET, you can submit the parameterized query like this:

curl -k \ "https://localhost:8182/openCypher?query=MATCH%20%28n%20%7Bname:\$name,age:\$age%7D%29%20RETURN%20n&parameters=%7B%22name%22:%22john%22,%22age%22:20%7D"

Alternatively, you can use POST:

curl -k \ https://localhost:8182/openCypher \ -d "query=MATCH (n {name: \$name, age: \$age}) RETURN n" \ -d "parameters={\"name\": \"john\", \"age\": 20}"

Or, using DIRECT POST:

curl -k \ -H "Content-Type: application/opencypher" \ "https://localhost:8182/openCypher?parameters=%7B%22name%22:%22john%22,%22age%22:20%7D" \ -d "MATCH (n {name: \$name, age: \$age}) RETURN n"

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