CS 1656 Lab 06: Cypher+Neo4j+Python solution


Original Work


Rate this product

In this lab, we will query a Neo4j graph database using with Cypher language and Python. Neo4j
is a highly scalable, native graph database purpose-built to leverage not only data but also its relationships. Cypher is a declarative graph query language that allows for expressive and efficient
querying and updating of the graph store.
In [1]: # Use the following to get the neo4j database password from the user
import getpass
print (“Give me your neo4j password:”)
neopass = getpass.getpass()
#print (“You typed:”, neopass)
Give me your neo4j password:
In [2]: from neo4j import GraphDatabase
# More information on neo4j python API at:
# http://neo4j.com/docs/api/python-driver/current/
#Connect to the database
uri = “bolt://localhost:7687”
#auth=(“neo4j”, neopass)
driver = GraphDatabase.driver(uri, auth=(“neo4j”, neopass))
#Start new session
session = driver.session()
#Start new transaction
transaction = session.begin_transaction()
1.2.1 Queries
Q1) Find the actor named “Tom Hanks”.
In [ ]: result = transaction.run(“””
MATCH (tom:Actor {name: ‘Tom Hanks’})
for record in result:
print (record)
1.2.2 Tasks
Q2) Find the movie with title “Avatar”.
Q3) Find movies released in the 1990s.
Q4) List all Tom Hanks movies.
Q5) Who directed “Avatar”.
Q6) Tom Hanks’ co-actors.
Q7) How people are related to “Avatar”.
Q8) Extend Tom Hanks co-actors, to find co-co-actors who haven’t worked with Tom Hanks.
Q9) Find someone to introduce Tom Hanks to Tom Cruise.
Let’s close the session and the transaction.
In [ ]: transaction.close()