cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 2: migrate recent content

Stored procedures similar to those in RDBMS using python driver

kashu94
Node Clone

I wanted to know if I could write stored procedures similar to those in RDBMS, to update edge attributes or node attributes on the fly rather than fetching all values and then performing calculations ans storing all these new values back to database. Is there anything like this possible using the python driver?

1 ACCEPTED SOLUTION

Not that I know of. You can also create custom functions, which you can call from cypher. Implementing complex calculations in a custom function (single value returned) or a custom procedure would be a good use case. You need to code using the Java API and deploy your code each server in your cluster. I have done this for custom procedures I need to run and it works very well.

If you are using the python driver, can't you use it to retrieve the data, perform calculations in python, and then write back updates with the python driver?

View solution in original post

8 REPLIES 8

glilienfield
Ninja
Ninja

Yes, you can write a custom procedure and call it in cypher using the python driver. Custom procedures are written in Java and deployed to each server. This is not allowed with Aura DB, if you are using the service.

https://neo4j.com/developer/cypher/procedures-functions/

kashu94
Node Clone

I'll be loading and updating data via CSV files. So, I'm looking for something like, whenever I maybe encounter a match on (d)-[r]->(t) suppose, then I would like do some calculations on edge attributes and store the updated values. I want this to happen on the fly ..... Should I go for triggers?

Yeah, I don’t think a custom procedure is a good fit for your use case. It is good for traversal algorithms, which cypher is good at.

yes, a trigger would be an option to investigate.

The thing is, I have edge attributes like Q1, Q2, Q3, min, max, mean (where Q is Quartile). So, whenever a match happens, I would like to perform some complex calculations on these values and store them back. Is there a way I could call a python function from a cypher query?
Something like,
def function1():
------ function body ------

query = "
MERGE (d:Diagnosis)-[r:DiagnosisToTreatment]->(t:Treatment)
ON CREATE ........
ON MATCH <call a python function here> "

The functions would obviously take in parameters and return a result which would be used to update the edge attributes. Why I don't want to write those functions in cypher is because I would be requiring some python libraries like DDsketch .... can you suggest something like this?

Not that I know of. You can also create custom functions, which you can call from cypher. Implementing complex calculations in a custom function (single value returned) or a custom procedure would be a good use case. You need to code using the Java API and deploy your code each server in your cluster. I have done this for custom procedures I need to run and it works very well.

If you are using the python driver, can't you use it to retrieve the data, perform calculations in python, and then write back updates with the python driver?

Yes, even I thought the last resort would be to fetch the data, perform calculations and then write back results. But I was also going through 'Pypher', not sure if that could be used in my case though ....

Pypher looks like a command line tool. Maybe you can write code in python to orchestrate everything and call Pypher from python to get your calculations.

Will look into it further. Thanks a lot.