Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package ac.simons.neo4j.twitch;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;
import org.neo4j.configuration.connectors.BoltConnector;
import org.neo4j.configuration.helpers.SocketAddress;
import org.neo4j.dbms.api.DatabaseManagementServiceBuilder;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Transaction;
public class GraphApplication {
public static void main(String... a) throws IOException {
// This is the db itself, should be long lived
var graphDb = new DatabaseManagementServiceBuilder(Files.createTempDirectory("neo4j").toFile())
.setConfig(BoltConnector.enabled, true)
.setConfig(BoltConnector.listen_address, new SocketAddress("localhost", 7687))
.build();
// You could also use the graph database api and skip using bolt.
// The advante of using build also in an embedded scenario: You can switch to a server with ease.
// Same goes for the driver with the connection pool
// The session itself is short lived
try (
var driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.none());
var session = driver.session()
) {
session.writeTransaction(t -> t.run("CREATE (p:Person {name: 'Arnold Schwarzenegger'}) - [:ACTED_IN] -> (:Movie {title: 'The Terminator'})").consume().counters().nodesCreated());
var movies = session.readTransaction(GraphApplication::findMovieAndTheirActors);
movies.forEach(System.out::println);
}
graphDb.shutdown();
}
record Person(String name) {
}
record Movie(String title, List<Person>actedIn) {
}
static List<Movie> findMovieAndTheirActors(Transaction tx) {
var query = """
MATCH (m:Movie) <- [:ACTED_IN] - (p:Person)
WHERE m.title =~ $movieTitle
RETURN m.title AS title, collect(p.name) AS actors
""";
return tx.run(query, Map.of("movieTitle", ".*The.*")).list(r -> {
var actors = r.get("actors").asList(v -> new Person(v.asString()));
var movie = new Movie(r.get("title").asString(), actors);
return movie;
});
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ac.simons.neo4j</groupId>
<artifactId>neo4jtwitch</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
<neo4j.version>4.0.1</neo4j.version>
<neo4j-java-driver.version>4.0.1</neo4j-java-driver.version>
</properties>
<dependencies>
<!-- Embbeded instance -->
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>${neo4j.version}</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-bolt</artifactId>
<version>${neo4j.version}</version>
</dependency>
<!-- Driver (for connection) -->
<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>${neo4j-java-driver.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.