Basic usage

Hello world

Create and access nodes and relationships.

To begin with, we define the relationship types we want to use:

    private static enum ExampleRelationshipTypes implements RelationshipType
    {
        EXAMPLE
    }

The next step is to start the database server:

        GraphDatabaseService graphDb = new EmbeddedGraphDatabase( DB_PATH );
        registerShutdownHook( graphDb );

As seen, we register a shutdown hook that will make sure the database shuts down when the JVM exits.

Now it's time to interact with the database:

        // Encapsulate operations in a transaction
        Transaction tx = graphDb.beginTx();
        try
        {
            Node firstNode = graphDb.createNode();
            firstNode.setProperty( NAME_KEY, "Hello" );
            Node secondNode = graphDb.createNode();
            secondNode.setProperty( NAME_KEY, "World" );

            firstNode.createRelationshipTo( secondNode,
                ExampleRelationshipTypes.EXAMPLE );

            String greeting = firstNode.getProperty( NAME_KEY ) + " "
                + secondNode.getProperty( NAME_KEY );
            System.out.println( greeting );

At this point this is how the database looks:

Node space view

In this case we'll remove the data before committing:

            // let's remove the data before committing
            firstNode.getSingleRelationship( ExampleRelationshipTypes.EXAMPLE,
                    Direction.OUTGOING ).delete();
            firstNode.delete();
            secondNode.delete();

            tx.success();
        }
        finally
        {
            tx.finish();
        }

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

        graphDb.shutdown();

Full source code: EmbeddedNeo4j