I went to a very cool meetup this week about Neo4j, a graph database. It is not something I have had the chance to get my hands on before, so it was great to get a taste of it. This post is a short introduction to graph databases and a small example of what one can do with Neo4j.

Graph databases

Definition

In computing, a graph database is a database that uses graph structures for semantic queries with nodes, edges and properties to represent and store data. A key concept of the system is the graph (or edge or relationship), which directly relates data items in the store. Wikipedia

And to insist even more on the importance of relationships:

[…] a graph database is a database designed to treat the relationships between data as a first-class citizen in the data model. Neo4j

Based on these definitions and the experience I have working with relational databases, I feel like relational databases are good for storing data and dealing with very simple relationships (e.g. customer, account, which account belongs to which customer). On the other hand, graph databases are good for storing data together with their relationships and resolve complex relationships queries (e.g. people, connections, who are the friends of my friends of my friends).
This being said, I am very new to this world so don’t take my word for granted!

Structure

At its minimum a graph database is made of:

  • Node
    Represents a basic entity that can exist on its own (e.g. a person)
  • Relationship
    Represents a connection between two nodes (e.g. is friends with)

And to complete the representation of the graph and make it more interesting, graph databases also define:

  • Properties
    Information about nodes (e.g. name of a person) or relationships (met in xx).

Neo4j

Let’s take a look at an actual graph database: Neo4j. Bear in mind that I didn’t pick Neo4j because I think it is the best graph database platform out there, I picked it because this is what I learnt this week.
One of the great thing though, is that it has a very nice sandbox available for free. So it is very easy to get started, no need to install anything.

Neo4j uses a query language called Cypher. This cheatsheet gives a good idea of what the language looks like.

Creating a graph and relationships

Here a little graph of people that I have created:

People graph

You can see the command I run to create the nodes: CREATE (:Person { name: "Alice"}). Person is called a label. name: "Carole" is a property of the node with key name and value Alice.

I then created relationships between people, here is my final graph:

Friends graph

I find the graph so cool! It is super simple to create relationships. You need to match the nodes you want to create relationships on and then create the relationships:

MATCH(claire:Person{name: "Claire"})
MATCH(alice:Person{name: "Alice"})
CREATE (claire)-[r:FRIENDS_WITH]->(alice)
Analysing the graph

Now that I have a graph (yes, it’s a tiny one…), let’s take a look at people’s relationships.

Find friends

Who are my friends?

Carole's friend

This is sad, I should work on my social life…

Who are Alice’s friends?

Carole's friend

Well, she is more popular than me…

Let’s have a look at the query:

MATCH (alice:Person{ name: "Alice" })-[:FRIENDS_WITH]-(p:Person)
RETURN p

I find the query super readable: we are looking at the Person node Alice, i.e. with property name equal to "Alice" and we are looking for the Person nodes who have the relationships FRIENDS WITH with her.

Find friends of my friends

Now, a slightly more complex query, who are friends with my friends? (well, friend in this case…)

Carole's friend's friends

Thanks to my single friend Alice, I am indeed connected to John and Claire.
And this is the query:

MATCH (carole:Person{ name: "Carole" })-[:FRIENDS_WITH]-(p1:Person)-[:FRIENDS_WITH]-(p2:Person)
RETURN p2

Again super readable! We are simply traversing the graph and describing what we are looking for: looking at the Person node Carole we are interested in her friends, i.e. Person nodes with relationships FRIENDS_WITH and from there we are looking for their friends as well.


And that is my full experience with graph databases so far!