You are currently viewing Using Neo4j and Python to Implement Graph Database

Using Neo4j and Python to Implement Graph Database

  • Post published:April 11, 2017

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:

from neo4j.v1 import GraphDatabase, basic_auth
driver = GraphDatabase.driver("bolt://localhost", 	auth=basic_auth("username", "password"))"CREATE (a:Person {name:'Name', title:'Title'})")
result ="MATCH (a:Person) WHERE = 'Arthur' RETURN AS name, a.title AS title")
for record in result:
	print("%s %s" % (record["title"], record["name"]))

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:

>>> from py2neo import Graph, Path
>>> graph = Graph()
# authentication for local graph database
authenticate("localhost:7474", "username", "password")
graph = Graph("http://localhost:7474/db/data/")
tx = graph.cypher.begin()
for name in ["A", "B", "C"]:
	tx.append("CREATE (person:Person {name:{name}}) RETURN person", name=name)
a, b, c = [ for result in tx.commit()]
friends = Path(a, "KNOWS", b, "KNOWS", c)

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:

from neo4jrestclient.client import GraphDatabase
gdb = GraphDatabase("http://localhost:7474/db/data/")

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:

n = gdb.nodes.create()
# Equivalent to
n = gdb.node()

To specify properties for the new node:

n = gdb.nodes.create(color="Green", width=35, height=12)

To access the properties:

value = n['key'] # Get property value
n['key'] = value # Set property value

To create relationships:

n1.relationships.create("Knows", n2) # Useful when the name of
					# relationship is stored in a variable

To specify properties for new relationships:

n1.Knows(n2, since=2015, introduced_at="Some Event")