Created
January 10, 2019 22:58
-
-
Save aleqsio/0bd61ebbe90a48c13a5f605b6559cf28 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
package pl.edu.agh.ki.bd2; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import org.neo4j.driver.v1.*; | |
import static org.neo4j.driver.v1.Values.parameters; | |
public final class GraphDatabase { | |
public static GraphDatabase createDatabase() { | |
return new GraphDatabase(); | |
} | |
Driver driver; | |
private GraphDatabase() { | |
driver = org.neo4j.driver.v1.GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "alek")); | |
registerShutdownHook(); | |
} | |
private void registerShutdownHook() { | |
// Registers a shutdown hook for the Neo4j instance so that it | |
// shuts down nicely when the VM exits (even if you "Ctrl-C" the | |
// running application). | |
Runtime.getRuntime().addShutdownHook(new Thread(() -> driver.close())); | |
} | |
public String runCypher(final String cypher) { | |
try (Session session = driver.session()) { | |
String txres = session.writeTransaction(tx -> { | |
StatementResult result = tx.run(cypher); | |
List<String> records = new ArrayList<>(); | |
result.forEachRemaining(x -> records.add(x.values().stream().map(y ->y.asNode().asMap().toString()).collect(Collectors.joining()))); | |
return records.toString(); | |
}); | |
return txres; | |
} catch (Exception e) { | |
return e.toString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment