PFrank
PFrank

Reputation: 323

How to connect to a locally installed neo4j server using Java

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

Upvotes: 17

Views: 16651

Answers (4)

F.O.O
F.O.O

Reputation: 4960

Ended using Jersey Client's HttpBasicAuthFilter

Your parameters should be something like:

            public static final String DATABASE_ENDPOINT = "http://localhost:7474/db/data";

            public static final String DATABASE_USERNAME = "neo4j";

            public static final String DATABASE_PASSWORD = "3c0a0a6ea1sdsdsdsdsdsdsdsdf2a94d";

        private String callRest(String query) {
                final String cypherUrl = ENDPOINT + "/cypher";
                Client c = Client.create();
                c.addFilter(new HTTPBasicAuthFilter(USERNAME, PASSWORD));
                WebResource resource = c.resource(cypherUrl);
                String request = "{\"query\":\"" + query + "\"}";
                ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
                        .type(MediaType.APPLICATION_JSON).entity(request)
                        .post(ClientResponse.class);
                String object = response.getEntity(String.class);
                response.close();
                return object;
        }

Latest Jersey client can be found by adding this to your mvn pom if its not already in your dependency tree.

            <dependency>
                <groupId>com.sun.jersey</groupId>
                <artifactId>jersey-client</artifactId>
                <version>1.18.3</version>
            </dependency>

See: https://stackoverflow.com/posts/28303766

Upvotes: 1

DNA
DNA

Reputation: 42586

The windows service exposes the REST interface.

The embedded interface is entirely different - you point it at the database file structure and then access it via Java method calls.

If you have both running at the same time, pointing at the same data, then bad things might happen (actually, I think it detects this and prevents it). So you probably need to stop the service and/or backup the data from this instance to another directory. Then edit your EmbeddedGraphDatabase constructor to point to this directory.

The manual describes the embedded Java interface, as you've probably seen. See also this section which briefly mentions the rather nice web-based management interface.

Upvotes: 3

praDeepu
praDeepu

Reputation: 21

I know its an old post, but still adding my answer. You can use Neo4jConnection. Sample Code

Neo4jConnection connect=null;
connect = new Driver(). connect(DB_URL, new Properties());
ResultSet resultSet=connect.createStatement().executeQuery("YOUR QUERY")

Upvotes: 1

styfle
styfle

Reputation: 24610

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

Upvotes: 5

Related Questions