Introduction to Apache AGE
Tags: apacheage, beginners, database, postgres
Published: 2023-08-18 18:44:47

## **What is Apache AGE?**
The [Apache AGE](https://age.apache.org/) is a PostgreSQL extension that provides graph database functionality. Therefore, it encompasses the Postgres functionalities while incorporating Cypher for graph management. This unique combination empowers you with access to advanced graph query modeling within the framework of your relational database infrastructure.

## **Definitions**
**Vertex (node)**
It serves as a container for storing entity-related data, potentially encompassing multiple properties. Notably, each vertex can accommodate a variety of properties, yet it is restricted to possessing a singular label designation.
**Edge (relationship)**
An edge links nodes in a graph, defining a distinct relationship or interaction with defining attributes. This is vital for navigating intricate interconnections in the graph data model.
**Path**
The path represents a sequence of connected edges and nodes. It captures the route or journey between entities, enabling the exploration of relationships and patterns within the graph.

## **Common Clauses using Cypher**
**CREATE**: used to add new nodes and edges to the graph.
**DELETE**:  is used to remove nodes, edges, or entire paths from the graph.
**MATCH**: used to retrieve data from the graph database. It allows you to specify patterns of nodes and edges, facilitating the discovery of interconnected entities and relationships.
**MERGE**: allows you to either create new nodes and edges or match and update existing ones based on specified patterns.
**REMOVE**: used to remove properties from vertex and edges.
**RETURN**: specify what data you want to retrieve as a result of your query. It allows you to define the attributes or properties of nodes and edges that you're interested in.
**SET**: used to update properties of nodes or edges within the graph.
**WHERE**: filters the results of a query based on specified conditions. It helps you narrow down your search and retrieve only the data that meets specific criteria.

## **Examples**
Create a vertex without label and returns it
```
SELECT * FROM cypher('graph_name', $$ 
    CREATE (v) 
    RETURN v
$$) AS (vertex agtype);
```
Returns all vertices
```
SELECT * FROM cypher('graph_name', $$ 
    MATCH (v) 
    RETURN v
$$) AS (vertex agtype);
```
Create a vertex with a property
```
SELECT * FROM cypher('graph_name', $$ 
    CREATE (v {property: 'value'})
    RETURN v
$$) AS (vertex agtype);
```
Return the vertex with the specified value
```
SELECT * FROM cypher('graph_name', $$ 
    MATCH (v {property: 'value'}) 
    RETURN v
$$) AS (vertex agtype);
```
Return the vertex with the specified value using WHERE
```
SELECT * FROM cypher('graph_name', $$ 
    MATCH (v)
    WHERE v.property = 'value'
    RETURN v
$$) AS (vertex agtype);
```
Delete all vertices
```
SELECT * FROM cypher('graph_name', $$ 
    MATCH (v) 
    DELETE v
$$) AS (vertex agtype);
```
Create a vertex with a label and properties
```
SELECT * FROM cypher('graph_name', $$ 
    CREATE (v:Label {property1: 1, property2: 'value', property3: true}) 
    RETURN v
$$) AS (vertex agtype);
```
Get all vertices with a specified label
```
SELECT * FROM cypher('graph_name', $$ 
    MATCH (v:Label) 
    RETURN v
$$) AS (vertex agtype);
```
Get the vertex with the label and change the value of its property
```
SELECT * FROM cypher('graph_name', $$ 
    MATCH (v:Label)
    SET v.property1 = 0
    RETURN v
$$) AS (vertex agtype);
```
Remove the third property
```
SELECT * FROM cypher('graph_name', $$ 
    MATCH (v:Label)
    REMOVE v.property3
    RETURN v
$$) AS (vertex agtype);
```
Creating a new vertex and a relationship with our already existing node, returning the created path.
```
SELECT * FROM cypher('graph_name', $$ 
    MERGE p=(x:Label)-[r:relationship]->(y:newNode)
    RETURN p
$$) AS (path agtype);
```

## References and manual
[Apache AGE documentation](https://age.apache.org/age-manual/master/index.html)
[Apache AGE repository](https://github.com/apache/age)
[Apache AGE website](https://age.apache.org)