Neo4j Cypher Transform in Apache Hop

Sam L
5 min readNov 2, 2022

--

In this blog, I will look at the Neo4j Cypher transform in the Apache Hop ETL tool, because I find it helpful to see examples of how these transforms can be used.

As always, let’s start by consulting the documentation (and make sure you select the appropriate version!):

Per the description: “The Neo4j Cypher transform allows you to execute any Cypher through the Neo4j Bolt protocol using the Java driver.
Both reading, writing or both are supported. You can also call procedures and get the output.”

You can use the Neo4j Cypher transform to send any arbitrary cypher statement through Hop to Neo4j, such as match/delete, creating indexes, etc.

I’m going to stick with very basic examples, and I have two examples in mind:

  1. Simple example that creates nodes only.
  2. Slightly more complicated example that creates 2 nodes and their joining relationships.

__Example #1: create nodes only

I have a csv file that contains a list of humans. I’ll use a csv file input to grab that data, which will flow into a Neo4j Cypher transform like this:

Let’s look at how to use the Neo4j Cypher transform. Here is a very typical example:

The key to using this transform is:

> select the “Collect parameter values map” and

> enter a value for the “Name of the values map list” (I use “rows”, but you can use any value that makes you happy)

Fill out the Parameters Tab

> Click on “Get Parameters” for Hop to automatically populate all of the columns from your CSV file.

The “Field” column is column header from your input file.

The “Neo4j Type” is the data type detected by Hop.

The “Parameter” is a value you can edit, and it is the value we need to use in the Cypher tab and in our cypher statements. You need to match these values exactly, and may want to use copy / paste to avoid typos which will cause your cypher statement to error and fail.

Fill out the Cypher tab

“UNWIND $rows as row”:

The “UNWIND” is a cypher clause that takes a list/set and splits it back into individual rows.

The “$rows” is from our “Name of the values map list” in the Options tab.

“row” refers to the data in our CSV file and must match the “Parameter” column of the Parameters tab exactly.

That’s it. Running this will create “Human” nodes with those 6 properties.

__Example #2: create two nodes and their joining relationships

Here is the simple model that I have as a goal:

For this example, I have a CSV file that contains a list of Authors and the Books that they wrote. It looks like this:

Let’s start the same way by using a CSV file input and connecting it to a Neo4j Cypher transform:

The Options tab can be filled out exactly the same as before:

There is nothing new to note about the Parameters tab:

The Cypher tab is slightly different in that this example shows multiple MERGEs in a single cypher statement.

Out of curiosity, I tried to put two separate cypher statements into the transform like this

From reading the resulting error messages, the Neo4j Cypher transform is expecting one and only one cypher statement:

ERROR: java.lang.RuntimeException: Unable to run batch of cypher statements…. Expected exactly one statement per query but got 2

__Sharing these examples on Github

If you are curious to try out these transforms for yourself, please feel free to download the repo. My goal is to build out samples for each of the key Neo4j transforms as well as some other transforms that are of interest to my prospects and myself.

I’ll try to keep the Hop files organized to make it easy to get right to the specific example you are interested in. For this blog, please check the “Transform-Neo4j-Cypher” folder.

--

--

Sam L

I’m a sales engineer working at Neo4j. I’m hoping to use Medium to make life/work less frustrating.