0

How can I export and import a graph in Apache AGE?

I have a graph in one database that I need to move to another. Is there a method or tool available in Apache AGE that allows for exporting the graph from the source database and importing it into the target database?

I would greatly appreciate any insights, guidance, or recommendations on how to efficiently perform this data transfer.

Omar Saad
  • 349
  • 3
  • 8
  • There is an answer on a similar question [How to migrate from a relational database to a graph database using apache AGE.?](https://stackoverflow.com/a/75969133/18610676) and [How can I migrate an existing Neo4j graph database to Apache Age?](https://stackoverflow.com/a/76122711/18610676). Hope these will be helpful. – Zainab Saad Jun 04 '23 at 20:29

7 Answers7

1

Simply export the graph to CSV and import the CSV graph into AGE in another database.

Refer to the Neo4j documentation on how to export to CSV and to import into AGE, refer to this.

Tito
  • 289
  • 8
  • I want to export the graph from Apache AGE not from Neo4j. – Omar Saad Jun 05 '23 at 09:13
  • Cypher is basically a Neo4j graph query language so that will work for AGE. – Tito Jun 05 '23 at 09:56
  • Use `apoc.export.csv.query` or `all` to write the results to a CSV file. For example: `CALL apoc.export.csv.query("cypher query", "file.csv", {})` or `CALL apoc.export.csv.all("file.csv", {})` should work. – Tito Jun 05 '23 at 10:18
1

You can export the postgres table using COPY, using this command you can export the edges to a csv file:

COPY (SELECT * FROM cypher('graph_name', $$
    MATCH (a)-[e]->(b)
    RETURN start_id(e), label(a), end_id(e), label(b) $$)
    AS (start_id agtype, start_vertex_type agtype, end_id agtype, end_vertex_type agtype)
) TO '/path_to_csv/edges_graph.csv' DELIMITER ',' CSV HEADER;

To export nodes, you need to manually retrieve the properties and use the column list definition as the header. For example, if you have a person vertex with properties like id, name, and age, the code would look like this:

COPY (SELECT * FROM cypher('graph_name', $$
    MATCH (a)
    RETURN a.id, a.name, a.age $$)
    AS (id agtype, name agtype, age agtype)
) TO '/path_to_csv/person_vertex_graph.csv' DELIMITER ',' CSV HEADER;

More information about how to do it on here.

Finally, you can import from the csv file using the documentation. Here is an example for importing the Person nodes from csv:

SELECT create_vlabel('graph_name','Person');
SELECT load_labels_from_file('graph_name',
                             'Person',
                             'path_to_csv/person_vertex_graph.csv');
Wendel
  • 763
  • 1
  • 12
1

You can use the Apoc from neo4j to extract the data in the form of CSV file. and after you have exported you can import the data into the apache age.

Let's assume we have this movies dataset:

CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'})
CREATE (Keanu:Person {name:'Keanu Reeves', born:1964})
CREATE (Carrie:Person {name:'Carrie-Anne Moss', born:1967})
CREATE (Laurence:Person {name:'Laurence Fishburne', born:1961})
CREATE (Hugo:Person {name:'Hugo Weaving', born:1960})
CREATE (LillyW:Person {name:'Lilly Wachowski', born:1967})
CREATE (LanaW:Person {name:'Lana Wachowski', born:1965})
CREATE (JoelS:Person {name:'Joel Silver', born:1952})
CREATE
(Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrix),
(Carrie)-[:ACTED_IN {roles:['Trinity']}]->(TheMatrix),
(Laurence)-[:ACTED_IN {roles:['Morpheus']}]->(TheMatrix),
(Hugo)-[:ACTED_IN {roles:['Agent Smith']}]->(TheMatrix),
(LillyW)-[:DIRECTED]->(TheMatrix),
(LanaW)-[:DIRECTED]->(TheMatrix),
(JoelS)-[:PRODUCED]->(TheMatrix);

Now for exporting this data in csv format:

CALL apoc.export.csv.all("movies.csv", {})

We can use different functions from Neo4j like "apoc.export.csv.data" or "apoc.export.csv.query" to export data in the form of separate labels and edges files. Once we have the data in labels and edges form we can use that to import it in Apache Age.

Importing data in Apache Age:

We can load the labels from file using this command:

load_labels_from_file('<graph name>', 
                  '<label name>',
                  '<file path>', 
                  false)
#The fourth parameter is optional, use this only when you have no id field in file.

The CSV format for this file is as follows:

id - properties

The id column will be exempted if we have given the fourth parameter and it's false.

For loading relationships, this function will be used:

load_edges_from_file('<graph name>',
                '<label name>',
                '<file path>');

The CSV format for this file is as follows:

start_id - start_vertex_type - end_id - end_vertex_type - properties

Talha Munir
  • 121
  • 5
1

To export a graph to another database, You can export it to a CSV file the postgres COPY command can be used. Its syntax is like this.

COPY ( SELECT * FROM <table name> ) TO 'absolute/path/to/export.csv' WITH CSV HEADER;

You can refer to this similar issue on GitHub for more context.

To import a CSV file, the Apache AGE official documentation provides a detailed description.

1

GraphML is a well-known XML based format and can be used for representing graphs. It can be easily used in Apache AGE as well.

The steps to transfer data to Apache AGE using graphML are as follows:

  1. Use the Apache AGE CLI to connect the graph data to the source database using the command: ‘age-sh’.

  2. The graph data will be saved in a file by running the following cypher query:

    EXPORT GRAPH TO 'graph_export.graphml';

0

you have to export and import the database as the CSV format. Follow a complete tutorial on the AGE WEBSITE.

Marcos Silva
  • 115
  • 5
0

use pg_dump and pg_restore to export and import the entire database, including the graph data, between databases