Neo4j: Friends Edition

Creating a simple graph database using Neo4j in less than 10 minutes.

Selen Parlar
Oct 22, 2019 · 6 min read

Simply, a graph database is a database that pays equal attention to the data and the underlying relationships. It is a collection of relationships that uses graph structures for semantic queries to represent and store data. You can refer to my previous posts about graphs and graph databases for mote detail. In this post, we will walk through downloading and creating a simple graph database using the Neo4j.

What is Neo4j?

Neo4j is an open-source, non-relational (NoSQL), and native graph database that provides an Atomicity, Consistency, Isolation, and Durability (ACID) compliance backend for the applications [1]. It is available on GitHub or as a desktop application download. Here are some popular features of Neo4j:

  • Native graph database since the data is stored exactly as you whiteboard it,
  • Provides full database characteristics like ACID transaction compliance, cluster support, and runtime failover,
  • Has a declarative query language called Cypher to query the graph database,
  • Provides constant time traversals in big graphs due to the efficient node and relationship representations,
  • Has drivers for several programming languages like Java, JavaScript, .NET, and Python,
  • Provides visualizations.

Neo4j Setup

  1. To install Neo4j for desktop, use the link and click Download.
  2. Then, one can either create an account or sign in with an existing account.
  3. Once signed in, you will see the main screen of the application.

4. In the left pane, one can see the list of projects.

5. In the right pane, one can create and run databases and can install several plugins.

Let’s Start

  1. To create a database one can follow the 5 steps shown in red on the picture below:

2. Finally, click the Start button to start the database.

Let’s Interact

There are several ways to interact with Neo4j. Here are some of them:

  • opening the Neo4j Browser from Neo4j desktop,
  • typing localhost in one’s own browser,
  • creating an application in any programming language, then interact via the application.

Let’s See an Example Database

We have seen an example database representation in my previous post about one of the famous 90’s TV series FRIENDS. It’s designed to store nodes as main characters and the two different edges as close friends relationship and likes relationship. Characters are stored in the database with their names: Jennifer Aniston as Rachel Green, Courteney Cox as Monica Geller, Lisa Kudrow as Phoebe Buffay, Matt LeBlanc as Joey Tribbiani, Matthew Perry as Chandler Bing, and David Schwimmer as Ross Geller.

Apart from users, there are also pages about food, fashion, and animal.

Let’s Create

Friends database is a simple but nice example since we can represent each node with characters and the edges with the relationships between characters and the things they like. Let’s create this graph on Neo4j. To do so, we will use Cypher language which is a flexible language and provides easy creation of nodes and relationships.

Nodes

To create a node we will use the given syntax:

Using that syntax we can start to create the nodes of our graph database. For each character we have three different information; name, age, and sex. Let’s start with the node of Rachel. Note that the label of the node is Person and it has three properties: Name, Age, and Sex.

Rather than creating nodes one by one, we can also create multiple nodes at once. Let’s create node Monica and node Phoebe at the same time.

Once we add all the 6 character nodes, which are labeled as Person, to our database and using RETURN command we get the following visualization of our graph:

After the creation of Person nodes, we also add the different types of nodes for Food, Fashion, and Animal. Again we use the RETURN command to see the graph and it depicts each type of node in different colors.

Relationships

To create a relationship between nodes, we first get the two nodes. After loading the nodes, we create a relationship between them.

In our FRIENDS database, close friendship is a reciprocal relationship and the image below shows the close friends. For instance, Rachel’s close friends are Monica, Phoebe, and Ross whereas Monica’s close friends are Rachel, Chandler, and Joey.

Let’s add a relationship between Rachel and Monica using Neo4j. First, we need to load the two Person nodes whose Name properties are Rachel and Monica. Then we create an edge from node Rachel to node Monica and an edge from node Monica to node Rachel.

When we add all 7 relationships to our graph we get the following visualization:

Apart from the relationships between the same node types, we can also add relationships between any types of nodes. For instance, when we add the Rachel likes Ralph Lauren, Joey likes Pizza, Monica likes Turkey and Ross likes Dinosaur relationships we get the following graph.

Query

Since we created a graph database, we can run some queries on that. For instance, let’s retrieve the close friends of Rachel:

Another query to retrieve the person who likes any kinds of food:

Apart from the positive queries, we can also retrieve some results with NOT command. For instance, let’s see the person who does not like any food:

As can be seen from example queries, once you created a database, it is straightforward to retrieve the required information using Cypher. Moreover, one can also integrate a data file to the Neo4j. For instance, to load the CSV file data.csv into Neo4j one can use the code:

All in all…

In this post, we have provided an introduction about Neo4j which allows you to easily create a graph database and load data to it in a user-friendly manner. Within less than 15 minutes, you can install Neo4j and start to interact with your own graph database and apply various algorithms to it.

References

  1. Graph database overview
  2. Use cases of Neo4j
  3. Drawings are created using Sketch.io

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Sign up for Analytics Vidhya News Bytes

By Analytics Vidhya

Latest news from Analytics Vidhya on our Hackathons and some of our best articles! Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Selen Parlar

Written by

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Selen Parlar

Written by

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store