Quickstart

Here we’ll briefly tour basic features of ADBC with the PostgreSQL driver for Java.

Installation

To include ADBC in your Maven project, add the following dependency:

<dependency>
    <groupId>org.apache.arrow.adbc</groupId>
    <artifactId>adbc-driver-jdbc</artifactId>
    <version>${adbc.version}</version>
</dependency>

For the examples in this section, the following imports are required:

import org.apache.arrow.adbc.core.AdbcConnection;
import org.apache.arrow.adbc.core.AdbcDatabase;
import org.apache.arrow.adbc.core.AdbcDriver;
import org.apache.arrow.adbc.core.AdbcException;
import org.apache.arrow.adbc.core.AdbcStatement;

JDBC-style API

ADBC provides a high-level API in the style of the JDBC standard.

Usage

final Map<String, Object> parameters = new HashMap<>();
parameters.put(AdbcDriver.PARAM_URL, "jdbc:postgresql://localhost:5432/postgres");
try (
    BufferAllocator allocator = new RootAllocator();
    AdbcDatabase db = new JdbcDriver(allocator).open(parameters);
    AdbcConnection adbcConnection = db.connect();
    AdbcStatement stmt = adbcConnection.createStatement()

) {
    stmt.setSqlQuery("select * from foo");
    AdbcStatement.QueryResult queryResult = stmt.executeQuery();
    while (queryResult.getReader().loadNextBatch()) {
        // process batch
    }
}  catch (AdbcException e) {
    // throw
}

In application code, the connection must be closed after usage or memory may leak. It is recommended to wrap the connection in a try-with-resources block for automatic resource management. In this example, we are connecting to a PostgreSQL database, specifically the default database “postgres”.

Note that creating a statement is also wrapped in the try-with-resources block. Assuming we have a table “foo” in the database, an example for setting and executing the query is also provided.