Get Nodes from Embedded Neo4j

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.

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/

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.*.

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.

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.

Doktor_DOOM_0-1673092474757.png

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());
		    }

Hi @glilienfield,

i forgot one question. Is it possible to open this stored db with the neo4j desktop version?

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.

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

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.

Hi @glilienfield,

this find method was a well solution. Thanks