Export and import your Neo4j graph easily with APOC

Niyas Mohammed
3 min readJan 28, 2018

--

TLDR

We all know how frustrating it is trying to export your Neo4j database if you’re running the community edition.

One popular trick is to shut down your Neo4j service safely, and copy your database folder into a safe place and restart your service again. But be careful, you’re bound to be riddled issues related to user permissions if you’re not careful.

A better and recommended way to reliably dump your database has also been posted by the good people at Neo Technology, but it still requires you to shutdown and restart your service. But what if you’re just looking to export your small database quickly? Or maybe you’re trying to export the graph from within a pet Python project. Surely there’s got to be an easier way to do this right?

Enter APOC — Awesome Procedures On Cypher.

APOC is a Neo4j plugin that brings hundreds of procedures that help you do awesome stuff like data integration, graph algorithms and data conversion. It’s essentially some really great Neo4j procedures that you can easily call using Cypher, the Neo4j query language. Today, we’ll only be using a couple of these to export our graph and import it back.

Installing APOC

To install APOC (or any other Neo4j plugin), you can download the corresponding .jar file for the version of Neo4j you are running and place it in $NEO4J_HOME/plugins. Also make sure you have read and execute permissions on the .jar file:

$ chmod +rx apoc-3.x.x.x-all.jar

You need to restart Neo4j service once to enable plugin, but hold that up for a minute, because we also need to change add some configuration changes.

Open up $NEO4J_HOME/conf/neo4j.conf file and add the following lines:

dbms.security.procedures.unrestricted=apoc.export.*,apoc.import.*
apoc.export.file.enabled=true
apoc.import.file.enabled=true

Since we are doing something quite unusual here, I think this warrants an explanation.

Basically, Neo4j 3.2 and higher versions have increased security for procedures and functions (aka sandboxing). Procedures that use internal APIs have to be allowed in $NEO4J_HOME/conf/neoj4.conf with, e.g. dbms.security.procedures.unrestricted=apoc.trigger.*,apoc.meta.* for security reasons (or apoc.* for all). You can find out more about this here.

We’ve also set permissions for apoc to read and write to files.

Okay, good work- now you can restart your service once for the changes to take place and never worry about all this again.

$ sudo service neo4j restart

Exporting the graph to a file

In the following examples, I’ll be exporting to GraphML format. APOC lets you export to a bunch of other formats too- including Cypher.

If you want to export the entire database issue, open up Neo4j browser and type in

If you want to export only the results of a particular query, you can do so with apoc.export.graphml.query . See the APOC documentation for more on this.

Importing the graph from a file

Importing is as simple as exporting. Just point to the file and set your configuration variables:

A word of caution

Although apoc.export and apoc.import configurations contain a setting to storeNodeId , in practice, you’ll never be able to set the same Node id (<id> ) when you import this back. What this will instead do is to create a new property called id on each node and set it’s value to say,n1234 if the original the <id> was 1234 .

That’s it! That’s everything you should know about importing and exporting the graphs using APOC. Hopefully this little nugget will save you time and trouble as it has for me.

--

--