DEV Community

Talha Munir 🇵🇸
Talha Munir 🇵🇸

Posted on

Importing graph from files

In this blog we will discuss on how you can import graph from different files in Apache age.

A user can load graphs in 2 steps:

  1. First step will involve loading the vertices.
  2. and in second step you will load the edges.

Note: A user must create graph and labels before loading data from different files.

Load Graph functions:

The below function are used to create vertices and edges from the file.

load_labels_from_file:

The function load_labels_from_file is used for the loading of vertices from the csv files.

Syntax:

load_labels_from_file('<graph name>', 
                      '<label name>',
                      '<file path>')
Enter fullscreen mode Exit fullscreen mode

load_edges_from_file:

The function load_edges_from_file can be used to load the edges from the CSV file.

Syntax:

load_edges_from_file('<graph name>', 
                      '<label name>',
                      '<file path>')
Enter fullscreen mode Exit fullscreen mode

About the CSV format:

Below is a brief explanation about the csv file for vertices and edges:

  • A csv file for nodes should look like as follows(Field names and with their brief description):

id: it should be the first column and all the values should be the positive integers. This is an optional field when id_field_exists is false. However it should be present when id_field_exists is not set to false.

properties: All other columns should contain the properties of the node while the header should contain the name of the property.

  • Similarly, the csv file for the edges should be formatted as follows(Field names and with their brief description):

start_id: node id of the node from where the edge is stated. The id should be present in nodes.csv file.

start_vertex_type: This field specifies the class of the node.

end_id: The field indicated the end id of the node where the
edge will be terminated.

end_vertex_type: Class of the node.

properties: Contains different properties of the edge, the header must contain the name of the property.

A short tutorial:

Following are some of the SQL commands for tutorial:

  • Loading and creating graphs"
LOAD 'age';

SET search_path TO ag_catalog;
SELECT create_graph('test_graph');
Enter fullscreen mode Exit fullscreen mode
  • Create Label country:
SELECT create_vlabel('test_graph','Country');
Enter fullscreen mode Exit fullscreen mode
  • Load vertices from the csv file:
SELECT load_labels_from_file('test_graph',
                             'Country',
                             'age_load/data/countries.csv');
Enter fullscreen mode Exit fullscreen mode
  • Create label city:
SELECT create_vlabel('test_graph','City');
Enter fullscreen mode Exit fullscreen mode
  • Load vertices from the csv file:
SELECT load_labels_from_file('test_graph',
                             'City', 
                             'age_load/data/cities.csv');
Enter fullscreen mode Exit fullscreen mode
  • Create label has_city:
SELECT create_elabel('agload_test_graph','has_city');
Enter fullscreen mode Exit fullscreen mode
  • Load edges from the csv file:
SELECT load_edges_from_file('test_graph', 'has_city',
     'age_load/data/edges.csv');
Enter fullscreen mode Exit fullscreen mode
  • Querying to see if the graph is loaded properly:
SELECT table_catalog, table_schema, table_name, table_type
FROM information_schema.tables
WHERE table_schema = 'test_graph';

SELECT COUNT(*) FROM test_graph."Country";
SELECT COUNT(*) FROM test_graph."City";
SELECT COUNT(*) FROM test_graph."has_city";

SELECT COUNT(*) FROM cypher('test_graph', $$MATCH(n) RETURN n$$) as (n agtype);
SELECT COUNT(*) FROM cypher('test_graph', $$MATCH (a)-[e]->(b) RETURN e$$) as (n agtype);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)