Getting Started In One Minute Guide

From Neo4j Wiki

Jump to: navigation, search

This is a compressed summary of the slightly more verbose Getting Started Guide. Please refer to the full guide if something here is unclear.

  • If you don't use Maven: Download the Neo4j release and add the Neo4j kernel and JTA jars to your classpath.
  • If you do use Maven: Check the Maven2 info here and add it to your pom.xml.
  • If you want some commonly used Neo4j components bundled right away, check out the Getting Started With Apoc guide before downloading/configuring anything.
public enum MyRelationshipTypes implements RelationshipType
{
    KNOWS
}
...
GraphDatabaseService graphDb = new EmbeddedGraphDatabase( "var/base" ); 
  • Create a small graph:
Node firstNode = graphDb.createNode();
Node secondNode = graphDb.createNode();
Relationship relationship = firstNode.createRelationshipTo( secondNode, MyRelationshipTypes.KNOWS );

firstNode.setProperty( "message", "Hello, " );
secondNode.setProperty( "message", "world!" );
relationship.setProperty( "message", "brave Neo4j " );
  • Recall that Neo4j is fully transactional, so wrap everything in transactions:
Transaction tx = graphDb.beginTx();
try
{
   // all Neo4j operations that work with the graph, for example:
   // Node firstNode = graphDb.createNode();
   // ... etc ...
   tx.success();
}
finally
{
   tx.finish();
   graphDb.shutdown();
}
  • Print the result and you're done:
System.out.print( firstNode.getProperty( "message" ) );
System.out.print( relationship.getProperty( "message" ) );
System.out.print( secondNode.getProperty( "message" ) );

Now, you can move on to more detailed information:

Finally, be sure to sign up on the mailing list.

Personal tools