Create a Data Access Object for Databricks Data using JDBI



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

About Databricks Data Integration

Accessing and integrating live data from Databricks has never been easier with CData. Customers rely on CData connectivity to:

  • Access all versions of Databricks from Runtime Versions 9.1 - 13.X to both the Pro and Classic Databricks SQL versions.
  • Leave Databricks in their preferred environment thanks to compatibility with any hosting solution.
  • Secure authenticate in a variety of ways, including personal access token, Azure Service Principal, and Azure AD.
  • Upload data to Databricks using Databricks File System, Azure Blog Storage, and AWS S3 Storage.

While many customers are using CData's solutions to migrate data from different systems into their Databricks data lakehouse, several customers use our live connectivity solutions to federate connectivity between their databases and Databricks. These customers are using SQL Server Linked Servers or Polybase to get live access to Databricks from within their existing RDBMs.

Read more about common Databricks use-cases and how CData's solutions help solve data problems in our blog: What is Databricks Used For? 6 Use Cases.


Getting Started


Create a DAO for the Databricks Customers 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 MyCustomersDAO { //insert new data into Databricks @SqlUpdate("INSERT INTO Customers (Country, CompanyName) values (:country, :companyName)") void insert(@Bind("country") String country, @Bind("companyName") String companyName); //request specific data from Databricks (String type is used for simplicity) @SqlQuery("SELECT CompanyName FROM Customers WHERE Country = :country") String findCompanyNameByCountry(@Bind("country") String country); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Databricks

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

To connect to a Databricks cluster, set the properties as described below.

Note: The needed values can be found in your Databricks instance by navigating to Clusters, and selecting the desired cluster, and selecting the JDBC/ODBC tab under Advanced Options.

  • Server: Set to the Server Hostname of your Databricks cluster.
  • HTTPPath: Set to the HTTP Path of your Databricks cluster.
  • Token: Set to your personal access token (this value can be obtained by navigating to the User Settings page of your Databricks instance and selecting the Access Tokens tab).

Built-in Connection String Designer

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

java -jar cdata.jdbc.databricks.jar

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

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

jdbc:databricks:Server=127.0.0.1;Port=443;TransportMode=HTTP;HTTPPath=MyHTTPPath;UseSSL=True;User=MyUser;Password=MyPassword;

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:databricks:Server=127.0.0.1;Port=443;TransportMode=HTTP;HTTPPath=MyHTTPPath;UseSSL=True;User=MyUser;Password=MyPassword;"); MyCustomersDAO dao = dbi.open(MyCustomersDAO.class); //do stuff with the DAO dao.close();

Read Databricks Data

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

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

Write Databricks Data

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

//add a new entry to the Customers entity dao.insert(newCountry, newCompanyName);

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

Ready to get started?

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

 Download Now

Learn more:

Databricks Icon Databricks JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with Databricks.