v5.5.0 - Feedback wanted on execute_query
📣
#896
Closed
robsdedude
announced in
Preview Features
Replies: 2 comments
-
With the new API will be stabilized. Some names were slightly adjusted. The feature is scheduled to hit GA with version 5.8.0. |
Beta Was this translation helpful? Give feedback.
0 replies
-
I love this new API, much simpler to implement! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
execute_query
API In Action
In version 5.5.0, we introduce a new experimental API to the driver. This is a simplified API for running queries against a database without needing to get into the detail of sessions and transactions.
Let us see it in action first, just below:
This runs a the cypher query inside a transaction with retries if necessary and returns an
EagerResult
object. This object is a named tuple withrecords
: list ofneo4j.Record
s returned,keys
: the list of keys present in the records, andsummary
theneo4j.ResultSummary
of the query.Under the hood
A lot happens under the hood:
Moreover, it tracks bookmarks through a default
BookmarkManager
implementation so that subsequentexecute_query
reads can read the results of formerexecute_query
writes. You can even pass this bookmark manager instance to sessions viadriver.session(..., bookmark_manager=driver.query_bookmark_manager)
, so the causal consistency is preserved across all the driver calls your application makes.As a consequence, you do not have to worry about all of this! This is why we believe
execute_query
is a great starting point for new users. It does many things right by default.And there is more!
Configuration
For more fine-grained scenarios,
execute_query
accepts extra parameters:You can configure impersonation, select a specific database, change from the default
WRITERS
routing mode and/or configure bookmark management.Beyond
EagerResult
EagerResult
is a good starting point, as it includes all you need about the results of your query. However, as its name suggests, it loads all records in memory, which is not a great thing to do when your query returns huge results (in size and/or quantity). Fortunately, we left out one configuration option above as there's much to say about it:result_transformer_
. It allows you to process the result cursor in any way. By default it'sneo4j.Result.to_eager_result
.But you can change it to any function (or any callable, really) that takes a
neo4j.Result
and returns anything. Whatever it returns, will be returned from theexecute_query
call.neo4j.Result
from the transformer. This is because underlying transaction of the cursor will be closed whenexecute_query
returns. So the cursor will be out of scope and dysfunctional.💡 Good to note:
In particular, any method in Python is a function that takes an object of its class as first argument. This means you can use any method of
neo4j.Result
that has no non-optional parameters as transformer.For example (requires
pandas
to be installed):Feedback wanted
This new API is currently marked as experimental.
What it means is that we are eagerly (no pun intended) waiting for your feedback.
Does it work well in your scenario? Do you wish there was more?
Could some defaults be improved?
Let us know and we will correct course in the next releases!
Beta Was this translation helpful? Give feedback.
All reactions