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
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
execute_queryAPI 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
EagerResultobject. This object is a named tuple withrecords: list ofneo4j.Records returned,keys: the list of keys present in the records, andsummarytheneo4j.ResultSummaryof the query.Under the hood
A lot happens under the hood:
Moreover, it tracks bookmarks through a default
BookmarkManagerimplementation so that subsequentexecute_queryreads can read the results of formerexecute_querywrites. 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_queryis 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_queryaccepts extra parameters:You can configure impersonation, select a specific database, change from the default
WRITERSrouting mode and/or configure bookmark management.Beyond
EagerResultEagerResultis 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.Resultand returns anything. Whatever it returns, will be returned from theexecute_querycall.neo4j.Resultfrom the transformer. This is because underlying transaction of the cursor will be closed whenexecute_queryreturns. 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.Resultthat has no non-optional parameters as transformer.For example (requires
pandasto 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