Skip to content

Instantly share code, notes, and snippets.

@dacr
Created June 19, 2024 22:07
Show Gist options
  • Save dacr/450382a693f5a82213e5c9e2bce1e2b5 to your computer and use it in GitHub Desktop.
Save dacr/450382a693f5a82213e5c9e2bce1e2b5 to your computer and use it in GitHub Desktop.
neo4j cypher queries - query parameters / published by https://github.com/dacr/code-examples-manager #9b544bf0-8bbc-4058-9992-1e3a35d519c1/f62477252d05decd6d225e33d962af91108a803a
// summary : neo4j cypher queries - query parameters
// keywords : scala, neo4j, cypher, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 9b544bf0-8bbc-4058-9992-1e3a35d519c1
// created-on : 2024-06-19T17:23:21+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.4.2"
//> using dep "org.neo4j.test:neo4j-harness:5.20.0"
//> using dep "org.neo4j.driver:neo4j-java-driver:5.21.0"
// ---------------------
import org.neo4j.driver.{AuthTokens, GraphDatabase, Result}
import scala.util.Using
import scala.util.chaining.*
import scala.jdk.CollectionConverters.*
val fixture =
"""CREATE ( jane:Person {firstName: 'DAISY', lastName: 'DUCK', age: 32} )
|CREATE ( joe:Person {firstName: 'JOE', lastName: 'DALTON', age: 42} )
|CREATE ( james:Person {firstName: 'JOHN', lastName: 'CONNORS', age: 64} )
|CREATE ( albert:Person {firstName: 'ALBERT', lastName: 'EINSTEIN', age: 24} )
|""".stripMargin
val builder =
org.neo4j.harness.Neo4jBuilders
.newInProcessBuilder()
.withFixture(fixture)
Using(builder.build()) { embedded =>
Using(GraphDatabase.driver(embedded.boltURI(), AuthTokens.none())) { driver =>
Using(driver.session()) { session =>
// ------------------------------------------------
val parameterizedQuery =
"""MATCH (n)
|WHERE n.firstName = $firstName AND n.lastName = $lastName
|RETURN n.age AS age
|""".stripMargin
val parameters1 = Map("firstName" -> "JOE", "lastName" -> "DALTON")
val parameters2 = Map("firstName" -> "JOHN", "lastName" -> "CONNORS")
val response1:Result = session.run(parameterizedQuery, parameters1.asJava)
val joeAge = response1.single().get("age")
val response2:Result = session.run(parameterizedQuery, parameters2.asJava)
val johnAge = response2.single().get("age")
println(s"joe age is $joeAge and john age is $johnAge")
// ------------------------------------------------
}.tap(r => println(r))
}.tap(r => println(r))
}.tap(r => println(r))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment