Connect to Twitter Data in JRuby

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!



Create a simple JRuby app with access to live Twitter data.

JRuby is a high-performance, stable, fully threaded Java implementation of the Ruby programming language. The CData JDBC Driver for Twitter makes it easy to integrate connectivity to live Twitter data in JRuby. This article shows how to create a simple JRuby app that connects to Twitter data, executes a query, and displays the results.

Configure a JDBC Connection to Twitter Data

Before creating the app, note the installation location for the JAR file for the JDBC Driver (typically C:\Program Files\CData\CData JDBC Driver for Twitter\lib).

JRuby natively supports JDBC, so you can easily connect to Twitter and execute SQL queries. Initialize the JDBC connection with the getConnection function of the java.sql.DriverManager class.

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.

Below is a typical JDBC connection string for Twitter:

jdbc:twitter:InitiateOAuth=GETANDREFRESH

Create a JRuby App with Connectivity to Twitter Data

Create a new Ruby file (for example: TwitterSelect.rb) and open it in a text editor. Copy the following code into your file:

require 'java' require 'rubygems' require 'C:/Program Files/CData/CData JDBC Driver for Twitter 2018/lib/cdata.jdbc.twitter.jar' url = "jdbc:twitter:InitiateOAuth=GETANDREFRESH" conn = java.sql.DriverManager.getConnection(url) stmt = conn.createStatement rs = stmt.executeQuery("SELECT From_User_Name, Retweet_Count FROM Tweets") while (rs.next) do puts rs.getString(1) + ' ' + rs.getString(2) end

With the file completed, you are ready to display your Twitter data with JRuby. To do so, simply run your file from the command line:

jruby -S TwitterSelect.rb

Writing SQL-92 queries to Twitter allows you to quickly and easily incorporate Twitter data into your own JRuby applications. Download a free trial today!