Last active
June 28, 2024 23:14
-
-
Save dacr/557f4f8e9469899598df0b08e7baec2e to your computer and use it in GitHub Desktop.
neo4j cypher queries in java using scala-cli - simple embedded test example / published by https://github.com/dacr/code-examples-manager #610b5cfa-3b78-41b2-ace0-81b0b0bb1792/2feda5b3330de9a104ea7afe917168442a9f9bb6
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 in java using scala-cli - simple embedded test example | |
// keywords : java, 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 : 610b5cfa-3b78-41b2-ace0-81b0b0bb1792 | |
// created-on : 2024-06-19T13:47:53+02:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// requirements : install https://scala-cli.virtuslab.org/ to get the scala-cli repl command | |
// --------------------- | |
//> using dep org.neo4j.test:neo4j-harness:5.20.0 | |
//> using dep org.neo4j.driver:neo4j-java-driver:5.21.0 | |
//DEPS org.neo4j.test:neo4j-harness:5.20.0 // for compatibility with jbang (https://www.jbang.dev/) | |
//DEPS org.neo4j.driver:neo4j-java-driver:5.21.0 // for compatibility with jbang (https://www.jbang.dev/) | |
// --------------------- | |
import org.neo4j.driver.*; | |
import org.neo4j.harness.Neo4jBuilders; | |
public class Neo4jCypherRawHello { | |
public static void main(String[] args) { | |
var fixture = """ | |
CREATE (:Person {name: 'Jane'}) | |
CREATE (:Person {name: 'Joe'}) | |
"""; | |
var builder = | |
Neo4jBuilders | |
.newInProcessBuilder() | |
.withFixture(fixture); | |
try (var embedded = builder.build()) { | |
try (var driver = GraphDatabase.driver(embedded.boltURI(), AuthTokens.none())) { | |
try (var session = driver.session()) { | |
var query = "MATCH (n) RETURN count(n) AS count"; | |
var result = session.run(query); | |
var count = result.single().get("count").asInt(); | |
System.out.println("'%s' : count = %d".formatted(query, count)); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment