Hi all ! I trying to visualize a java embedded graph, generated with your code.
/*
* 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[]
}
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.
Can you give me more explanation for this line please ? : graphDb = managementService.database("neo4j");
Why do I have an error if I change "neo4" with an other string ?
I double checked and "new.db" is created whereas I set graphDb = managementService.database("neo4j");
I have a DatabaseNotFoundException if not.
Thanks for your help.
Nicolas.