3.2. Hello world

This section describes how to create and access nodes and relationships.

For information on project setup, see Section 3.1, “Including Neo4j in your project”.

A Neo4j graph consists of:

All relationships have a type. For example, if the graph represents a social network, a relationship type could be KNOWS. If a relationship of the type KNOWS connects two nodes, that probably represents two people that know each other. A lot of the semantics (that is the meaning) of a graph is encoded in the relationship types of the application. And although relationships are directed they are equally well traversed regardless of which direction they are traversed.

The source code of this example is found here: EmbeddedNeo4j.java

3.2.1. Preparing the database

Relationship types can be created by using an enum. In this example we only need a single relationship type. This is how to define it:

    private enum RelTypes implements RelationshipType

We also prepare some variables to use:

    GraphDatabaseService graphDb;
    Node firstNode;
    Node secondNode;
    Relationship relationship;

The next step is to start the database server. Note that if the directory given for the database does not already exist, it will be created.

        graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( databaseDirectory );
        registerShutdownHook( graphDb );

Note that starting a database server is an expensive operation, so do not start up a new instance every time you need to interact with the database! The instance can be shared by multiple threads. Transactions are thread confined.

As seen, we register a shutdown hook that will make sure the database shuts down when the JVM exits. Now it is time to interact with the database.

3.2.2. Wrapping operations in a transaction

All operations have to be performed in a transaction. This is a conscious design decision, since we believe transaction demarcation to be an important part of working with a real enterprise database. The example below illustrates transaction handling in Neo4j:

        try ( Transaction tx = graphDb.beginTx() )
            // Database operations go here

For more information on transactions, see Chapter 5, Transaction management and Java API for Transaction.

For brevity, we do not spell out wrapping of operations in a transaction throughout the manual.

3.2.3. Creating a small graph

Now, we can create a few nodes. The API is very intuitive. Feel free to have a look at the Neo4j Javadocs. They are included in the distribution, as well. Here is how to create a small graph consisting of two nodes, connected with one relationship and some properties:

            firstNode = graphDb.createNode();
            firstNode.setProperty( "message", "Hello, " );
            secondNode = graphDb.createNode();
            secondNode.setProperty( "message", "World!" );

            relationship = firstNode.createRelationshipTo( secondNode, RelTypes.KNOWS );
            relationship.setProperty( "message", "brave Neo4j " );

We now have a graph that looks like this:

Figure 3.1. Hello World Graph

3.2.4. Printing the result

After we have created our graph, we can read from it and print the result.

            System.out.print( firstNode.getProperty( "message" ) );
            System.out.print( relationship.getProperty( "message" ) );
            System.out.print( secondNode.getProperty( "message" ) );

Which will output:

Hello, brave Neo4j World!

3.2.5. Removing the data

In this case we will remove the data before committing:

            // let's remove the data
            firstNode.getSingleRelationship( RelTypes.KNOWS, Direction.OUTGOING ).delete();

Note that deleting a node which still has relationships when the transaction commits will fail. This is to make sure relationships always have a start node and an end node.

3.2.6. Shutting down the database server

Finally, shut down the database server when the application finishes: