Starting with graph databases using Neo4j and Rails

Graphs are data structures that are highly useful for understanding and represent many real-world problems in all areas, such as business, government, and science.

Starting with graph databases using Neo4j and Rails

Graphs are data structures that are highly useful for understanding and represent many real-world problems in all areas, such as business, government, and science.

To take advantage of graph databases we don't need to take a master's in Graph Theory. Instead of that, we must understand what a graph is, and be able to build one by drawing it on paper.

Mathematically speaking, a graph is just a collection of vertices and edges. Or if you don't like math, a set of nodes and relationships that connect them. Graphs represent entities with nodes (vertices), and the way that entities relate to each other is expressed by relationships (edges).

If you stop now and think about it, this structure allows us to model countless scenarios, from commercial systems to more complex problems such as optimization algorithms.

This graph model is formally known as a Property Graph. A property graph has the following characteristics:

  • It contains nodes and relationships.
  • Nodes contain properties (key-value pairs).
  • Relationships are named and directed, and always have a start and end node.
  • Relationships can also contain properties (key-value pairs).

Despite being intuitive and easy to understand, the property graph model can be used to describe almost all graph use cases.

As you probably are thinking, graph databases use the graph model to store data as a graph, with a structure consisting of vertices and edges, the two entities used to model any graph. In addition, you can use all the algorithms from the long history of graph theory to solve graph problems in less time than using relational database queries. I'll be covering some of them in my next posts.

What is a graph database?

Beyond the image above, and now talking specifically about Neo4j, it is an open-source graph database supported by Neo Technology, that stores data using the Property Graph model. It is reliable, with full ACID transactions, expressive, with a powerful, human-readable graph query language called Cypher, and simple, accessible by a convenient REST interface or an object-oriented Java API.

Enough theory and talking for now. Let's prepare our environment to play a little with Neo4j, and build a simple Rails application.

Installing Neo4j on development machines is very easy. If you are on OSX and is using brew, go ahead and issue brew install neo4j on a terminal window.

Or, if you prefer, follow these five steps:

  1. Download the Neo4j Community package.
  2. Unzip on your installations folder, let's say ~/Applications/.
  3. Create a symbolic link named neo4j to the unzipped folder. For instance: ln -s ~/Applications/neo4j-community-2.1.2 ~/Applications/neo4j.
  4. Create an environment variable named NEO4J_HOME pointing to this symbolic link.
  5. Change the PATH environment variable, adding the NEO4J_HOME/bin to it.

This way, in the future, when you want to update the Neo4j database on your machine, you can just download the new version, unpack, and update the symbolic link pointing it to the new version.

When you have it installed, open a terminal window and type: neo4j start. This command will start the Neo4j server on your machine. Now go check it on your browser accessing http://localhost:7474/. You'll be presented with a super nice administration panel, where you can visualize the data stored on your Neo4j instance, manipulate data using the Cypher Query Language, check all instance configurations, and more.

Neo4j Admin - Fresh Install

Neo4j is built on top of Java and the rock-solid JVM. As we want to use (MRI) Ruby on Rails here, let's connect our app using Neo4j's REST API.

To make things simpler, we'll use the awesome gem (surprisingly) called neo4j from @andreasronge. Version 2.x is the stable version. But here we will use it directly from the master branch where version three is under active development, which enables us to use the MRI Ruby connecting to Neo4j via its REST interface. If you are into JRuby, you can even use the stable version and connect using the embedded DB (by filesystem), which means a Neo4j instance running on the same JVM of your app.

Another option to connect an MRI Ruby application to Neo4j using the REST interface is the Neography gem from @maxdemarzi.

Neo4j Admin - With data

This blog post is intended to introduce you to the world of Graph Databases, giving some basic theory about graphs and a practical hands-on using Neo4j and Rails.

For future posts expect to read more about Neo4j, Cypher Query Language, and traversal algorithms.

So, what about learning by doing? And remember: graphs are everywhere!