DEV Community

Humza Tareen
Humza Tareen

Posted on

How to use the Apache AGE's Cypher Query Language

The examples provided in the documentation demonstrate how to use the Apache AGE's Cypher Query Language to interact with graph data stored in PostgreSQL. Here are some examples:

  • Get all vertices: This query returns all vertices in the graph.
SELECT * FROM cypher('graph_name', $$
MATCH (v)
RETURN v
$$) as (v agtype);
Enter fullscreen mode Exit fullscreen mode

The output will be something like:

{id: 0; label: ‘Person’; properties: {name: ‘Charlie Sheen’}}::vertex   
{id: 1; label: ‘Person’; properties: {name: ‘Martin Sheen’}}::vertex   
{id: 2; label: ‘Person’; properties: {name: ‘Michael Douglas’}}::vertex   
{id: 3; label: ‘Person’; properties: {name: ‘Oliver Stone’}}::vertex   
{id: 4; label: ‘Person’; properties: {name: ‘Rob Reiner’}}::vertex   
{id: 5; label: ‘Movie’; properties: {name: ‘Wall Street’}}::vertex   
{id: 6; label: ‘Movie’; properties: {title: ‘The American President’}}::vertex
Enter fullscreen mode Exit fullscreen mode
  • Get all vertices with a label: This query returns all vertices with a specific label. In this case, it returns all movies in the database.
SELECT * FROM cypher('graph_name', $$
MATCH (movie:Movie)
RETURN movie.title
$$) as (title agtype);
Enter fullscreen mode Exit fullscreen mode

The output will be something like:

title  
‘Wall Street’   
‘The American President’
Enter fullscreen mode Exit fullscreen mode
  • Find related vertices: This query returns all vertices related to a specific vertex, regardless of the type or direction of the edge. Here, it returns all the movies directed by 'Oliver Stone'.
SELECT * FROM cypher('graph_name', $$
MATCH (director {name: 'Oliver Stone'})-[]-(movie)
RETURN movie.title
$$) as (title agtype);
Enter fullscreen mode Exit fullscreen mode

The output will be something like:

title  
‘Wall Street’
Enter fullscreen mode Exit fullscreen mode
  • Find vertices connected by a specific type of edge: This query finds all vertices connected by a specific type of edge. It returns all actors that acted in 'Wall Street'.
SELECT * FROM cypher('graph_name', $$
MATCH (:Movie {title: 'Wall Street'})<-[:ACTED_IN]-(actor)
RETURN actor.name
$$) as (actors_name agtype);
Enter fullscreen mode Exit fullscreen mode

The output will be something like:

actors_name  
‘Charlie Sheen’   
‘Martin Sheen’   
‘Michael Douglas’
Enter fullscreen mode Exit fullscreen mode

References

Top comments (0)