Ready to get started?

Learn more about the CData JDBC Driver for Splunk or download a free trial:

Download Now

Create a Data Access Object for Splunk Data using JDBI

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

Create a DAO for the Splunk DataModels 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 MyDataModelsDAO { //insert new data into Splunk @SqlUpdate("INSERT INTO DataModels (Id, Owner) values (:id, :owner)") void insert(@Bind("id") String id, @Bind("owner") String owner); //request specific data from Splunk (String type is used for simplicity) @SqlQuery("SELECT Owner FROM DataModels WHERE Id = :id") String findOwnerById(@Bind("id") String id); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Splunk

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

To authenticate requests, set the User, Password, and URL properties to valid Splunk credentials. The port on which the requests are made to Splunk is port 8089.

The data provider uses plain-text authentication by default, since the data provider attempts to negotiate TLS/SSL with the server.

If you need to manually configure TLS/SSL, see Getting Started -> Advanced Settings in the data provider help documentation.

Built-in Connection String Designer

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

java -jar cdata.jdbc.splunk.jar

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

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

jdbc:splunk:user=MyUserName;password=MyPassword;URL=MyURL;InitiateOAuth=GETANDREFRESH

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:splunk:user=MyUserName;password=MyPassword;URL=MyURL;InitiateOAuth=GETANDREFRESH"); MyDataModelsDAO dao = dbi.open(MyDataModelsDAO.class); //do stuff with the DAO dao.close();

Read Splunk Data

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

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

Write Splunk Data

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

//add a new entry to the DataModels entity dao.insert(newId, newOwner);

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