@@ -61,6 +61,14 @@ To install the latest pre-release, use:
6161 python -m pip install --pre neo4j
6262
6363
64+ .. TODO: 7.0 - remove this note
65+
66+ .. note ::
67+
68+ ``neo4j-driver `` is the old name for this package. It is now deprecated and
69+ and will receive no further updates starting with 6.0.0. Make sure to
70+ install ``neo4j `` as shown above.
71+
6472.. note ::
6573
6674 It is always recommended to install python packages for user space in a virtual environment.
@@ -92,60 +100,40 @@ To deactivate the current active virtual environment, use:
92100Quick Example
93101*************
94102
95- Creating nodes and relationships.
96-
97103.. code-block :: python
98104
99- from neo4j import GraphDatabase
105+ from neo4j import GraphDatabase, RoutingControl
100106
101107
102108 URI = " neo4j://localhost:7687"
103109 AUTH = (" neo4j" , " password" )
104110
105111
106- def create_person (tx , name ):
107- tx.run(" CREATE (a:Person {name: $name} )" , name = name)
112+ def add_friend (driver , name , friend_name ):
113+ driver.execute_query(
114+ " MERGE (a:Person {name: $name} ) "
115+ " MERGE (friend:Person {name: $friend_name} ) "
116+ " MERGE (a)-[:KNOWS]->(friend)" ,
117+ name = name, friend_name = friend_name, database_ = " neo4j" ,
118+ )
108119
109120
110- def create_friend_of (tx , name , friend ):
111- tx.run(" MATCH (a:Person) WHERE a.name = $name "
112- " CREATE (a)-[:KNOWS]->(:Person {name: $friend} )" ,
113- name = name, friend = friend)
121+ def print_friends (driver , name ):
122+ records, _, _ = driver.execute_query(
123+ " MATCH (a:Person)-[:KNOWS]->(friend) WHERE a.name = $name "
124+ " RETURN friend.name ORDER BY friend.name" ,
125+ name = name, database_ = " neo4j" , routing_ = RoutingControl.READ ,
126+ )
127+ for record in records:
128+ print (record[" friend.name" ])
114129
115130
116131 with GraphDatabase.driver(URI , auth = AUTH ) as driver:
117- with driver.session() as session:
118- session.execute_write(create_person, " Alice" )
119- session.execute_write(create_friend_of, " Alice" , " Bob" )
120- session.execute_write(create_friend_of, " Alice" , " Carl" )
121-
132+ add_friend(driver, " Arthur" , " Guinevere" )
133+ add_friend(driver, " Arthur" , " Lancelot" )
134+ add_friend(driver, " Arthur" , " Merlin" )
135+ print_friends(driver, " Arthur" )
122136
123- Finding nodes.
124-
125- .. code-block :: python
126-
127- from neo4j import GraphDatabase
128-
129-
130- URI = " neo4j://localhost:7687"
131- AUTH = (" neo4j" , " password" )
132-
133-
134- def get_friends_of (tx , name ):
135- friends = []
136- result = tx.run(" MATCH (a:Person)-[:KNOWS]->(f) "
137- " WHERE a.name = $name "
138- " RETURN f.name AS friend" , name = name)
139- for record in result:
140- friends.append(record[" friend" ])
141- return friends
142-
143-
144- with GraphDatabase.driver(URI , auth = AUTH ) as driver:
145- with driver.session() as session:
146- friends = session.execute_read(get_friends_of, " Alice" )
147- for friend in friends:
148- print (friend)
149137
150138
151139*******************
@@ -155,99 +143,112 @@ Example Application
155143.. code-block :: python
156144
157145 import logging
158- from neo4j import GraphDatabase
159- from neo4j.exceptions import ServiceUnavailable
146+
147+ from neo4j import GraphDatabase, RoutingControl
148+ from neo4j.exceptions import DriverError, Neo4jError
160149
161150
162151 class App :
163152
164- def __init__ (self , uri , user , password ):
153+ def __init__ (self , uri , user , password , database = None ):
165154 self .driver = GraphDatabase.driver(uri, auth = (user, password))
155+ self .database = database
166156
167157 def close (self ):
168- # Don't forget to close the driver connection when you are finished with it
158+ # Don't forget to close the driver connection when you are finished
159+ # with it
169160 self .driver.close()
170161
171162 def create_friendship (self , person1_name , person2_name ):
172163 with self .driver.session() as session:
173- # Write transactions allow the driver to handle retries and transient errors
174- result = session.execute_write(
175- self ._create_and_return_friendship, person1_name, person2_name)
176- for record in result:
177- print (" Created friendship between: {p1} , {p2} " .format(
178- p1 = record[' p1' ], p2 = record[' p2' ]))
164+ # Write transactions allow the driver to handle retries and
165+ # transient errors
166+ result = self ._create_and_return_friendship(
167+ person1_name, person2_name
168+ )
169+ print (" Created friendship between: "
170+ f " { result[' p1' ]} , { result[' p2' ]} " )
179171
180- @ staticmethod
181- def _create_and_return_friendship (tx , person1_name , person2_name ):
172+ def _create_and_return_friendship (self , person1_name , person2_name ):
182173
183174 # To learn more about the Cypher syntax,
184175 # see https://neo4j.com/docs/cypher-manual/current/
185176
186- # The Reference Card is also a good resource for keywords,
187- # see https://neo4j.com/docs/cypher-refcard/current /
177+ # The Cheat Sheet is also a good resource for keywords,
178+ # see https://neo4j.com/docs/cypher-cheat-sheet /
188179
189180 query = (
190181 " CREATE (p1:Person { name: $person1_name }) "
191182 " CREATE (p2:Person { name: $person2_name }) "
192183 " CREATE (p1)-[:KNOWS]->(p2) "
193- " RETURN p1, p2"
184+ " RETURN p1.name , p2.name "
194185 )
195- result = tx.run(query, person1_name = person1_name, person2_name = person2_name)
196186 try :
197- return [{" p1" : record[" p1" ][" name" ], " p2" : record[" p2" ][" name" ]}
198- for record in result]
187+ record = self .driver.execute_query(
188+ query, person1_name = person1_name, person2_name = person2_name,
189+ database_ = self .database,
190+ result_transformer_ = lambda r : r.single(strict = True )
191+ )
192+ return {" p1" : record[" p1.name" ], " p2" : record[" p2.name" ]}
199193 # Capture any errors along with the query and data for traceability
200- except ServiceUnavailable as exception:
201- logging.error(" {query} raised an error: \n {exception} " .format(
202- query = query, exception = exception))
194+ except (DriverError, Neo4jError) as exception:
195+ logging.error(" %s raised an error: \n %s " , query, exception)
203196 raise
204197
205198 def find_person (self , person_name ):
206- with self .driver.session() as session:
207- result = session.execute_read(self ._find_and_return_person, person_name)
208- for record in result:
209- print (" Found person: {record} " .format(record = record))
199+ names = self ._find_and_return_person(person_name)
200+ for name in names:
201+ print (f " Found person: { name} " )
210202
211- @ staticmethod
212- def _find_and_return_person (tx , person_name ):
203+ def _find_and_return_person (self , person_name ):
213204 query = (
214205 " MATCH (p:Person) "
215206 " WHERE p.name = $person_name "
216207 " RETURN p.name AS name"
217208 )
218- result = tx.run(query, person_name = person_name)
219- return [record[" name" ] for record in result]
209+ names = self .driver.execute_query(
210+ query, person_name = person_name,
211+ database_ = self .database, routing_ = RoutingControl.READ ,
212+ result_transformer_ = lambda r : r.value(" name" )
213+ )
214+ return names
220215
221216 if __name__ == " __main__" :
222- # See https://neo4j.com/developer/aura-connect-driver/ for Aura specific connection URL.
217+ # For Aura specific connection URI,
218+ # see https://neo4j.com/developer/aura-connect-driver/ .
223219 scheme = " neo4j" # Connecting to Aura, use the "neo4j+s" URI scheme
224220 host_name = " example.com"
225221 port = 7687
226- url = f " { scheme} :// { host_name} : { port} "
222+ uri = f " { scheme} :// { host_name} : { port} "
227223 user = " <Username for Neo4j database>"
228224 password = " <Password for Neo4j database>"
229- app = App(url, user, password)
225+ database = " neo4j"
226+ app = App(uri, user, password, database)
230227 try :
231228 app.create_friendship(" Alice" , " David" )
232229 app.find_person(" Alice" )
233230 finally :
234231 app.close()
235232
236233
237- *****************
238- Other Information
239- *****************
234+ *******************
235+ Further Information
236+ *******************
240237
241- * `Neo4j Documentation `_
242- * `The Neo4j Drivers Manual `_
243- * `Cypher Cheat Sheet `_
244- * `Example Project `_
238+ * `The Neo4j Operations Manual `_ (docs on how to run a Neo4j server)
239+ * `The Neo4j Python Driver Manual `_ (good introduction to this driver)
240+ * `Python Driver API Documentation `_ (full API documentation for this driver)
241+ * `Neo4j Cypher Cheat Sheet `_ (summary of Cypher syntax - Neo4j's graph query language)
242+ * `Example Project `_ (small web application using this driver)
243+ * `GraphAcademy `_ (interactive, free online trainings for Neo4j)
245244* `Driver Wiki `_ (includes change logs)
246- * `Neo4j Aura `_
245+ * `Neo4j Migration Guide `_
247246
248- .. _`Neo4j Documentation` : https://neo4j.com/docs/
249- .. _`The Neo4j Drivers Manual` : https://neo4j.com/docs/driver-manual/current/
250- .. _`Cypher Cheat Sheet` : https://neo4j.com/docs/cypher-cheat-sheet/current/
247+ .. _`The Neo4j Operations Manual` : https://neo4j.com/docs/operations-manual/current/
248+ .. _`The Neo4j Python Driver Manual` : https://neo4j.com/docs/python-manual/current/
249+ .. _`Python Driver API Documentation` : https://neo4j.com/docs/api/python-driver/current/
250+ .. _`Neo4j Cypher Cheat Sheet` : https://neo4j.com/docs/cypher-cheat-sheet/
251251.. _`Example Project` : https://github.com/neo4j-examples/movies-python-bolt
252+ .. _`GraphAcademy` : https://graphacademy.neo4j.com/categories/python/
252253.. _`Driver Wiki` : https://github.com/neo4j/neo4j-python-driver/wiki
253- .. _`Neo4j Aura ` : https://neo4j.com/neo4j-aura /
254+ .. _`Neo4j Migration Guide ` : https://neo4j.com/docs/migration-guide/current /
0 commit comments