Create a Data Access Object for Twitter Data using JDBI

Ready to get started?

Download for a free trial:

Download Now

Learn more:

Twitter JDBC Driver

A straightforward interface to connect any Java application with Twitter integration capabilities including Search, GeoSearch, UserInfo, DirectMessages, Followers, and more!



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

Create a DAO for the Twitter Tweets 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 MyTweetsDAO { //insert new data into Twitter @SqlUpdate("INSERT INTO Tweets (From_User_Name, Retweet_Count) values (:from_User_Name, :retweet_Count)") void insert(@Bind("from_User_Name") String from_User_Name, @Bind("retweet_Count") String retweet_Count); //request specific data from Twitter (String type is used for simplicity) @SqlQuery("SELECT Retweet_Count FROM Tweets WHERE From_User_Name = :from_User_Name") String findRetweet_CountByFrom_User_Name(@Bind("from_User_Name") String from_User_Name); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Twitter

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

All tables require authentication. You can connect using your User and Password or OAuth. To authenticate using OAuth, you can use the embedded OAuthClientId, OAuthClientSecret, and CallbackURL or you can register an app to obtain your own.

If you intend to communicate with Twitter only as the currently authenticated user, then you can obtain the OAuthAccessToken and OAuthAccessTokenSecret directly by registering an app.

See the Getting Started chapter in the help documentation for a guide to using OAuth.

Built-in Connection String Designer

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

java -jar cdata.jdbc.twitter.jar

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

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

jdbc:twitter: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:twitter:InitiateOAuth=GETANDREFRESH"); MyTweetsDAO dao = dbi.open(MyTweetsDAO.class); //do stuff with the DAO dao.close();

Read Twitter Data

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

//disply the result of our 'find' method String retweet_Count = dao.findRetweet_CountByFrom_User_Name("twitter"); System.out.println(retweet_Count);

Write Twitter Data

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

//add a new entry to the Tweets entity dao.insert(newFrom_User_Name, newRetweet_Count);

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