APOC

APOC stands for Awesome Procedures on Cypher. Before APOC’s release, developers needed to write their own procedures and functions for common functionality that Cypher or the Neo4j database had not yet implemented for support. Each developer might write his own version of these functions, causing a lot of duplication. (Userguide v.3)

Introduction

APOC needs to be installed and is not embedded by default.

With so much to offer in the APOC library, it is easy to feel overwhelmed and unsure how to approach it. There are a variety of docs and blog posts that show how to use some of the procedures, which are linked at the bottom of this guide. To get started, though, let us take a look at some helpful ways to begin working with APOC.

Running call apoc.help('keyword') will lists all procedures and functions that have the keyword in their name. If no keyword is provided, it will return a list of all APOC procedures and functions, along with their signatures and descriptions.

If you use RETURN apoc.version() it displays the current APOC version.

General Syntax

CALL apoc.<subpackage>.<procedure>(<argument1>,<argument2>,...);

Examples

  • apoc.load.csv

  • apoc.load.xls

  • apoc.load.html

  • apoc.date.format(..)

  • apoc.load.json(url)

  • apoc.math.round()

  • apoc.export.csv.all

  • apoc.diff.nodes

Working with MAPs

Using maps we can create a data-structure:

return {name:"Jan",credits:{MDA:6,STM:4,DMA:4},nbrofCars:null}

The data can be assigned to a variable teacher:

:param teacher => ({name:"Jan",credits:{MDA:6,STM:4,DMA:4},nbrofCars:null});

APOC offers lots of functions to handle the map datastructure : call apoc.help('.map').

For example:

return apoc.map.flatten($teacher)

We can “clean” the map removing certain datapoints or keys”. For example we would like to remove the credits-keys and the values in the map returning a null value.

return apoc.map.clean($teacher,['credits'],[null])

Creating Nodes

We already know how to use Cypher we to create nodes and add labels and properties to those nodes

CREATE (node:Person:Actor {name: "Tom Hanks"})
RETURN node;

With APOC, the equivalent code becomes :

CALL apoc.create.node(["Person", "Actor"], {name: "Tom Hanks"})

Of course we would like to create several nodes in as less lines of code as possible. This can be done with the procedure apoc.create.nodes. This procedures requires two inputs

  • label : a list of strings

  • properties : a list of dictionaries covering the properties for each of the to be created nodes.

For Example:

CALL apoc.create.nodes(['Actor','Human'],
[{name:"Brad Pitt",age:50,place:"USA"},
{name:"Angelina Jolie",age:50,place:"USA"}])

This is quite difficult to read. In stead the following code will be more readible:

:param labels =>  (["Human", "Actor"]);
:param properties => ([{name: "Brad Pitt", place: "USA" , age:50}, {name: "Angelina Jolie", place: "USA",age:50}]);

The code above defines the labels of the nodes and the properties

CALL apoc.create.nodes($labels, $properties);

Creating RelationShips

MATCH (p1:Actor {name: "Brad Pitt"})
MATCH (p2:Actor {name: "Angelina Jolie"})
CALL apoc.create.relationship(p1, "ACTEDTOGETHER", {movie:['By the Sea'],year:2014}, p2)
YIELD rel
RETURN rel;

The code-snippet below illustrates the syntax of this apoc procedure. It requires two nodes (source and target), a string for the relation and a map to describe the properties (if any) between the two nodes:

Last change: Oct 30, 2023