Create a Data Access Object for Rootly Data using JDBI

Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
A brief overview of creating a SQL Object API for Rootly 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 Rootly integrates connectivity to live Rootly data in Java applications. By pairing these technologies, you gain simple, programmatic access to Rootly data. This article explains how to build a basic Data Access Object (DAO) and the accompanying code to read Rootly data.

Create a DAO for the Rootly Incidents 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 MyIncidentsDAO {
  //request specific data from Rootly (String type is used for simplicity)
  @SqlQuery("SELECT  FROM Incidents WHERE Status = :status")
  String findByStatus(@Bind("status") String status);

  /*
   * close with no args is used to close the connection
   */
  void close();
}

Open a Connection to Rootly

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

Using API Key Authentication

To authenticate using an API key, you will need to obtain your API key from your Rootly account settings.

To get your API key:

  1. Log in to your Rootly account
  2. Navigate to Settings > API & Integrations
  3. Click on "API Tokens"
  4. Copy the generated token

After setting the following connection properties, you are ready to connect:

  • AuthScheme: Set this to APIKey.
  • APIKey: Set this to your Rootly API token.

Example Connection String

Profile=Rootly.apip;Authscheme=APIKey;ProfileSettings="APIKey=your_apikey";

Built-in Connection String Designer

For assistance in constructing the JDBC URL, use the connection string designer built into the Rootly 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 Rootly will typically look like the following:

jdbc:api:Profile=Rootly.apip;Authscheme=APIKey;ProfileSettings="APIKey=your_apikey";

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=Rootly.apip;Authscheme=APIKey;ProfileSettings="APIKey=your_apikey";");
MyIncidentsDAO dao = dbi.open(MyIncidentsDAO.class);

//do stuff with the DAO

dao.close();

Read Rootly Data

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

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

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

Ready to get started?

Connect to live data from Rootly with the API Driver

Connect to Rootly