Ready to get started?

Learn more about the CData JDBC Driver for Presto or download a free trial:

Download Now

Create a Data Access Object for Presto Data using JDBI

A brief overview of creating a SQL Object API for Presto data in JDBI.

JDBI is a SQL convenience library for Java that exposes two different style APIs, a fluent style and a SQL object style. The CData JDBC Driver for Presto integrates connectivity to live Presto data in Java applications. By pairing these technologies, you gain simple, programmatic access to Presto data. This article walks through building a basic Data Access Object (DAO) and the accompanying code to read and write Presto data.

Create a DAO for the Presto Customer Entity

The interface below declares the desired behavior for the SQL object to create a single method for each SQL statement to be implemented.

public interface MyCustomerDAO { //insert new data into Presto @SqlUpdate("INSERT INTO Customer (Id, LastName) values (:id, :lastName)") void insert(@Bind("id") String id, @Bind("lastName") String lastName); //request specific data from Presto (String type is used for simplicity) @SqlQuery("SELECT LastName FROM Customer WHERE Id = :id") String findLastNameById(@Bind("id") String id); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Presto

Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Presto.

Set the Server and Port connection properties to connect, in addition to any authentication properties that may be required.

To enable TLS/SSL, set UseSSL to true.

Authenticating with LDAP

In order to authenticate with LDAP, set the following connection properties:

  • AuthScheme: Set this to LDAP.
  • User: The username being authenticated with in LDAP.
  • Password: The password associated with the User you are authenticating against LDAP with.

Authenticating with Kerberos

In order to authenticate with KERBEROS, set the following connection properties:

  • AuthScheme: Set this to KERBEROS.
  • KerberosKDC: The Kerberos Key Distribution Center (KDC) service used to authenticate the user.
  • KerberosRealm: The Kerberos Realm used to authenticate the user with.
  • KerberosSPN: The Service Principal Name for the Kerberos Domain Controller.
  • KerberosKeytabFile: The Keytab file containing your pairs of Kerberos principals and encrypted keys.
  • User: The user who is authenticating to Kerberos.
  • Password: The password used to authenticate to Kerberos.

Built-in Connection String Designer

For assistance in constructing the JDBC URL, use the connection string designer built into the Presto JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.

java -jar cdata.jdbc.presto.jar

Fill in the connection properties and copy the connection string to the clipboard.

A connection string for Presto will typically look like the following:

jdbc:presto:Server=127.0.0.1;Port=8080;

Use the configured JDBC URL to obtain an instance of the DAO interface. The particular method shown below will open a handle bound to the instance, so the instance needs to be closed explicitly to release the handle and the bound JDBC connection.

DBI dbi = new DBI("jdbc:presto:Server=127.0.0.1;Port=8080;"); MyCustomerDAO dao = dbi.open(MyCustomerDAO.class); //do stuff with the DAO dao.close();

Read Presto Data

With the connection open to Presto, simply call the previously defined method to retrieve data from the Customer entity in Presto.

//disply the result of our 'find' method String lastName = dao.findLastNameById("123456789"); System.out.println(lastName);

Write Presto Data

It is also simple to write data to Presto, using the previously defined method.

//add a new entry to the Customer entity dao.insert(newId, newLastName);

Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for Presto by integrating with the CData JDBC Driver for Presto. Download a free trial and work with live Presto data in custom Java applications today.