DEV Community

Emmanuel Allison
Emmanuel Allison

Posted on

Useful Apache AGE Functions

Apache AGE is an extension for PostgreSQL which adds graph database functionalities to the relational database. Here are some AGE functions you should know.

create_graph

Creates a new graph.

-- You can write it this way.
SELECT create_graph('cities');
-- Or this way.
SELECT * FROM create_graph('cities', true);
Enter fullscreen mode Exit fullscreen mode

drop_graph

Deletes a graph. The optional third parameter indicates if the delete operation should be cascaded (forced). If it is, all relations dependent on the graph will be deleted too.

-- You can write it this way.
SELECT drop_graph('cities', true);
-- Or this way.
SELECT * FROM drop_graph('cities', true);
Enter fullscreen mode Exit fullscreen mode

alter_graph

Use this to rename you graphs.

-- Renames the graph 'CITIES' to 'cities'.

-- You can write it this way.
SELECT alter_graph('CITIES', 'RENAME', 'cities');
-- Or this way.
SELECT * FROM alter_graph('CITIES', 'RENAME', 'cities');
Enter fullscreen mode Exit fullscreen mode

create_vlabel

Creates a new vertex label in the given graph.

SELECT * FROM create_vlabel('cities', 'City');
Enter fullscreen mode Exit fullscreen mode

create_elabel

Creates a new edge label in the given graph.

SELECT * FROM create_elabel('cities', 'CONNECTED');
Enter fullscreen mode Exit fullscreen mode

_label_Id

Returns the ID of the label of an edge or vertex.

SELECT * FROM _label_id('cities', 'CONNECTED');
(1 row)
Enter fullscreen mode Exit fullscreen mode

drop_label

Drops an edge's or vertex's label. The optional third parameter indicates if the delete operation should be cascaded (forced). If it is, all relations dependent on the label will be deleted too.

SELECT drop_label('cities', 'City', true);
Enter fullscreen mode Exit fullscreen mode

cypher

With the cypher() function, you can execute Cypher queries in a graph.

SELECT * FROM cypher('cities', $$
  CREATE (:City {name: 'New York', size_in_km: 783.8})
$$) as (v agtype);
Enter fullscreen mode Exit fullscreen mode

get_cypher_keywords

Returns a list of Cypher grammar keywords.

SELECT * FROM get_cypher_keywords();
Enter fullscreen mode Exit fullscreen mode

create_complete_graph

There are times you may need a graph in which all vertices are interconnected (that is, there is an edge between all vertices). This type of graph is called a complete graph, and this function can help you accomplish that quickly. All you need to do is supply the name of the new graph to be created, the number of vertices you want, the label of the edges and the label of the vertices.

-- You can write it this way.
SELECT create_complete_graph('cities', 5, 'CONNECTED', 'City');
-- Or this way.
SELECT * FROM create_complete_graph('cities', 5, 'CONNECTED', 'City');
Enter fullscreen mode Exit fullscreen mode

Using Apache AGE Viewer, we can see the newly created complete graph.

A complete graph

I hope these will come in handy for you. You can learn more about Apache AGE in the official website and read its documentation here.

Top comments (0)