Experimenting with Neo4j and Apache Zeppelin

(Neo4j)-[:LOVES]-(Zeppelin)

Andrea Santurbano
Apache Zeppelin Stories
5 min readMar 17, 2016

--

“A web-based notebook that enables interactive data analytics.
You can make beautiful data-driven, interactive and collaborative documents with SQL, Scala and
more.”
(taken from Apache Zeppelin official website)

The result of the experiment

I felt in love with Apache Zeppelin for it’s native support for Apache Spark, it allows to create powerful (big) data analytics dashboard really fast.
After some weeks spent using it with Apache Spark that “more” intrigued me, so i have tried to integrate Zeppelin with Neo4j, a graph database that i have used in an epidemiology related project.

The full notebook of this experiment is here:

To achive my goal i have used only pure javascript language directly on Zeppeling Notebook, using just one (at most two) framework(s):

  • Sigma.js: a JavaScript library dedicated to graph drawing. Sigma.js has a plugin that allows to connect to neo4j REST apis instantly;
  • Angular.js: an MVVM client-side framework. In particular for angular i use the Zeppelin’s angular display system (some deeping here)

Background: Zeppelin and Neo4j

You can get Zepplin in several ways:

  1. Building it from the source
  2. Dowloading it from official website
  3. Pulling a docker container, such as https://hub.docker.com/r/conker84/zeppelin/ or https://hub.docker.com/r/epahomov/docker-zeppelin/
  4. Build a docker container from a Dockerfile (https://github.com/conker84/docker-zeppelin)

Neo4j is a graph database developed by Neo Technology, Inc. Graph databases are well suited for datasets deeply interconnected that form a complex web difficult to represent in a relational (old) fashioned way.

You can download neo4j from official website

Step 1 (First Paragraph): Create some util functions

The first paragraph is for utility functions creation.

We found here two function:

  • loadJS: to dynamically load js using javascript Promises, simplifying the script injection phase;
  • randomColor: that creates random colors; its use will be clear afterwards.
How first paragraph will be displayed

Step 2 (Paragraph two): Create the query/visualization stage

The second paragraph contains the main code of this project.

Let’s to highlight the code.

This is the form were the user will be able to write cypher queries, we have a simple form with bootstrap classes for css (Zeppelin use Bootstrap so you can).

The initSigma function create the an util method called “neighbors” that get all neighbors from a known node.

Next we have the initGraph function that will be highlighted in several piece.

This part contains the general configuration of Sigma.js, we found three objects:

  • endpoint: contains the necessary data to connect to neo4j instance;
  • s: the Sigma.js instance, where is passed “graph” as container id, so the graph is rendered in that div;
  • dragListener: the Sigma.js dragNodes plugin instance.

This part is responsible of the node neighbors de/highlighting, in particular:

  • clickNode event is for highlighting part;
  • clickStage event is for de-highlighting part;

This part is responsible of passing the cypher query to neo4j server, after the nodes/edges are loaded every node will be colored by a color assigned to the relate Label (if it exists) so i have defined two properties:

  1. color: that will be used by sigma internals to color the node;
  2. originalColor: that will be used to retrive the original color when it will be hidend in neighbors highlighting phase.

The last part of initGraph function is responsible of get:

  • all relationship types (in this i will not use them);
  • all nodes labels where it is assinged one color per label.

The last part of paragraph 2 consists in the loading of Sigma.js javascript dependencies.

How the second paragraph will be displayed

The next step is to build a native Zeppelin interpreter that provides more features like:

  • Support to neo4j bolt protocol instead of REST apis;
  • Cypher syntax highlighting;
  • Native graph visualization.

See you soon ;)

--

--