Implementation Notes

Using Neo4j and Python to Implement Graph Database

vteam #679 has been working on a client’s web application of data analysis. This application is able to sift through the vast amounts of information available to its users through social media, news and blogs and identify trends and patterns. In this article, you will learn to implement graph database using Neo4j and Python.


Graph data modeling, in Neo4j, is when a user describes an arbitrary domain as a connected graph of nodes and relationships. While comparing RDBMS to Graph Database (Neo4j) or defining models in Neo4j, the following baselines are important:

  • Row in RDBMS is a node in Neo4j
  • Table in RDBMS is a label in Neo4j

Each node (entity or attribute) in the graph database model directly and physically contains a list of relationship-records that represent relationships to other nodes. These relationship records are organized by type and direction and may hold additional attributes. Whenever you run the equivalent of a JOIN operation, the database uses this list for direct access to the connected nodes, eliminating the need for excessive search / match computation.

The key difference between a graph and relational database is that relational databases work with sets, while graph databases work with paths and relationships.

Neo4j Python Driver

The Neo4j Python driver is officially supported by Neo4j and connects to the database using a binary protocol. To install python driver, run pip install neo4j-driver command and add the following code:

For anyone working with Python, the Neo4j community has contributed a range of driver options. These range from lightweight to comprehensive driver packages, as well as libraries designed for use with web frameworks such as Django. Both Python2 and Python3 are supported by most of the libraries. They include:

  • Py2neo
  • Neo4jRestClient
1- Py2neo

It is a client library and comprehensive toolkit for working with Neo4j, from within Python applications and from the command line. The core library has no external dependencies and has been carefully designed to be easy and intuitive to use. To install py2neo library, run pip install py2neo command.

The simplest way to try out a connection to the Neo4j server is via the console. Once you have started a local Neo4j server, open a new Python console and enter the following code:

A Graph object provides a basis for most of the interaction with the Neo4j server, that a typical client application will need to make. The database URI is therefore generally the only URI that needs to be provided explicitly.

2- Neo4jRestClient

The main goal of neo4j-rest-client was to enable Python programmers (already using Neo4j locally) through python-embedded, to use the Neo4j REST server. So the syntax of neo4j-rest-client’s API is fully compatible with python-embedded. However, a new syntax is introduced in order to reach a more python like style and to enrich the API with new features the Neo4j team introduces.

Available through the Python Package Index, run pip install neo4jrestclient command. The main class is GraphDatabase:

Due to the syntax which is fully compatible with python-embedded, the next lines only show the commands added and its differences.

For creating a node:

To specify properties for the new node:

To access the properties:

To create relationships:

To specify properties for new relationships: