Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →Connect to FTP Data in JRuby
Create a simple JRuby app with access to live FTP data.
JRuby is a high-performance, stable, fully threaded Java implementation of the Ruby programming language. The CData JDBC Driver for FTP makes it easy to integrate connectivity to live FTP data in JRuby. This article shows how to create a simple JRuby app that connects to FTP data, executes a query, and displays the results.
Configure a JDBC Connection to FTP 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 FTP\lib).
JRuby natively supports JDBC, so you can easily connect to FTP and execute SQL queries. Initialize the JDBC connection with the getConnection function of the java.sql.DriverManager class.
To connect to FTP or SFTP servers, specify at least RemoteHost and FileProtocol. Specify the port with RemotePort.
Set User and Password to perform Basic authentication. Set SSHAuthMode to use SSH authentication. See the Getting Started section of the data provider help documentation for more information on authenticating via SSH.
Set SSLMode and SSLServerCert to secure connections with SSL.
The data provider lists the tables based on the available folders in your FTP server. Set the following connection properties to control the relational view of the file system:
- RemotePath: Set this to the current working directory.
- TableDepth: Set this to control the depth of folders to list as views.
- FileRetrievalDepth: Set this to retrieve and list files recursively from the root table.
Stored Procedures are available to download files, upload files, and send protocol commands. See the Data Model chapter of the FTP data provider documentation for more information.
Built-in Connection String Designer
For assistance in constructing the JDBC URL, use the connection string designer built into the FTP JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.
java -jar cdata.jdbc.ftp.jar
Fill in the connection properties and copy the connection string to the clipboard.
Below is a typical JDBC connection string for FTP:
jdbc:ftp:RemoteHost=MyFTPServer;
Create a JRuby App with Connectivity to FTP Data
Create a new Ruby file (for example: FTPSelect.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 FTP 2018/lib/cdata.jdbc.ftp.jar'
url = "jdbc:ftp:RemoteHost=MyFTPServer;"
conn = java.sql.DriverManager.getConnection(url)
stmt = conn.createStatement
rs = stmt.executeQuery("SELECT Filesize, Filename FROM MyDirectory")
while (rs.next) do
puts rs.getString(1) + ' ' + rs.getString(2)
end
With the file completed, you are ready to display your FTP data with JRuby. To do so, simply run your file from the command line:
jruby -S FTPSelect.rb
Writing SQL-92 queries to FTP allows you to quickly and easily incorporate FTP data into your own JRuby applications. Download a free trial today!