Data migrationMigrate from Neo4j using a single Cypher query

Migrating from Memgraph to Neo4j with a single cypher query

Migrating your graph database from Neo4j to Memgraph can be done seamlessly using a single Cypher query, thanks to Memgraph’s built-in migrate module. This approach eliminates the need for manual data exports and imports, making migration efficient and error-free.

The migrate module is available directly in the memgraph/memgraph-mage Docker image, allowing you to execute migration queries out of the box without additional installations.

Motivation for having the ready-made migration module

Many database migrations rely on exporting data to CSV files and then importing them into the target system. While this method is widely used, it has several drawbacks:

  • Time-consuming: Large datasets require significant time for export and re-import.
  • Manual intervention: CSV formatting issues often require additional preprocessing.

Benefits of direct migration from the source system

With Memgraph’s migrate module, you can stream data directly from Neo4j into Memgraph, bypassing the need for intermediate files:

  • Instant data transfer: No need to generate and handle large CSV files.
  • Automatic property mapping: Ensures seamless migration of node and relationship properties.
  • Cypher expressiveness: By streaming rows into Memgraph’s query engine, you can migrate data and recreate nodes and relationships in the same fashion
  • Efficient for large graphs: Can handle millions of nodes and relationships efficiently.
  • No downtime: Data can be transferred while Memgraph remains operational.

Prerequisites

  • Neo4j running on a specific Bolt port (e.g., bolt://localhost:7687)
  • Memgraph running on a different Bolt port (e.g., bolt://localhost:7688)
  • The migrate module available in the memgraph/memgraph-mage Docker image

Start Neo4j and Memgraph

If you are running Neo4j and Memgraph on the same server, ensure they are running on different ports:

# Start Neo4j (default port 7687)
# Start Memgraph on a different port (7688) with MAGE pre-installed
docker run -it --rm -p 7688:7687 memgraph/memgraph-mage

Migrate the entire graph

To migrate the entire graph from Neo4j to Memgraph, use the following Cypher query in Memgraph:

CALL migrate.neo4j(
    "MATCH (n)-[r]->(m) 
     RETURN properties(n) AS from_properties, 
            properties(r) AS edge_properties, 
            properties(m) AS to_properties", 
    {host: "localhost", port: 7687}) 
YIELD row 
RETURN row;
  • The MATCH (n)-[r]->(m) query selects all nodes and relationships.
  • The RETURN clause extracts node and relationship properties.
  • The {host: "localhost", port: 7687} parameter specifies the Neo4j instance to migrate from.
  • YIELD row RETURN row streams the data directly from Neo4j to Memgraph.

Migrate specific data

If you want to migrate only certain parts of the graph, use the following queries:

Migrate nodes with a specific label

CALL migrate.neo4j(":Person", {host: "localhost", port: 7687}) YIELD row RETURN row;

This migrates all nodes with the Person label.

Migrate relationships of a certain type

CALL migrate.neo4j("[:KNOWS]", {host: "localhost", port: 7687}) YIELD row RETURN row;

This migrates only relationships of type KNOWS.

Conclusion

Using Memgraph’s migrate module, you can efficiently migrate your graph data from Neo4j with a single Cypher query. Whether you are migrating the full dataset or specific labels/relationships, this method allows for seamless, real-time streaming without CSV exports. 🚀

Memgraph’s office hours

Schedule a 30 min session with one of our engineers to discuss how Memgraph fits with your architecture. Our engineers are highly experienced in helping companies of all sizes to integrate and get the most out of Memgraph in their projects. Talk to us about data modeling, optimizing queries, defining infrastructure requirements or migrating from your existing graph database. No nonsense or sales pitch, just tech.