Now let's try using Neo4j with Python. If it wasn't installed before, make sure to install the (officially supported) Neo4j Python driver:
sudo pip install neo4j-driver
Now we will write a simple Python script (named neo4jHelloWorld.py) to accomplish three tasks:
- Create a node representing the Python language entity
- Create an ACCESSED_FROM edge connecting Python with the Welcome message
- Query the Welcome message via the ACCESSED_FROM Python edge
We will start with our imports and our system arguments to handle hostname, username, and password from the command line:
from neo4j.v1 import GraphDatabase, basic_auth import sys hostname=sys.argv[1] username=sys.argv[2] password=sys.argv[3]
Next, we will connect to our local Neo4j instance using the bolt protocol:
driver=GraphDatabase.driver("bolt://" + hostname +
":7687",auth=basic_auth(username,password))
session=driver.session()
With our session established, we will create the Python language node:
createLanguage="CREATE (:Language {name:{name},version:{ver}});"
session.run(createLanguage, {"name":"Python","ver":"2.7.13"})
Next, we will create the ACCESSED_FROM edge:
createRelationship="""MATCH (m:Message),(l:Language) WHERE m.title='Welcome' AND l.name='Python' CREATE (m)-[:ACCESSED_FROM]->(l);""" session.run(createRelationship)
Then, we will query for the Python node via the ACCESSED_FROM edge to the Welcome message and process the result set as output:
queryRelationship="""MATCH (m:Message)-[:ACCESSED_FROM]->
(l:Language {name:'Python'})
RETURN m,l;"""
resultSet = session.run(queryRelationship)
for result in resultSet:
print("%s from %s" % (result["m"]["text"], result["l"]["name"]))
Finally, we will close our connection to Neo4j:
session.close()
Running this script from the command line yields the following output:
python neo4jHelloWorld.py 192.168.0.100 neo4j flynnLives Hello world! from Python
Going back to Neo4j Browser, if we click the ACCESSED_FROM edge, as shown in the screenshot provided previously in the chapter, two language nodes should now be connected to the Welcome message.
Now let's move to a more complicated example. The current data set stops at the ISS Expedition 50/51 mission. We will write a Python script to add data for the ISS Expedition 51/52. The name of the script will be neo4jISS52.py. The beginning will be very similar to the previous Python script, with our imports, parsing command-line arguments, and building the session connection object to our Neo4j instance:
from neo4j.v1 import GraphDatabase, basic_auth
import sys
hostname=sys.argv[1]
username=sys.argv[2]
password=sys.argv[3]
driver=GraphDatabase.driver("bolt://" + hostname + ":7687",
auth=basic_auth(username,password))
session=driver.session()
With our session established, we will create the new Mission node:
createMission="CREATE (:Mission {name:{name}});"
session.run(createMission,{"name":"ISS-51/52 (Soyuz)"})
With the Mission node created, we can now create our FLEW_ON edges to it from the three NASA astronauts who were assigned to it:
createRelationship="""MATCH (m:Mission),(a:Astronaut)
WHERE m.name={mname} AND a.name={aname}
CREATE (a)-[:FLEW_ON]->(m);"""
session.run(createRelationship,{"mname":"ISS-51/52 (Soyuz)",
"aname":"Jack D. Fischer"})
session.run(createRelationship,{"mname":"ISS-51/52 (Soyuz)",
"aname":"Peggy A. Whitson"})
session.run(createRelationship,{"mname":"ISS-51/52 (Soyuz)",
"aname":"Randolph J. Bresnik"})
With the mission and required edges created, we can now query the mission for all astronauts who flew on it, print out the results to the console, and close our connection:
queryRelationship="""MATCH (m:Mission {name:'ISS-51/52 (Soyuz)'})
<-[:FLEW_ON]-(a:Astronaut) RETURN m,a;"""
resultSet=session.run(queryRelationship)
for result in resultSet:
print("%s flew on %s" % (result["a"]["name"],result["m"]["name"]))
session.close()
Running this script from the command line yields the following output:
python neo4jISS52.py 192.168.0.100 neo4j flynnLives Randolph J. Bresnik flew on ISS-51/52 (Soyuz) Peggy A. Whitson flew on ISS-51/52 (Soyuz) Jack D. Fischer flew on ISS-51/52 (Soyuz)