cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Java embedded graph in Neo4j community 4.1.1

Hi all ! I trying to visualize a java embedded graph in Neo4j community.

There is my code to generate the new.db I want to visualize:

/*
 * Licensed to Neo4j under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Neo4j licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.File;
import java.io.IOException;

import org.neo4j.dbms.api.DatabaseManagementService;
import org.neo4j.dbms.api.DatabaseManagementServiceBuilder;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;

public class Embedded3 {
	private static final File databaseDirectory = new File(
			"/home/nicolas/neo4j-community-4.1.1/data/databases/new.db");

	public String greeting;

	// tag::vars[]
	GraphDatabaseService graphDb;
	Node firstNode;
	Node secondNode;
	Relationship relationship;
	private DatabaseManagementService managementService;
	// end::vars[]

	// tag::createReltype[]
	private enum RelTypes implements RelationshipType {
		KNOWS
	}
	// end::createReltype[]

	public static void main(final String[] args) throws IOException {
		Embedded3 hello = new Embedded3();
		hello.createDb();
		// hello.removeData();
		hello.shutDown();
	}

	void createDb() throws IOException {
		// tag::startDb[]
		managementService = new DatabaseManagementServiceBuilder(databaseDirectory).build();
		graphDb = managementService.database("neo4j");
		registerShutdownHook(managementService);
		
		try (Transaction tx = graphDb.beginTx()) {

			// Database operations go here
			firstNode = tx.createNode();
			firstNode.setProperty("message", "Hello, ");
			secondNode = tx.createNode();
			secondNode.setProperty("message", "World!");

			relationship = firstNode.createRelationshipTo(secondNode, RelTypes.KNOWS);
			relationship.setProperty("message", "brave Neo4j ");
			// end::addData[]

			greeting = ((String) firstNode.getProperty("message")) + ((String) relationship.getProperty("message"))
					+ ((String) secondNode.getProperty("message"));

			// tag::transaction[]
			tx.commit();
		}
		
	}

	void shutDown() {
		System.out.println();
		System.out.println("Shutting down database ...");
		// tag::shutdownServer[]
		managementService.shutdown();
		// end::shutdownServer[]
	}

	// tag::shutdownHook[]
	private static void registerShutdownHook(final DatabaseManagementService managementService) {
		// 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() {
			@Override
			public void run() {
				managementService.shutdown();
			}
		});
	}
	// end::shutdownHook[]
}

After, I have changed the value of dbms.default_database to dbms.default_database=new.db in neo4j.conf.

When I start neo4j, the database is well implemented:
2X_5_54077c9019dfd5cd09da35f9222d7c48c76eec88.png

But when I run a simple query MATCH (n) return n there is no record in the database to show.

Then I successfully created some nodes from the neo4j browser. But it's impossible to read them with my Java code, the database looks empty.

There must be an error in my code but I am really stucked right now.
Thanks for your help.

Nicolas.

3 REPLIES 3

anthapu
Graph Fellow

It seems like you are creating the database named neo4j not "new.db"

Thanks for you answer.
I tried to change this name by "new.db" and "new" but I always have a "DatabaseNotFoundException".
The problem must come from here.

In the code example given by Neo4j https://github.com/neo4j/neo4j-documentation/blob/4.1/embedded-examples/src/main/java/org/neo4j/exam... the DEFAULT_DATABASE_NAME is set to "neo4j".
I double checked and "new.db" is created whereas I set graphDb = managementService.database("neo4j");
I have a DatabaseNotFoundException if not.