DEV Community

WALEED SHAHID
WALEED SHAHID

Posted on

Basics for Querying Apache Age

Apache Age is a graph database that uses Apache Arrow as a storage layer. It provides an easy way to store, manage and query graph data. In this blog, we will be discussing the basics of writing Cypher queries in Apache Age.

Cypher is a declarative query language used for querying and manipulating graph data. It is used in Apache Age, a graph database built on top of PostgreSQL. Cypher queries are used to interact with graph data, which is represented as nodes and edges.

To get started with writing Cypher queries in Apache Age, we can create a graph database and add some data to it.

Before we dive into the queries, let's understand some terminologies that are used in Apache Age.

Vertex: A vertex represents an entity in a graph. For example, a person, a place, a thing, etc.

Edge: An edge represents a relationship between two vertices.
Label: A label is a way of grouping vertices based on their characteristics.

Property: A property is a key-value pair that can be associated with a vertex or an edge.

Now, let's write some basic Cypher queries in Apache Age.

Creating a Vertex:
To create a vertex in Apache Age, we use the CREATE clause. The following query creates a vertex labeled as Person and adds two properties to it.

CREATE (:Person {name: 'John Doe', age: 30})
Enter fullscreen mode Exit fullscreen mode

Creating Multiple Vertices:
We can create multiple vertices using a single query. The following query creates four vertices labeled as Person and adds two properties to each vertex.

CREATE (:Person {name: 'John Doe', age: 30}),
       (:Person {name: 'Jane Doe', age: 25}),
       (:Person {name: 'Bob Smith', age: 45}),
       (:Person {name: 'Alice Smith', age: 40})

Enter fullscreen mode Exit fullscreen mode

Creating a Relationship:
To create a relationship between two vertices, we use the CREATE clause along with the MATCH clause. The MATCH clause is used to match the vertices that we want to create a relationship between. The following query creates a relationship between two vertices labeled as Person.

MATCH (p1:Person), (p2:Person)
WHERE p1.name = 'John Doe' AND p2.name = 'Jane Doe'
CREATE (p1)-[:FRIENDS]->(p2)
Enter fullscreen mode Exit fullscreen mode

Creating a Labeled Vertex:
To create a vertex with a label, we use the CREATE clause and specify the label along with the properties. The following query creates a vertex labeled as Country and adds a name property to it.

CREATE (:Country {name: 'USA'})
Enter fullscreen mode Exit fullscreen mode

Creating a Relationship with Labeled Vertices:
To create a relationship between two labeled vertices, we use the CREATE clause along with the MATCH clause. The MATCH clause is used to match the vertices that we want to create a relationship between. The following query creates a relationship between a Person and a Country vertex.

MATCH (p:Person), (c:Country)
WHERE p.name = 'John Doe' AND c.name = 'USA'
CREATE (p)-[:BORN_IN]->(c)
Enter fullscreen mode Exit fullscreen mode

Conclusion:
In this blog, we have discussed the basics of writing Cypher queries in Apache Age. We have learned how to create vertices, relationships, labeled vertices, and relationships between labeled vertices. These are the basic building blocks of any graph database. With these basic queries, you can start building your own graph database and perform more complex operations on it.

Top comments (0)