"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects." (Robert A. Heinlein)

Tuesday 5 August 2014

Neo4j and Java: demos with an embedded Ne04j graph


After my first experience in installing Neo4j graph database I decided to continue my experiments by writing a little Java demo program. The scope of my program just to learn how to connect to a Neo4j embedded graph, to generate ,connect and query some hundreds of nodes. Neo4j site and the downloaded manual provide plenty of documentation about interfacing with Java, and the other supported languages.

Project set-up

Setting up a Java project is quit simple: just matter of including all Neo4j libraries jars, available in the 'lib' folder, in the project class-path. To make easier future projects set-up I prepared, in Netbeans, a custom library configuration.


Database startup and shutdown

In order to work with a Neo4J database it must be initialized by providing the path where datawill be saved:


since database startup is a relatively heavy operation documentation warmly suggests to execute this operation only at startup.
As all database operations are concluded, before closing the application, database resources must be released by calling the shutdown method:



Documentation suggest attaching a shutdown hook to application, just after database startup, in order to ensure proper database shutdown in every case




Last but not least recommendation: all nodes operation must be carried inside a transaction



Hello → World

The most basic example coming from documentation creates two nodes and links them together

relationships types are defined as enum types

Something more …

To go a little further than the usual “hello-world” I decided to write a small demo that filled a graph with a group of random generated “people”. First I made a function that adds to the graph a node representing a person with some random properties:


then I defined a first population pass where I filled the graph with an initial group (generation “0”) of random people. I used a “root” node as a marker to avoid repeating this step more than once.


 
In each “generation” iteration I selected two random group of “nodes”, filtered by sex, from the previous generation using the following Cypher query.


the nodes from the two sets are then linked together and some “child” nodes are then created


Conclusions

After playing a little with birth ratio and other parameters I managed to fill the graph with some hundreds of widely connected nodes. The graph is not a realistic representation of a real population ,since I should have considered many more constraints, but is still an interesting starting point. The full code can be downloaded from here.

No comments :

Post a Comment