DEV Community

Namsi Lydia
Namsi Lydia

Posted on

Comprehensive Guide :Getting Started with Apache Age In Java

As we all have come to learn Apache Age is a force to reckon with in the realm of data analysis and management .Apache age as we all know is a postgresql extension which it's capabilities is to integrate graph database capabilities into the familiar and robust PostgreSQL environment, empowering users to harness the power of graphs without sacrificing the familiarity and performance of PostgreSQL.

In this article we are going to describe the sample template guide on how to get started with apache age using programming language such as java.

Prerequisite needed to get started with Apache Age in java

  • Postgresql- Install the postgresql version of your choice from 12 - higher
  • Java Development Kit -Install Jdk 8 or higher
  • Apache Age- Install the apache age extension version that is compatible to you postgresql

It is recommended that you install the above tools, as they are essential prerequisites for working with the JDBC driver and AGE. Once you have set up your PostgreSQL , jdk and AGE environment, you can follow the steps that are going to be listed below to set up the JDBC driver and start working with graph data in your Java applications.

Next we are going to install the jdbc driver.The apache age jdbc driver enables users connect to an AGE database from a Java application. This JDBC driver provides a standard interface for accessing and manipulating the data stored in an AGE database, allowing developers to work with both relational and graph data models using the familiar JDBC API.

1. Setting up the jdbc driver
To be able to set up the environment for jdbc to be used with apache age it's important that the following jar files and packages are installed and they include:

  • gradle build tool
  • postgres JDBC driver
  • antlr4-4.9.2-complete
  • common-lang3
  • commons-text-1.6

Nb:ensure you unzip the jars if they are zipped before using them

2.Build from source
After you have unzipped the jar files next we are going to clone the official Apache AGE JDBC repository. The repository contains the necessary source code and build scripts for compiling the driver and creating a JAR file that you can use in your Java applications and this can be done with the following command:

git clone https://github.com/apache/age.git
cd age/drivers/jdbc

gradle assemble
Enter fullscreen mode Exit fullscreen mode

After the build completes successfully, a jar file will be created at path age/drivers/jdbc/lib/build/libs/lib.jar. Now add this JAR file to classpath for your java project.

3.Connect to the postgresql database and create graph
to connect to the postgresql database in java we can implement the following code to create a connection

import org.apache.age.jdbc.base.Agtype;
import org.postgresql.jdbc.PgConnection;

import java.sql.*;

public class studentDb {
    static final String DB_URL = "jdbc:postgresql://localhost:5432/students-demo";
    static final String USER = "postgres";
    static final String PASS = "pass";

    public static void main(String[] args) {

        // Open a connection
        try {

            PgConnection connection = DriverManager.getConnection(DB_URL, USER, PASS).unwrap(PgConnection.class);
            connection.addDataType("agtype", Agtype.class);

            // configure AGE
            Statement stmt = connection.createStatement();
            stmt.execute("CREATE EXTENSION IF NOT EXISTS age;");
            stmt.execute("LOAD 'age'");
            stmt.execute("SET search_path = ag_catalog, \"$user\", public;");

            // Run cypher
            ResultSet rs = stmt.executeQuery("SELECT * from cypher('stud_graph', $$ MATCH (n) RETURN n $$) as (n agtype);");

            while (rs.next()) {

                // Returning Result as Agtype
                Agtype returnedAgtype = rs.getObject(1, Agtype.class);

                String nodeLabel = returnedAgtype.getMap().getObject("label").toString();
                String nodeProp =  returnedAgtype.getMap().getObject("properties").toString();

                System.out.println("Vertex : " + nodeLabel + ", \tProps : " + nodeProp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Enter fullscreen mode Exit fullscreen mode

The above code will create a connection to the postgresql database ,configure AGE and finally execute a cypher query on the graph database.

This is just a sample example on how one can get started with Apache Age in java. this can be your starting guide on how to implement Apache Age in your java applications. In the next articles we will explore more how Apache Age can be utilised in Java with various use cases.

Top comments (0)