@@ -61,6 +61,14 @@ To install the latest pre-release, use:
61
61
python -m pip install --pre neo4j
62
62
63
63
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
+
64
72
.. note ::
65
73
66
74
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:
92
100
Quick Example
93
101
*************
94
102
95
- Creating nodes and relationships.
96
-
97
103
.. code-block :: python
98
104
99
- from neo4j import GraphDatabase
105
+ from neo4j import GraphDatabase, RoutingControl
100
106
101
107
102
108
URI = " neo4j://localhost:7687"
103
109
AUTH = (" neo4j" , " password" )
104
110
105
111
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
+ )
108
119
109
120
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" ])
114
129
115
130
116
131
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" )
122
136
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)
149
137
150
138
151
139
*******************
@@ -155,99 +143,112 @@ Example Application
155
143
.. code-block :: python
156
144
157
145
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
160
149
161
150
162
151
class App :
163
152
164
- def __init__ (self , uri , user , password ):
153
+ def __init__ (self , uri , user , password , database = None ):
165
154
self .driver = GraphDatabase.driver(uri, auth = (user, password))
155
+ self .database = database
166
156
167
157
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
169
160
self .driver.close()
170
161
171
162
def create_friendship (self , person1_name , person2_name ):
172
163
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' ]} " )
179
171
180
- @ staticmethod
181
- def _create_and_return_friendship (tx , person1_name , person2_name ):
172
+ def _create_and_return_friendship (self , person1_name , person2_name ):
182
173
183
174
# To learn more about the Cypher syntax,
184
175
# see https://neo4j.com/docs/cypher-manual/current/
185
176
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 /
188
179
189
180
query = (
190
181
" CREATE (p1:Person { name: $person1_name }) "
191
182
" CREATE (p2:Person { name: $person2_name }) "
192
183
" CREATE (p1)-[:KNOWS]->(p2) "
193
- " RETURN p1, p2"
184
+ " RETURN p1.name , p2.name "
194
185
)
195
- result = tx.run(query, person1_name = person1_name, person2_name = person2_name)
196
186
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" ]}
199
193
# 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)
203
196
raise
204
197
205
198
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} " )
210
202
211
- @ staticmethod
212
- def _find_and_return_person (tx , person_name ):
203
+ def _find_and_return_person (self , person_name ):
213
204
query = (
214
205
" MATCH (p:Person) "
215
206
" WHERE p.name = $person_name "
216
207
" RETURN p.name AS name"
217
208
)
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
220
215
221
216
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/ .
223
219
scheme = " neo4j" # Connecting to Aura, use the "neo4j+s" URI scheme
224
220
host_name = " example.com"
225
221
port = 7687
226
- url = f " { scheme} :// { host_name} : { port} "
222
+ uri = f " { scheme} :// { host_name} : { port} "
227
223
user = " <Username for Neo4j database>"
228
224
password = " <Password for Neo4j database>"
229
- app = App(url, user, password)
225
+ database = " neo4j"
226
+ app = App(uri, user, password, database)
230
227
try :
231
228
app.create_friendship(" Alice" , " David" )
232
229
app.find_person(" Alice" )
233
230
finally :
234
231
app.close()
235
232
236
233
237
- *****************
238
- Other Information
239
- *****************
234
+ *******************
235
+ Further Information
236
+ *******************
240
237
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)
245
244
* `Driver Wiki `_ (includes change logs)
246
- * `Neo4j Aura `_
245
+ * `Neo4j Migration Guide `_
247
246
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/
251
251
.. _`Example Project` : https://github.com/neo4j-examples/movies-python-bolt
252
+ .. _`GraphAcademy` : https://graphacademy.neo4j.com/categories/python/
252
253
.. _`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