User Tools

Site Tools


technology:databases:neo4j:experiment_1

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:

 1:     public static void main( final String[] args )
 2:     {
 3:         GraphDatabaseService graphDb = new EmbeddedGraphDatabase( DB_PATH );
 4:         registerShutdownHook( graphDb );
 5:         // Encapsulate some operations in a transaction
 6:         Transaction tx = graphDb.beginTx();
 7:         try
 8:         {
 9:             Node firstNode = graphDb.createNode();
10:             firstNode.setProperty( NAME_KEY, "Hello" );
11:             Node secondNode = graphDb.createNode();
12:             secondNode.setProperty( NAME_KEY, "World" );
13:             firstNode.createRelationshipTo( secondNode,
14:                 ExampleRelationshipTypes.EXAMPLE );
15:             String greeting = firstNode.getProperty( NAME_KEY ) + " "
16:                 + secondNode.getProperty( NAME_KEY );
17:             System.out.println( greeting );
18:             firstNode.getSingleRelationship( ExampleRelationshipTypes.EXAMPLE,
19:                 Direction.OUTGOING ).delete();
20:             firstNode.delete();
21:             secondNode.delete();
22:             tx.success();
23:         }
24:         finally
25:         {
26:             tx.finish();
27:         }
28:         System.out.println( "Shutting down database ..." );
29:         graphDb.shutdown();
30:     }

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.

/home/cfreyer/public_html/data/pages/technology/databases/neo4j/experiment_1.txt · Last modified: 2010/07/31 18:28 by Chris Freyer