Home > Software Center, Structured Storage > Neo4j: the First Cup of Tea

Neo4j: the First Cup of Tea

The documentation for Neo4J is complete — spend several hours for those pages then you’ll learn a lot. It seems not necessary to write a guide for Neo4J repeatedly, but I want to make you start to use it in less than 30 minutes.

  1. Go to http://neo4j.org/download/ and download Neo4j. I choose advanced version with so said “monitoring capabilities” on the database.
  2. Unpack the package and import all JAR files in /lib/ to your Java project.
  3. Follow instructions here to write your first HelloWorld example. It is straightforward.
  4. It is essential to create index on nodes. Follow this tutorial to know how to create index. Just dig the code – it is easy to read.
  5. Here is Java API. You should at least know the following graph operations:

    Locate a node: given a key/value pair, find nodes with this key/value pair. When value is a unique identifier for key, this will help you locate the destination node you want to find.

    Node node = nodeIndex.get(KEY, "value").getSingle();

    Get neighbors of a node: the following code will return you all the outgoing neighbors of a node with edge type CITES.

    Traverser traverser = node.traverse(Order.BREADTH_FIRST, StopEvaluator.DEPTH_ONE,
    ReturnableEvaluator.ALL_BUT_START_NODE, RelTypes.CITES, Direction.OUTGOING);
    System.out.println(traverser.getAllNodes().size());

    Get all shortest paths between two nodes: the following code will show you all the shortest paths between startNode and endNode whth length smaller than 3.

    PathFinder finder = GraphAlgoFactory.shortestPath(Traversal.expanderForTypes
    (RelTypes.CITES, Direction.OUTGOING), maxLength);
    Iterator paths = finder.findAllPaths(start, end).iterator();
    while (paths.hasNext()) {
    	Path path = paths.next();
    	Iterator iterator = path.nodes().iterator();
    	while (iterator.hasNext()) {
    		Node node = iterator.next();
    		System.out.print(node.getProperty(NAME_KEY) + " ");
    	}
    	System.out.println();
    }

    Get all paths between two nodes:

    PathFinder finder = GraphAlgoFactory.allPaths(Traversal.expanderForAllTypes(),
     maxLength);
    Iterator paths = finder.findAllPaths(start, end).iterator();

    Get all simple paths (without cycles) between two nodes:

    PathFinder finder = GraphAlgoFactory.allSimplePaths(Traversal.
    expanderForAllTypes(), maxLength);
    Iterator paths = finder.findAllPaths(start, end).iterator();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: