Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
01-06-2023 10:39 AM
Hi,
complete new in this topic. I got a project wich creates a Java Neo4j db. With the folder data, logs and sub folder database and transactions. These Project is using the 4.* Neo4j Libraries to store the data. With this mentioned dependencies, im searching for a possibility to get the connection to this stored db and get a Node from there.
Solved! Go to Solution.
01-09-2023 12:36 PM
Just a note, you would not use the driver to execute queries against your embedded server from within the same application. You would use the 'execute' method on a transaction. This is what you did in the first query.
I do use the driver in my test code when I want to test a custom procedure or function I built works properly when accessed remotely through a query, as it will be used in my application.
01-06-2023 11:52 AM
This reference should help:
https://neo4j.com/docs/java-reference/current/java-embedded/
the hello world section has example code showing you how to access the database.
https://neo4j.com/docs/java-reference/current/java-embedded/hello-world/
01-06-2023 12:12 PM
Hi @glilienfield,
i will try this approach tomorrow. At the moment i have some doubts regarding this hello world example. Because this examples is related to the newest Neo4j SW Version. And my stored Neo4j db is created with version 4.*.
01-06-2023 12:40 PM
I use version 4 of the Java API. The hello world looks good. You can also change the version of the document to the version you are during. There is a drop down menu within the icon at the top left.
01-06-2023 11:52 PM
Hi @glilienfield,
i forgot one question. Is it possible to open this stored db with the neo4j desktop version?
01-07-2023 10:24 AM - edited 01-07-2023 02:07 PM
check this out:
https://neo4j.com/docs/java-reference/current/java-embedded/bolt/
once you have enabled the bolt protocol and your application is running, you can create a ‘remote connection’ in neo4j desk or to connect to your embedded server.
01-07-2023 04:06 AM
Hi @glilienfield,
i tried three approaches to get the nodes from the embedded neo4j.
String db_path = System.getProperty("user.dir") + "/cooccsdatabase";
File database = new File(db_path);
managementService = new DatabaseManagementServiceBuilder(database).build();
graphDB = managementService.database(DEFAULT_DATABASE_NAME);
try ( Transaction tx = graphDB.beginTx();
Result result = tx.execute( "MATCH (n {name: 'Secure'}) RETURN n, n.name" ) )
{
while ( result.hasNext() )
{
Map<String,Object> row = result.next();
String rows = "";
for ( Entry<String, Object> column : row.entrySet() )
{
rows += column.getKey() + ": " + column.getValue() + "; ";
}
rows += "\n";
}
}
Results from the first approach are null.
With the second approach, i got an exception from the Driver.
Could not instrument class org/neo4j/driver/Driver: Unsupported class file major version 61.
String db_path = System.getProperty("user.dir") + "/cooccsdatabase";
File database = new File(db_path);
managementService = new DatabaseManagementServiceBuilder(database).build();
graphDB = managementService.database(DEFAULT_DATABASE_NAME);
try (Session session = driver.session()) {
return session.executeRead(tx -> {
List<String> names = new ArrayList<>();
//var result = tx.run("Match (n:Person{name: 'Tom Hanks'})-[r]->(m) Return m.title"); // Tom Hanks ist das gesuchte schlüssel wort
org.neo4j.driver.Result result = tx.run("MATCH (node:Person) WHERE node.name = 'Secure' RETURN node"); // Tom Hanks ist das gesuchte schlüssel wort
while (result.hasNext()) {
names.add(result.next().get(0).asString());
}
return names;
});
}
With the third one, i got the same exception.
Could not instrument class org/neo4j/driver/Driver: Unsupported class file major version 61.
String db_path = System.getProperty("user.dir") + "/cooccsdatabase";
File database = new File(db_path);
managementService = new DatabaseManagementServiceBuilder(database).build();
graphDB = managementService.database(DEFAULT_DATABASE_NAME);
try (Session session = driver.session()) {
return session.readTransaction(tx -> tx.run(
"optional match(n:Person{name: $Node}) " +
"return size(collect(distinct n)) > 0 as isExists", Map.of("person", Node))
.single().get("isExists").asBoolean());
}
01-07-2023 05:34 AM
Did you create the node you are looking for before your query? If not, use createNode method on the transaction to create a node.
I try to use the API as much as possible to retrieve/create nodes and traverse the graph. at this level you are working directly with the graph entities, i.e., nodes and relationships. I use execute only when executing complex cypher queries.
You can use the findNode method on the transaction to find a node directly. Then you can use the getRelationships method on the node to traverse graphs.
https://neo4j.com/docs/java-reference/5/javadocs/org/neo4j/graphdb/Transaction.html
https://neo4j.com/docs/java-reference/5/javadocs/org/neo4j/graphdb/Node.html
01-09-2023 12:36 PM
Just a note, you would not use the driver to execute queries against your embedded server from within the same application. You would use the 'execute' method on a transaction. This is what you did in the first query.
I do use the driver in my test code when I want to test a custom procedure or function I built works properly when accessed remotely through a query, as it will be used in my application.
All the sessions of the conference are now available online