Export/Import Data using GraphDB (Neo4j)

This article focusses on techniques to take Backups and to share data in Graph Databases, particularly in Neo4j

Export/Import Data using GraphDB (Neo4j)

NoSQL databases are gaining more popularity in recent times because of their obvious advantages over the traditional SQL databases. We'll discuss the different types of NoSQL databases, their advantages and selection criteria based on your use case in a separate detailed article.

In this article, we will focus mainly on how to export or import data while using a GraphDB. This is important for taking backups, spinning up multiple instances and to create Dev and QA environments. We have considered Neo4j for this article. Neo4j is the most popular Graph database available in the market which is popular for its capability to support Machine Learning based applications and is being used by many Tech giants. Please visit their official site for more info - https://neo4j.com/

Below image shows how data can be visualized in Neo4j.

There are 2 main ways to export/import data.

  1. Using graph.db folder: Using this method is not recommended for Actual projects but can be used for local development purposes. The graph.db folder contains all the information stored in the database. So you can simply share this folder for exporting and for importing just replace this with the new graph.db folder. To access this folder, go to the neo4j desktop console and click "Open Folder" as shown below

    It opens up the folder related to the "Test" database where all the information is stored. Go to /data/databases which contain our required graph.db folder. This method is not feasible for actual projects because these folders will not be accessible to the developers. There comes the 2nd approach.                                                                                                                                                   
  2. Using APOC procedure: Neo4j provides additional features in the form of procedures using APOC library. To export/import data using APOC follow below simple steps:
    • Install APOC plugin
    • Update the below configurations in neo4j.conf file. To access this file, click on the Setting tab shown in the above image. Go to the end of that file and add below lines to allow APOC.

      dbms.security.procedures.unrestricted=apoc.*

      apoc.export.file.enabled=true

      apoc.import.file.enabled=true

    • Run “CALL apoc.export.graphml.all(‘sample.graphml’),{config}” to export all the graph data to a graphml file
    • And “CALL apoc.import.graphml(‘file:///sample.graphml’),{config}” to import graph data from a .graphml file
    • The above commands should be run in the same way you run queries in Neo4j. Sample command call apoc.import.graphml('file:///C:/Users/Test/Desktop/sample.graphml',{readLabels: true, storeNodeIds: false, defaultRelationshipType:"RELATED"}) 
    • If you are importing the same data and want to override existing, empty your existing DB and then import.

The second approach is recommended and it can be automated using any CI/CD pipeline. For suppose, developers won't have access to run queries on Production database. So, create a CI/CD pipeline to generate graphml file from the production DB by running the above APOC commands. Developers can download it and import in their local to replicate production.

These simple techniques are really useful and increase productivity by reducing the dependency on Database administrators and helps Projects to function smoothly by taking backups at regular intervals.

Thank you for going through our article and keep watching this space for more interesting articles.

Happy Hacking..