Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to a locally installed neo4j server using Java

Tags:

java

neo4j

I'm new to Neo4J and couldn't find the answer to my question despite my hours of googling.

So far, I have been following the tutorials and now I have a basic understanding of how/when to use Neo4j. Now, I am about to start modifying my hello-world code and connect to a Neo4J server locally installed on my machine, accessible via http://127.0.0.1:7474.

Original connection (using an embedded database):

GraphDatabaseService gdb = new EmbeddedGraphDatabase("c:\\helloworld\\data\\graph.db");

The question is is there anyway to modify this line to connect to my "server" database in c:\neo4j\data\graph.db instead? The server is running currently as a windows service and I can view its database using the web admin tool. At this time, I am not interested in using the REST API since the server and the client app are running on the same machine.

I feel like I'm missing something obvious here...

like image 859
PFrank Avatar asked Feb 21 '12 22:02

PFrank


People also ask

How do I connect to a local Neo4j database?

Neo4j Browser is the easiest way to access a Neo4j database. To establish a connection, you enter the DBMS URL, the name of the database you want to connect, and the user credentials. You can also use the :server command to manage the connection to Neo4j. For more information, see Manage connection commands.

Does Neo4j support Java?

Neo4j 3.5. x supports Java 11 as runtime, however custom code should still be compiled against Java 8. As a best practice, it is recommended to maintain your infrastructure environment on supported components (Hypervisor, Operating System, Java Virtual Machine).


1 Answers

I couldn't find any example code on the wrapper so here is what I ended up doing.

EmbeddedGraphDatabase graphDb = new EmbeddedGraphDatabase("C:\\neo4j\\data\\graph.db");
WrappingNeoServerBootstrapper srv = new WrappingNeoServerBootstrapper(graphDb);
srv.start();
try {
    while (System.in.read() != 46) {
    // wait until we send a period (.) to kill the server
    }
} catch (IOException e) {}
srv.stop();

This will allow you to visit localhost:7474 and see the webadmin tool just like running the server but also continue with your usual Java code (write your own simple API using the input stream to read commands).

like image 93
styfle Avatar answered Sep 22 '22 02:09

styfle