Created
June 19, 2024 22:08
-
-
Save dacr/cc621d74e7b6c65133c7213124bed1b5 to your computer and use it in GitHub Desktop.
neo4j cypher queries - node name and label - relation name and type - records content / published by https://github.com/dacr/code-examples-manager #50657667-fdcf-4b0c-8562-aebed139c5c5/abd4396c122b2bd6c5be585b55f1819dab101520
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : neo4j cypher queries - node name and label - relation name and type - records content | |
// 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 : 50657667-fdcf-4b0c-8562-aebed139c5c5 | |
// created-on : 2024-06-19T14:12:31+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.* | |
import scala.jdk.CollectionConverters.* | |
val fixture = | |
"""// ---- NODES | |
|CREATE (jane:Person {name: 'JANE', age: 32}) | |
|CREATE (joe:Person {name: 'JOE', age: 42}) | |
|CREATE (james:User {name: 'JAMES', age: 64}) | |
| | |
|// ---- EDGES | |
|CREATE (jane)-[:KNOWS {since:5}]->(joe) | |
|CREATE (joe)-[:LIKES {level:32}]->(jane) | |
|""".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)-[r]->() RETURN n,r" | |
val response = session.run(query) | |
val results = response.list().asScala.map(r => (r.get("n").asNode(), r.get("r").asRelationship())) | |
results.foreach { (node, relation) => | |
println("---------------------------------") | |
println(s"node labels = ${node.labels().asScala.mkString(",")}") | |
println(s"node elementId = ${node.elementId()}") | |
println(s"node fields = ${node.asMap().asScala}") | |
println(s"relationship type = ${relation.`type`()}") | |
println(s"relationship elementId = ${relation.elementId()}") | |
println(s"relationship startNodeElementId = ${relation.startNodeElementId()}") | |
println(s"relationship endNodeElementId = ${relation.endNodeElementId()}") | |
println(s"relationship fields = ${relation.asMap().asScala}") | |
} | |
}.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