Up and Running with Neo4j
Recently I’ve been working on an application where I think a graph database would be ideal. Previously, I would have stored records in a RDBMS like Postgresql and constructed a graph in memory. But, this time, I’m working with an incredibly large dataset. So, I’d like to query against it without having to reconstruct a graph in memory upon each server query. Although, I could cache this in memory, why not use a tool which is equipped to solve this problem. Let’s jump into working with Neo4j—a graph database.
Setup
First we need to download the desktop client https://neo4j.com/download-thanks-desktop/?edition=desktop&flavour=osx&release=1.5.9&offline=false. You can do this at this link.
Concepts
Desktop Client
Project files — allows you to save queries/guides and other scripts as cyper files
Favorites — Stored in the Neo4j browser for easy access
Graph
Nodes - Represent entities of a domain
Labels - Shape domain by grouping nodes into sets. These are used to classify nodes. For instance, City or Airport.
Relationships - connect two nodes. These are also known as edges. These always have a direction. For instance, a flight from SFO to IST can be two nodes which have a relationship of FlightTo which points from SFO to IST. Then, some relationship properties can be added, such as flight price and so on.
Properties - Named values added to nodes and relationships (edges)
Cypher Language
The cypher language is used for CRUD operations on nodes and edges (relationships).
Create
First, we can create nodes with CREATE
like so:
CREATE (:Airport {name: "SFO", latitude: 37.6192, longitude: -122.389977})
“Airport” is the label for the created entity. This can be anything and other key-value pairs can be added too. CMD+Enter
can be used to run the operation or you can press the blue play button.
Read
We can search for nodes with the MATCH
keyword like so:
MATCH (airport:Airport) return airport;
“airport” is a variable and can be named arbitrarily. :Airport
is the same label as we created above in the create statement.
Querying
Launch the desktop client
Pop open the Movie DBMS and follow the intro guide. It’s incredibly helpful.