Create a Data Access Object for Databox Data using 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 Databox integrates connectivity to live Databox data in Java applications. By pairing these technologies, you gain simple, programmatic access to Databox data. This article explains how to build a basic Data Access Object (DAO) and the accompanying code to read Databox data.
Create a DAO for the Databox Datasets 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 MyDatasetsDAO {
//request specific data from Databox (String type is used for simplicity)
@SqlQuery("SELECT Title FROM Datasets WHERE DataSourceId = :dataSourceId")
String findTitleByDataSourceId(@Bind("dataSourceId") String dataSourceId);
/*
* close with no args is used to close the connection
*/
void close();
}
Open a Connection to Databox
Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Databox.
Start by setting the Profile connection property to the location of the Databox Profile on disk (e.g. C:\profiles\Databox.apip). Next, set the ProfileSettings connection property to the connection string for Databox (see below).
Databox API Profile Settings
To use the Databox API, you need to generate a personal access key from your Databox account. Navigate to the Databox account settings under Data Manager and generate an API key (prefixed with pak_).
After setting the following connection properties, you are ready to connect:
- AuthScheme: Set this to APIKey.
- APIKey: Set this to your Databox personal access key.
Built-in Connection String Designer
For assistance in constructing the JDBC URL, use the connection string designer built into the Databox JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.
java -jar cdata.jdbc.api.jar
Fill in the connection properties and copy the connection string to the clipboard.
A connection string for Databox will typically look like the following:
jdbc:api:Profile=C:\profiles\Databox.apip;AuthScheme=APIKey;ProfileSettings='APIKey=your_api_key';
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:api:Profile=C:\profiles\Databox.apip;AuthScheme=APIKey;ProfileSettings='APIKey=your_api_key';");
MyDatasetsDAO dao = dbi.open(MyDatasetsDAO.class);
//do stuff with the DAO
dao.close();
Read Databox Data
With the connection open to Databox, simply call the previously defined method to retrieve data from the Datasets entity in Databox.
//disply the result of our 'find' method
String title = dao.findTitleByDataSourceId("4976164");
System.out.println(title);
Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for Databox by integrating with the CData JDBC Driver for Databox. Download a free trial and work with live Databox data in custom Java applications today.