Ready to get started?

Download a free trial of the Phoenix Driver to get started:

 Download Now

Learn more:

Apache Phoenix Icon Phoenix JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with HBase through Apache Phoenix.

Create a Data Access Object for Phoenix Data using JDBI



A brief overview of creating a SQL Object API for Phoenix 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 Phoenix integrates connectivity to live Phoenix data in Java applications. By pairing these technologies, you gain simple, programmatic access to Phoenix data. This article walks through building a basic Data Access Object (DAO) and the accompanying code to read Phoenix data.

Create a DAO for the Phoenix MyTable 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 MyMyTableDAO { //request specific data from Phoenix (String type is used for simplicity) @SqlQuery("SELECT Column1 FROM MyTable WHERE Id = :id") String findColumn1ById(@Bind("id") String id); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Phoenix

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

Connect to Apache Phoenix via the Phoenix Query Server. Set the Server and Port (if different from the default port) properties to connect to Apache Phoenix. The Server property will typically be the host name or IP address of the server hosting Apache Phoenix.

Authenticating to Apache Phoenix

By default, no authentication will be used (plain). If authentication is configured for your server, set AuthScheme to NEGOTIATE and set the User and Password properties (if necessary) to authenticate through Kerberos.

Built-in Connection String Designer

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

java -jar cdata.jdbc.apachephoenix.jar

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

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

jdbc:apachephoenix:Server=localhost;Port=8765;

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:apachephoenix:Server=localhost;Port=8765;"); MyMyTableDAO dao = dbi.open(MyMyTableDAO.class); //do stuff with the DAO dao.close();

Read Phoenix Data

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

//disply the result of our 'find' method String column1 = dao.findColumn1ById("123456"); System.out.println(column1);

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