Working with Neo4j using Python

Image for post
Image for post
neo4j database
Image for post
Image for post
python programming

NoSQL databases are in demand these days because of various advantages, to list a few of them:

  1. They can handle large volumes of data at high speed with a scale-out architecture
  2. Stores unstuctured, semi-structured, or structured data
  3. Support multiple data structures

Neo4j is one of noSQL Graph Database. Let us take a look at some of its advantages below:

  1. Easy representation of data: Neo4j provides a very easy way to represent relationships and semi-structured data.
  2. Faster Execution: Neo4j is fast because more connected data is easy to retrieve and navigate.

As python is becoming a handy language to build applications, dealing with data & much more. It is important to learn how we can connect and play with the data coming from NoSQL databases.

In this Blog, we will learn how we can extract data from Neo4j database.

To begin with it, we first need to install neo4j library/connector:

Go to command prompt or anaconda prompt if you are using anaconda’s jupyter or spyder.
Run below command:

pip install neo4j

We will extract movie data from neo4j default database:

Image for post
Image for post

In above graph, we can see there are multiple nodes for Actors and Movies. Also, there are relationships defined as ‘ACTED IN’, ‘DIRECTED’, etc.

Lets jump into code:

# Import modules from neo4j library
from neo4j import GraphDatabase, basic_auth
# Establish connection with neo4j database, mention your host, port, username & password(default port is 7687 and default host is localhost)
driver = GraphDatabase.driver("bolt://<host>:<port>", auth = basic_auth("<username>","<password>"))
# create a neo4j session in python
session = driver.session()
# store all the records(including nodes & relationships) in a list
records = list(session.run("Match ((n)-[r]->(m)) return n,r,m"))

Now, data has been stored in a list and is ready to be consumed as per user’s specific usecase.

NOTE: If you wish to run any other cypher(query language for Neo4j) statement/s, you can run in similar manner. Below is an example:

# To list all databases
databases = list(session.run("SHOW DATABASES"))
# To find distinct labels of nodes
distinct_labels = list(session.run("MATCH (n) RETURN distinct labels(n)"))

Hope this tutorial helped you to work with Neo4j using python.

Written by

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