DEV Community

Humza Tareen
Humza Tareen

Posted on

2

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

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry 🕒

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay