Skip to content

Experiment #1

I decided to download the Neo4J APOC Bundle, version 1.1. (APOC = A Package Of Components). It comes with:

  • the database
  • an index utility, so you can look up nodes by their attribute values
  • a unix-like shell, so you can navigate and alter the database with ls, cd, etc.
  • a remote database connector (similar to a JDBC driver jar?)
  • a database backup tool
  • a package of graph algorithms

But first things first. I wanted to start small: start the DB, do a few operations, and shut down the database. Luckily Neo4J provides sample code to do just that:

    public static void main( final String[] args )
    {
        GraphDatabaseService graphDb = new EmbeddedGraphDatabase( DB_PATH );
        registerShutdownHook( graphDb );
        // Encapsulate some 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 );
            firstNode.getSingleRelationship( ExampleRelationshipTypes.EXAMPLE,
                Direction.OUTGOING ).delete();
            firstNode.delete();
            secondNode.delete();
            tx.success();
        }
        finally
        {
            tx.finish();
        }
        System.out.println( "Shutting down database ..." );
        graphDb.shutdown();
    }

The code in lines 9-22 is extremely compact. You can see that it creates two nodes with attributes, creates a relationship between them, queries and prints them, then deletes them. I couldn't believe it...14 lines of code. Obviously a real app would need more code, but I've never been able to do something this compact with JDBC.

First impression: Wow. 5 minutes from download to complete database interaction. Hoping my next experiment will be equally impressive.