Goals This guide explains how to use, create and deploy user defined procedures and functions, the extension mechanism of Cypher, Neo4j’s query language. It also covers existing, widely used procedure libraries Prerequisites You should have learned to read and write… Learn More →
This guide explains how to use, create and deploy user defined procedures and functions, the extension mechanism of Cypher, Neo4j’s query language. It also covers existing, widely used procedure libraries
You should have learned to read and write statements in Cypher and seen the need for additional capabilities.
Cypher is a quite powerful and expressive language, with first class graph pattern and collection support.
But sometimes you need to do more than it currently offers, like additional graph algorithms, parallelization or custom conversions.
That’s why the ability to extend Cypher with User Defined Procedures and Functions was added to the Neo4j 3.0 and 3.1 releases. But not only users, even Neo4j itself provides and utilizes custom procedures. Many of the monitoring, introspection and security features exposed by Neo4j-Browser are implemented using procedures.
What are Procedures and Functions?
- Functions are simple computations / conversions and return a single value
- Functions can be used in any expression or predicate
- Procedures are more complex operations and generate streams of results.
- Procedures must be used within the
YIELDtheir result columns
- They can generate, fetch or compute data to make it available to later processing steps in your Cypher query
Listing & Using Functions and Procedures
There are a number of built in procedures, two of which are used to list available functions and procedures.
Run the following statements along to get a hang of the usage and see their results.
dbms.functions() to list functions.
Each procedure returns one or more columns of data. With
yield these columns can be selected and also aliased and are then available in your Cypher statement.
Of course you can also process the result columns with other Cypher clauses. Here we group them by package.
As of Neo4j 3.1, all functions available are directly part of the Cypher implementation, so User Defined Functions would only come from installed libraries.
You can take any procedure library and deploy it to your server to make additional procedures and functions available.
Please also have a look at the procedure section in the Neo4j Manual.
Deploying Procedures & Functions
If you built your own procedures or downloaded them from an community project, they are packaged in a jar-file. You can copy that file into the
$NEO4J_HOME/plugins directory of your Neo4j server and restart.
|A word of caution. As procedures and functions use the low level Java API they can access all Neo4j internals as well as the file system and machine. That’s why you should know which procedures you deploy and why. Only install procedures from trusted sources. If they are open source, check their source-code and best build them yourself.|
Procedure and Function Gallery
In our procedure and functions gallery we describe some of the impressive procedure libraries built by our community. Check it out to see what’s already there. Many of your needs will already be covered by those, for example:
- graph algorithms
- index operations
- database/api integration
- graph refactorings
- import and export
- spatial index lookup
- rdf import and export
- and many more
Here are two cool examples of what you can do:
A procedure to load data from another database:
A function to format and parse timestamps of different resolutions:
Developing your own Procedures and Functions
You can find details on writing and testing procedures in the Neo4j Manual.
There is even an example GitHub repository with detailed documentation and comments that you can clone directly and use as a starting point.
Here are just some initial tips:
User-defined functions are simpler, so let’s start with them:
@UserFunctionare annotated, public Java methods in a class
- their default name is package-name.method-name
- they return a single value
- are read only
User defined procedures are similar:
@Procedureannotated, Java methods
- with an additional
READ, WRITE, DBMS)
- return a Java 8
Streamof simple objects with
- these fields names are turned into result columns available for
These things are valid for both:
@Nameannotated parameters (with optional default values)
- can use an injected
@Context public GraphDatabaseService
- run within transaction of the Cypher statement
- supported types for parameters and results are:
Long, Double, Boolean, String, Node, Relationship, Path, Object