Sofía Maiolo
Blog
DatosMarch 7, 2021

Extending Neo4j: User Functions & Procedures

Step-by-Step Tutorial for Extending Neo4j with User Functions and Procedures.

Continuing with the series on Neo4j, in this post I’ll provide a brief tutorial on how to start extending Neo4j by working with user functions and procedures. Neo4j offers various extension options, which are explained here. Of these, I’ll be focusing on user functions and procedures. Let’s go through it step by step:


Setting Up the Environment

We’ll need to install the following:

  • Java --> To use Neo4j v4 or later, JDK 11 is required. All requirements are listed here.

  • Maven --> Installing Maven is very simple: download the ZIP file from here, and set the environment variables and PATH as explained here.

  • Any Java IDE --> Eclipse, NetBeans, IntelliJ IDEA, or whichever you prefer :)

  • Neo4j Desktop --> All instructions are here.

  • GitHub Desktop --> Download it from here.

Running Neo4j

After installing it, simply run Neo4j Desktop, and you’ll see that Neo4j automatically creates a project and a test database. You can run a few queries on the test database to verify that everything is working correctly. Some basic queries you can run are:


View the graph schema:

call db.schema.visualization

Count the number of nodes:

MATCH (n)
RETURN count(n)

List the tags:

CALL db.labels()

Once we’ve verified that the instance is working correctly, let’s move on to the next step.


Creating the Neo4j function

All the information you need to start a project from scratch is available here, but the simplest approach is to use the example provided by Neo4j, compile it, and then customize it to your liking.


The example is available on GitHub; you should clone it using GitHub Desktop, and then, from the project folder in a command prompt, build the JAR by running:

....\Documents\GitHub\neo4j-procedure-template> mvn clean package

The resulting JAR will be generated in \neo4j-procedure-template\target.


Adding the JAR to the Neo4j database

Now you need to add the generated JAR to the database’s `plugins` folder.


To do this, from Neo4j Desktop, select the database, click the three dots, and select "Open folder." A File Explorer window will open showing the database’s installation folder. From there, locate the `plugins` subfolder. For example:

AppData\Local\Neo4j\Relate\Data\dbmss\dbms-78ee2f69-484f-4d5d-bd01-582f79f6cbb1\plugins

Simply paste the .jar file into that folder and restart the database.


Testing the user function

Let’s test to make sure the plugin actually works. On the Neo4j GitHub repository, there are several query examples we can try. I tested with the following:

MATCH (n:Person)
WITH n ORDER BY n.born
RETURN n.born, example.last(n) as last

Customizing Our Function

Now that we’ve seen the general idea, we’ll need to modify the code in the sample project and customize it to implement our own functions and procedures. To do this, open the project in your Java IDE and make the necessary changes to the classes.


Here’s an example of how to implement a BFS algorithm, and the Neo4j documentation contains several interesting use cases for reference.


This way, we can quickly add functionality to Neo4j.


See you next time!