Create a Data Access Object for Browse AI Data using 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 Browse AI integrates connectivity to live Browse AI data in Java applications. By pairing these technologies, you gain simple, programmatic access to Browse AI data. This article explains how to build a basic Data Access Object (DAO) and the accompanying code to read Browse AI data.
Create a DAO for the Browse AI RobotTasks 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 MyRobotTasksDAO {
//request specific data from Browse AI (String type is used for simplicity)
@SqlQuery("SELECT Status FROM RobotTasks WHERE RobotId = :robotId")
String findStatusByRobotId(@Bind("robotId") String robotId);
/*
* close with no args is used to close the connection
*/
void close();
}
Open a Connection to Browse AI
Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Browse AI.
Start by setting the Profile connection property to the location of the BrowseAI Profile on disk (e.g. C:\profiles\BrowseAI.apip). Next, set the ProfileSettings connection property to the connection string for BrowseAI (see below).
BrowseAI API Profile Settings
BrowseAI is a web scraping and automation platform that allows you to train robots to extract and monitor data from websites. Authentication is performed using an API key that is sent as a Bearer token in the Authorization header.
The BrowseAI API key has the format {userId}:{apiKey}, where both parts are UUIDs separated by a colon. This full string is provided as a single value in the BrowseAI dashboard.
To obtain your API key:
- Log in to your BrowseAI account at https://app.browse.ai
- Navigate to Settings in the dashboard
- Locate the API section
- Copy the full API key string displayed (format: userId:apiKey)
After obtaining your API key, set the following connection properties:
- AuthScheme: Set this to APIKey.
Set the following in the ProfileSettings connection property:
- APIKey: Set this to your BrowseAI API key (format: userId:apiKey).
Built-in Connection String Designer
For assistance in constructing the JDBC URL, use the connection string designer built into the Browse AI 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 Browse AI will typically look like the following:
jdbc:api:Profile=C:\profiles\BrowseAI.apip;AuthScheme=APIKey;ProfileSettings='APIKey=your_user_id:your_api_key';
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\BrowseAI.apip;AuthScheme=APIKey;ProfileSettings='APIKey=your_user_id:your_api_key';");
MyRobotTasksDAO dao = dbi.open(MyRobotTasksDAO.class);
//do stuff with the DAO
dao.close();
Read Browse AI Data
With the connection open to Browse AI, simply call the previously defined method to retrieve data from the RobotTasks entity in Browse AI.
//disply the result of our 'find' method
String status = dao.findStatusByRobotId("your-robot-id");
System.out.println(status);
Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for Browse AI by integrating with the CData JDBC Driver for Browse AI. Download a free trial and work with live Browse AI data in custom Java applications today.