Create a Data Access Object for Apache Airflow Data using JDBI

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

Create a DAO for the Apache Airflow DagRuns 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 MyDagRunsDAO {
  //request specific data from Apache Airflow (String type is used for simplicity)
  @SqlQuery("SELECT State FROM DagRuns WHERE DagId = :dagId")
  String findStateByDagId(@Bind("dagId") String dagId);

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

Open a Connection to Apache Airflow

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

Start by setting the Profile connection property to the location of the ApacheAirflow Profile on disk (e.g. C:\profiles\ApacheAirflow.apip). Next, set the ProfileSettings connection property to the connection string for ApacheAirflow (see below).

ApacheAirflow API Profile Settings

Apache Airflow 3 uses JWT Bearer tokens for API authentication. You can generate a token from the Airflow web UI under Settings or via the Airflow CLI using airflow users create and the /api/v2/auth/token endpoint. Note that this profile targets Apache Airflow 3.x using the /api/v2 REST API. The legacy /api/v1 endpoint used by Airflow 2.x is not supported.

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

  • AuthScheme: Set this to APIKey.
  • APIKey: Set this to your Apache Airflow JWT Bearer token.
  • Server: Set this to the base URL of your Airflow instance (e.g. http://localhost:8080).

Built-in Connection String Designer

For assistance in constructing the JDBC URL, use the connection string designer built into the Apache Airflow 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.

Using the built-in connection string designer to generate a JDBC URL (Salesforce is shown.)

A connection string for Apache Airflow will typically look like the following:

jdbc:api:Profile=C:\profiles\ApacheAirflow.apip;AuthScheme=APIKey;ProfileSettings='APIKey=your_jwt_token;Server=http://localhost: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:api:Profile=C:\profiles\ApacheAirflow.apip;AuthScheme=APIKey;ProfileSettings='APIKey=your_jwt_token;Server=http://localhost:8080';");
MyDagRunsDAO dao = dbi.open(MyDagRunsDAO.class);

//do stuff with the DAO

dao.close();

Read Apache Airflow Data

With the connection open to Apache Airflow, simply call the previously defined method to retrieve data from the DagRuns entity in Apache Airflow.


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

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

Ready to get started?

Connect to live data from Apache Airflow with the API Driver

Connect to Apache Airflow