Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active June 19, 2024 22:07
Show Gist options
  • Save dacr/38d2b7bda4d2f451e840a8ed51de96a7 to your computer and use it in GitHub Desktop.
Save dacr/38d2b7bda4d2f451e840a8ed51de96a7 to your computer and use it in GitHub Desktop.
neo4j cypher queries - simple embedded test example / published by https://github.com/dacr/code-examples-manager #d4ceb08f-d6fe-4853-8ce2-0a0994076590/3fc59c42e429a1414e9afaae856e9a7e1787df92
// summary : neo4j cypher queries - simple embedded test example
// 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 : d4ceb08f-d6fe-4853-8ce2-0a0994076590
// created-on : 2023-06-24T09:24:39+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.{GraphDatabase, AuthTokens}
import scala.util.Using
import scala.util.chaining.*
val fixture =
"""
|CREATE (:Person {name: 'Jane'})
|CREATE (:Person {name: 'Joe'})
|""".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 query = "MATCH (n) RETURN count(n) AS count"
val result = session.run(query)
val count = result.single().get("count").asInt()
println(s"'$query' : count = $count")
}.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