Getting Started with AGE and JDBC Driver - Setting up the driver
Published: 2023-02-17 16:37:16

Apache AGE is a powerful extension for PostgreSQL that allows users to integrate graph database capabilities with their existing relational database infrastructure. The name "AGE" stands for A Graph Extension and is based on Bitnine's AgensGraph, which is a multi-model database built on top of PostgreSQL. By using AGE, users can store both relational and graph data models in a single storage system, and they can utilize standard ANSI SQL in combination with openCypher, which is one of the most widely-used graph query languages in the industry today. This makes it easy for developers and data analysts to work with complex and interconnected data structures, enabling them to gain deeper insights and make more informed decisions based on the data.

You can find more information about Apache AGE on following resources

- https://age.apache.org/
- https://github.com/apache/age

Before continuing with this post, it is assumed that you have already set up Apache AGE and PostgreSQL on your system. This includes installing the PostgreSQL database server and the AGE extension for PostgreSQL. If you have not yet completed these steps, it is recommend doing so before proceeding, as they are essential prerequisites for working with the JDBC driver and AGE. Once you have set up your PostgreSQL and AGE environment, you can follow the steps in this post to set up the JDBC driver and start working with graph data in your Java applications.

## AGE JDBC Driver

Apache AGE has its own JDBC driver that enables users to 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. With the AGE JDBC driver, users can leverage the full power of AGE in their Java applications, making it easier to build and deploy robust, graph-enabled solutions.

## Setting up the driver
This post is all about setting up your environment to use the JDBC driver with AGE. This will guide you through the necessary steps to get started.

### Required JAR files
For setting up the environment for using JDBC driver with AGE, it is important to have the following jar files and packages. These are essential prerequisites for getting started.

- [gradle build tool](https://gradle.org/install/)
- [postgres JDBC driver](https://jdbc.postgresql.org/download/)
- [antlr4-4.9.2-complete](https://repo1.maven.org/maven2/org/antlr/antlr4/4.9.2/)
- [common-lang3](https://repo1.maven.org/maven2/org/antlr/antlr4/4.9.2/)
- [commons-text-1.6](http://www.java2s.com/ref/jar/download-commonstext16jar-file.html)

### Cloning the AGE repository
To build the JAR file for the AGE JDBC driver, you will need 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. If you have already AGE installed using source code, you can skip this as you will also have the JDBC driver in the same repository. To clone the repository, simply run the following command in your terminal or command prompt
```
git clone https://github.com/apache/age.git
```
Now move to the driver directory
```
cd age/drivers/jdbc
```

### Building the jar file
To assemble and build the JAR file for the AGE JDBC driver, run the following Gradle command in the project directory. This command will compile the source code and create a JAR file that you can use in your Java applications:
```
gradle assemble
```
If you face any errors at this stage, that would be probably because of gradle installation. Make sure you have installed the gradle correctly.

After the build completes successfully, a jar file will be created at path age/drivers/jdbc/lib/build/libs/lib.jar. 

### Adding jar files to your project
Now add all the jar files, the one created by gradle in last step and other jar files that you downloaded to your project. In VS Code, you can add the jar files to your project by adding files to referenced libraries (this works if you have installed JAVA extension in VS Code).

To use the AGE JDBC driver in your Java project, you need to add all the required JAR files, including the AGE JDBC driver JAR file created by Gradle, and all other jar files that you downloaded to your project.

In Visual Studio Code, you can add these JAR files to your project by adding them to the "Referenced Libraries" folder. This works if you have installed the Java extension in Visual Studio Code.

This will make your project ready to connect to AGE database. In the next post, we will dive deeper and test the AGE JDBC driver that we have set up in this post. We will create a sample program that showcases how to convert the graph database data into Java objects.