How do you execute a Cypher statement and access the results?
Execute the Cypher statement
Select the correct method to create an executable Cypher statement.
final String cypher = """
MATCH (m:Movie {title: $title})<-[:ACTED_IN]-(p)
RETURN p.name AS actor
""";
final String title = "Toy Story";
var result = driver./*select:executableQuery(*/
cypher
)
.withParameters(Map.of("title", title))
.execute();-
❏ cypher(
-
✓ executableQuery(
-
❏ execute(
-
❏ verifyConnectivity(
Hint
The method to execute a Cypher query with the driver is the same one you used in the previous lesson - it starts with executable.
Solution
The correct answer is executableQuery(). This method creates an executable Cypher query which can be executed.
var result = driver.executableQuery(cypher).execute();Specify database for query
You want to run the query on the "movies" database. Select the correct option to specify the database.
final String cypher = """
MATCH (m:Movie {title: $title})<-[:ACTED_IN]-(p)
RETURN p.name AS actor
""";
final String title = "Toy Story";
var result = driver.executableQuery(cypher)
.withParameters(Map.of("title", title))
.withConfig(/*select:QueryConfig.builder().withDatabase("movies").build()*/)
.execute();-
✓ QueryConfig.builder().withDatabase("movies").build()
-
❏ SessionConfig.builder().withDatabase("movies").build()
-
❏ DatabaseConfig.builder().withDatabase("movies").build()
-
❏ withDatabase("movies")
Hint
Use the configuration class that works with executableQuery() to specify which database to query.
Solution
The correct answer is QueryConfig.builder().withDatabase("movies").build().
import org.neo4j.driver.QueryConfig;
var result = driver.executableQuery(cypher)
.withParameters(Map.of("title", title))
.withConfig(QueryConfig.builder().withDatabase("movies").build())
.execute();The QueryConfig class is used to configure query execution options, including the database name. Use QueryConfig.builder().withDatabase() to specify which database to query when executing a Cypher statement with executableQuery().
Print the actor names
Select the correct method to print the name of each actor.
final String cypher = """
MATCH (m:Movie {title: $title})<-[:ACTED_IN]-(p)
RETURN p.name AS actor
""";
final String title = "Toy Story";
var result = driver.executableQuery(
cypher
)
.withParameters(Map.of("title", title))
.execute();
var records = result.records();
records.forEach(r -> {
System.out.println(
/*select:r.get("actor")*/
);
});-
❏ records.get("p.name")
-
❏ records.get("actor")
-
❏ r.get("p.name")
-
✓ r.get("actor")
Hint
Review the RETURN clause in the Cypher statement.
cypher = """
MATCH (m:Movie {title: $title})<-[:ACTED_IN]-(p)
RETURN p.name AS actor
"""Solution
The name property of the p node is aliased as actor in the RETURN clause.
The get method of the Record object takes the alias as an argument.
records.forEach(r -> {
System.out.println(
r.get("actor")
);
});Lesson summary
Nice work! You learned how to execute a Cypher statement and access the results.
In the next lesson, you will review how to transform the results.