Getting started with Neo4j

Kelsey Whitehead
5 min readFeb 27, 2017

--

This is part 2 of a series of posts on graph databases. You can start here, but if you’d like an introduction to the structure and use of a graph database you can find that here

Now that we’ve gotten some introduction into graph databases in general, let’s start using a graph database. I’ve decided to go with Neo4j and the companion query language Cypher for this series. Neo4j is the most popular graph database in use at this point in time, and so offers the largest amount of community resources available to assist while figuring your way around the system. Neo4j also offers a variety of tutorials and sample databases to practice with. That being said, there are plenty of options out there and it would be worth it to take some time to examine what each offers should you be considering implementing a graph backend into a system.

CYPHER

Before we dive into making nodes and relationships, we have to start with some basic Cypher commands. Cypher is a query language developed by Neo technology for use with Neo4j. It was inspired by SQL, and so will be fairly intuitive for anyone already familiar with SQL.

Some basic Cypher syntax from the ‘introduction to Cypher’ tutorial offered by Neo4j –

MATCH (node1:Label1)-[rel:TYPE]->(node2:Label2)

WHERE rel.property > {value}

RETURN rel.property, type(rel)

This first uses the ‘MATCH’ clause, which is used to indicate any relationship we would like to find. This is matched to the nodes and relationship we would like to include in the search. Within the parenthesis we state the name of the node (node1, node2), along with node Label (which could also be called its type — whatever object the node is representing)

It then uses a ‘WHERE’ clause to refine the search. In this instance, on a particular property held by the relationship between node1 and node2

The ‘RETURN’ clause states the data you would like returned by the query. Think SQL’s ‘SELECT’ statement.

If we wanted to delete a relationship or a node, we can simply replace the ‘RETURN’ statement at the end of the query with a ‘DELETE’ statement, and then specify the nodes or relationships to remove.

MATCH (node1:Label1)-[rel:TYPE]->(node2:Label2)

WHERE rel.property > {value}

DELETE rel

We can also build off of this basic structure to perform updates on our data — here changing the value of a property

MATCH (node1:Label1)-[rel:TYPE]->(node2:Label2)

WHERE rel.property > {value}

SET rel.property = ‘new value’

To create a new node or relationship you use the ‘CREATE’ keyword.

CREATE(node1:Label1{property1:’<something>’,property2:’<somethingelse>’})

As expected, this uses some of the same structure as the “MATCH” statement above. We replace ‘node1’ with the name of the node (some distinguishing feature of the data contained within), and replace ‘Label1’ with the type of node we want to create. We can also add however many properties we need to keep track of for our node within the curly braces.

DATABASE CREATION

To start, I’ve installed the Neo4j community edition from here and launched the program. Let’s make a new database by selecting ‘choose’ and creating a new folder ‘test.graphdb’ within the Neo4j folder

Then press start and navigate to localhost:7474 within your browser once the database has started.

Within the browser console you are able to perform any create, read, update or delete function as needed. It can be a useful tool for visualizing data and relationships — which makes it perfect for learning and demonstrating Neo4j. It is however a bit resource heavy for larger data sets. The Neo4j shell is significantly more useful for proper use, and I will be demonstrating it in later posts.

First, let’s start by adding some nodes. I’ll type the following three commands into the prompt within the web console —

CREATE(Billy:Person{name:’Billy’,id:’0000'})

CREATE(Susan:Person{name:’Susan’,id:’0001'})

MATCH(s:Person{name:’Billy’}),(d:Person{name:’Susan’})

CREATE (s)-[r:Friends_with]->(d)

Querying

If we now query the database using MATCH (n) RETURN (n) (a query that asks for everything and displays all results) we can see our nodes and relationship we just created!

I’ll add some more nodes and relationships so we can see some of the Cypher queries mentioned earlier in action

We can search for a specific node

Remove relationship

That was Bobs only relationship, so we might as well remove him from the database as well

Poor Bob

Let’s change Tammys name.

And now an to show all data, including the changes I just made

Conclusion

As you can see, Cypher is very straight forward for simple queries, and Neo4j is fairly intuitive for basic use. Next time we’ll look at converting an existing RDBMS to a graph database, and I will begin to examine some of the drawbacks I’ve found in my time working with Neo4j.

--

--