Skip to content

Instantly share code, notes, and snippets.

@michael-simons
Created July 27, 2020 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michael-simons/33f75e63e0a75c13f54bdaed5f2cb5fb to your computer and use it in GitHub Desktop.
Save michael-simons/33f75e63e0a75c13f54bdaed5f2cb5fb to your computer and use it in GitHub Desktop.
public static void main(String... a) throws Exception {
var driver = GraphDatabase.driver(
"bolt://localhost:7687",
AuthTokens.basic("neo4j", "secret"),
Config.builder().withLogging(Logging.console(Level.INFO)).build()
);
System.out.println("\nImperative transaction functions\n---");
Function<Record, String> getNameFromRecord = record -> record.get("name").asString();
try (var session = driver.session()) {
var names = session.readTransaction(
tx -> tx
.run("MATCH (n:Artist) RETURN n.name AS name")
.stream().map(getNameFromRecord)
.collect(toList())
);
names.forEach(System.out::println);
}
// Reactive transaction functions
System.out.println("\nReactive with Project reactor\n---");
RxTransactionWork<Publisher<Record>> txFunction =
tx -> tx.run("MATCH (n:Artist) RETURN n.name AS name").records();
Flux.usingWhen(
Mono.fromSupplier(driver::rxSession),
session -> session.readTransaction(txFunction),
RxSession::close
)
.map(getNameFromRecord)
.doOnNext(System.out::println)
.blockLast(); // Don't block at home
System.out.println("\nReactive with SmallRye Mutiny\n---");
var reactiveSession = driver.rxSession();
Multi.createFrom()
.publisher(reactiveSession.readTransaction(txFunction))
.map(getNameFromRecord)
.onCompletion().invoke(reactiveSession::close)
.subscribe()
.asStream()
.forEach(System.out::println);
driver.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment