NoSQL databases are in demand these days because of various advantages, to list a few of them:
- They can handle large volumes of data at high speed with a scale-out architecture
- Stores unstuctured, semi-structured, or structured data
- Support multiple data structures
Neo4j is one of noSQL Graph Database. Let us take a look at some of its advantages below:
- Easy representation of data: Neo4j provides a very easy way to represent relationships and semi-structured data.
- 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:
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.
Below are few links to my other content:
How to connect and perform operations of Snowflake using Python
Snowflake is a powerful Data Warehouse of 2020 as many companies are migrating their data to Snowflake and hopefully…
Free Tableau Tutorial - Learn Tableau - Beginner to Advanced (Add to your resume)
Kashish has over 6 years of experience working in Data Analytics, Data Science and Reporting. He has great expertise on…
connect with me on linkedin :