This website uses cookies to enhance the user experience

Using Neo4j with Python

Share:

In this article, we will explore how to use Neo4j with Python.

Installation:

To get started with using Neo4j with Python, you first need to install the necessary packages. You can use pip to install the cypher-dsl package for working with Cypher queries and the neo4j-driver package for connecting to the database. Here is an example of how to install the required packages:

pip install cypher-dsl
pip install neo4j-driver

Connecting to Neo4j:

After installing the necessary packages, you can use Python to connect to the database. Here is an example of how to connect to a local Neo4j instance using the neo4j-driver package:

import os
from neo4j import GraphDatabase
from cypherdsl import session as CypherSession

# Set up connection details
url = 'bolt://localhost'
username = 'neo4j'
password = 'password'

# Connect to Neo4j database
session = CypherSession(url, username, password)

In the above example, we set up the connection details for connecting to a local instance of Neo4j. We then create a session object using the CypherSession class from the cypher-dsl package. This object is used for executing Cypher queries on the database.

Executing Cypher Queries:

Once you have connected to the Neo4j database, you can execute Cypher queries using Python. Here is an example of how to create a node in the database and add some properties to it:

# Create a new node with properties
result = session.run('''
    CREATE (n:Person {name:"John Doe", age:30})
    ''')
print(result)  # Outputs None

In the above example, we use the run() method of the session object to execute a Cypher query that creates a new node with the name "John Doe" and an age of 30. The result variable will be None, indicating that no results were returned from the query.

Querying Data:

You can also use Python to query data from the Neo4j database using Cypher queries. Here is an example of how to retrieve all nodes in the database with a specific label:

# Retrieve all nodes with the Person label
result = session.run('''
    MATCH (n:Person)
    RETURN n
    ''')
for row in result:
    print(row)  # Outputs rows of data for each node returned from the query

In the above example, we use the run() method again to execute a Cypher query that retrieves all nodes with the Person label. The results are then printed out using a for loop.

Updating Data:

Finally, you can also use Python to update data in the Neo4j database using Cypher queries. Here is an example of how to update the age property of a node:

# Update the age property of a node
result = session.run('''
    MATCH (n:Person {name:"John Doe"})
    SET n.age = 35
    ''')
print(result)  # Outputs None

In the above example, we use the run() method to execute a Cypher query that updates the age property of the node with the name "John Doe". The result variable will be None, indicating that no results were returned from the query.

Conclusion:

In this article, we explored how to use Neo4j with Python. We discussed the installation process and how to connect to a local instance of Neo4j using Python. We also looked at how to execute Cypher queries using Python to create, retrieve, update, and delete data in the database. By leveraging the power of graph databases and Python, you can build complex data models with ease.

0 Comment


Sign up or Log in to leave a comment


Recent job openings