Neo4j Scala driver.
Features:
- Non-blocking
- Streaming
- Transactions
The goal of this driver is to allow you everything Neo4j transactional endpoint allows you to do, but in Scala.
libraryDependencies += "ru.dgolubets" %% "neo4s" % "0.1.1"
Create a single driver and connection instance per application.
val driver = NeoDriver()
val connection = driver.connection("localhost", 7476)
Run query.
connection.run(cypher"""MATCH (p: Person) -[:ACTED_IN]-> (m: Movie) RETURN p, m.title LIMIT 2""").map { result =>
result.data.flatMap(_.row).foreach {
case CypherRow(CyObject(person), CyString(title)) =>
println(s"${person("name").as[String]} acted in '$title'")
}
}
Neo4j encourages you to use parametrized queries, so it can cache query plans.
val year = 2008
val statementWithParameters = cypher"""MATCH (m: Movie) WHERE m.released > $year RETURN m"""
connection.run(cypher"""MATCH (p: Person) -[r:ACTED_IN]-> (m: Movie) RETURN p, r, m LIMIT 2""".withGraph).map { result =>
result.data.flatMap(_.graph).map { graph =>
graph.nodes.foreach { r =>
println(r)
}
graph.relationships.foreach { r =>
println(r)
}
}
}
connection.run(cypher"""CREATE (m: Movie{ title: "Unknown", released: "2007"})""".withStats).map { result =>
result.stats.map { stats =>
println(s"Created ${stats.nodesCreated} nodes.")
}
}
val t = connection.beginTransaction()
t.run(cypher"""CREATE (m: Movie{ title: "Unknown", released: "2007"})""".withStats).map { _ =>
t.commit()
}
Neo4j results can be processed as a stream.
import driver.materializer
connection.stream(cypher"""MATCH (p: Person) -[:ACTED_IN]-> (m: Movie) RETURN p, m.title""").runForeach {
case CypherStreamResultData(_, Some(row), _) => println(row)
case CypherStreamError(code, message) =>
case _ =>
}