![]() |
VOOZH | about |
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 explains how to build a basic Data Access Object (DAO) and the accompanying code to read and write Splunk data.
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();
}
Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Splunk.
To authenticate requests, set the , , and 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.
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.
👁 Using the built-in connection string designer to generate a JDBC URL (Salesforce is shown.)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();
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);
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.
Download a free trial of the Splunk Driver to get started:
Download NowLearn more:
👁 Splunk IconRapidly create and deploy powerful Java applications that integrate with Splunk data including Datamodels, Datasets, SearchJobs, and more!