Created
July 27, 2020 16:00
-
-
Save michael-simons/33f75e63e0a75c13f54bdaed5f2cb5fb to your computer and use it in GitHub Desktop.
This file contains 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
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