Introducing Aerospike JDBC Driver
Tags: aerospike, nosql, jdbc, java
Published: 2021-03-15 14:24:49

One of the most common ways to interact with databases on JVM is the [JDBC API](https://docs.oracle.com/javase/8/docs/technotes/guides/jdbc/).

JDBC stands for Java Database Connectivity, which is a standard Java API for database-independent connectivity between the Java programming language and a wide range of databases.

Aerospike is a fast and durable No-SQL database. It has its own Java client, but this article will introduce you to a supplementary way of interacting with it using SQL.

Get yourself a hot cup of tea or coffee (for the true Java look and feel) and we will dive into the details of the Aerospike journey into the JDBC world.

## Introduction

The Aerospike JDBC driver requires Java 8 and is compliant with JDBC 4.2. Also, Aerospike server 5.2+ is required, because the driver uses the new [Filter expressions](https://docs.oracle.com/javase/8/docs/technotes/guides/jdbc/).

The first release of the JDBC driver supports the following SQL statements:
* SELECT
* INSERT
* UPDATE
* DELETE

You can also add WHERE conditions and LIMIT your queries. JOINS, ORDER BY and GROUP BY are not supported.
From the very beginning, the driver was designed to support operations that could be done using the regular Java client, without UDFs and other compute and memory hungry components. So, the original intention is to keep it small and easy to start, without workarounds to support features that aren’t native to the Aerospike Database.

The driver doesn’t support SQL functions as well as the Aerospike [collection data types (CDTs)](https://www.aerospike.com/docs/guide/cdt.html).

## Getting Started

Install the Aerospike JDBC driver and add the location of it to your classpath.
You can take the JAR file from the [releases](https://github.com/aerospike/aerospike-jdbc/releases), add a [Maven dependency](https://mvnrepository.com/artifact/com.aerospike/aerospike-jdbc), or build it from the [sources](https://github.com/aerospike/aerospike-jdbc#build).

The Aerospike JDBC driver is statically registered in the AerospikeDriver class. So the only thing required is to load this class.

```
Class.forName("com.aerospike.jdbc.AerospikeDriver").newInstance();
```

The next thing you’ll need to do is to specify the JDBC URL. The URL template is:

```
jdbc:aerospike:HOST[:PORT][/NAMESPACE][?PARAM1=VALUE1[&PARAM2=VALUE2]
```

For example the `jdbc:aerospike:localhost` URL will connect to the Aerospike database running on a local machine and listening on the default port (3000). The `jdbc:aerospike:172.17.0.5:3300/test` URL connects to the test namespace on the Aerospike database running on 172.17.0.5:3300.

After the initial setup let’s see a simple usage example of it:
```java
try {
    String url = "jdbc:aerospike:localhost:3000/test";
    Connection connection = DriverManager.getConnection(url);

    String query = "select * from ns1 limit 10";
    ResultSet resultSet = connection.createStatement().executeQuery(query);
    while (resultSet.next()) {
        String bin1 = resultSet.getString("bin1");
        System.out.println(bin1);
    }
} catch (Exception e) {
    System.err.println(e.getMessage());
}
```

## JDBC Client tools

You can browse and manipulate data in Aerospike with any of the available SQL client tools using the JDBC driver.

There are a number of multiplatform and free database tools available like [DBeaver](https://dbeaver.io/), [SQuirreL](http://squirrel-sql.sourceforge.net/), and others.

Here are the steps to configure the DBeaver SQL Browser with the Aerospike JDBC driver:
* Database -> Driver Manager -> New  
Fill in settings:
    * Driver Name: Aerospike
    * Driver Type: Generic
    * Class Name: `com.aerospike.jdbc.AerospikeDriver`
    * URL Template: `jdbc:aerospike:{host}[:{port}]/[{database}]`
    * Default Port: 3000
* Click the `Add File` button and add the JDBC jar file.
* Click the `Find Class` button.
* Click `OK`.
    
Create a connection:  
* Database -> New Database Connection
* Select `Aerospike` and click `Next`.
* Fill in the connection settings
    * Host and Port
    * Database/Schema: the namespace you are connecting to
    * Username and Password if you have security turned on in Aerospike Database Enterprise Edition
* Click `Finish`.

Now you can open an SQL editor and query your Aerospike cluster:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fqyf6cn99ardx9ve4513.png)

## Summary

The Aerospike JDBC driver is in its very early stages. It will be great if you could try it and give us some feedback. Any contributions to the [project](https://github.com/aerospike/aerospike-jdbc) are very welcome.

Check out my previous Aerospike SQL series if you haven’t done this yet.
* [Introducing Aerospike Connect for Presto](https://www.aerospike.com/blog/introducing-aerospike-connect-for-presto/)
* [Aerospike Data Browser](https://medium.com/aerospike-developer-blog/aerospike-data-browser-c70c05b34a5f)

And don’t forget to subscribe to the Aerospike developer blog to get updated with our latest news.