Ready to get started?

Learn more about the CData JDBC Driver for IBM Cloud Object Storage or download a free trial:

Download Now

Analyze IBM Cloud Object Storage Data in R

Use standard R functions and the development environment of your choice to analyze IBM Cloud Object Storage data with the CData JDBC Driver for IBM Cloud Object Storage.

Access IBM Cloud Object Storage data with pure R script and standard SQL on any machine where R and Java can be installed. You can use the CData JDBC Driver for IBM Cloud Object Storage and the RJDBC package to work with remote IBM Cloud Object Storage data in R. By using the CData Driver, you are leveraging a driver written for industry-proven standards to access your data in the popular, open-source R language. This article shows how to use the driver to execute SQL queries to IBM Cloud Object Storage and visualize IBM Cloud Object Storage data by calling standard R functions.

Install R

You can match the driver's performance gains from multi-threading and managed code by running the multithreaded Microsoft R Open or by running open R linked with the BLAS/LAPACK libraries. This article uses Microsoft R Open 3.2.3, which is preconfigured to install packages from the Jan. 1, 2016 snapshot of the CRAN repository. This snapshot ensures reproducibility.

Load the RJDBC Package

To use the driver, download the RJDBC package. After installing the RJDBC package, the following line loads the package:

library(RJDBC)

Connect to IBM Cloud Object Storage as a JDBC Data Source

You will need the following information to connect to IBM Cloud Object Storage as a JDBC data source:

  • Driver Class: Set this to cdata.jdbc.ibmcloudobjectstorage.IBMCloudObjectStorageDriver
  • Classpath: Set this to the location of the driver JAR. By default this is the lib subfolder of the installation folder.

The DBI functions, such as dbConnect and dbSendQuery, provide a unified interface for writing data access code in R. Use the following line to initialize a DBI driver that can make JDBC requests to the CData JDBC Driver for IBM Cloud Object Storage:

driver <- JDBC(driverClass = "cdata.jdbc.ibmcloudobjectstorage.IBMCloudObjectStorageDriver", classPath = "MyInstallationDir\lib\cdata.jdbc.ibmcloudobjectstorage.jar", identifier.quote = "'")

You can now use DBI functions to connect to IBM Cloud Object Storage and execute SQL queries. Initialize the JDBC connection with the dbConnect function.

Register a New Instance of Cloud Object Storage

If you do not already have Cloud Object Storage in your IBM Cloud account, follow the procedure below to install an instance of SQL Query in your account:

  1. Log in to your IBM Cloud account.
  2. Navigate to the page, choose a name for your instance and click Create. You will be redirected to the instance of Cloud Object Storage you just created.

Connecting using OAuth Authentication

There are certain connection properties you need to set before you can connect. You can obtain these as follows:

API Key

To connect with IBM Cloud Object Storage, you need an API Key. You can obtain this as follows:

  1. Log in to your IBM Cloud account.
  2. Navigate to the Platform API Keys page.
  3. On the middle-right corner click "Create an IBM Cloud API Key" to create a new API Key.
  4. In the pop-up window, specify the API Key name and click "Create". Note the API Key as you can never access it again from the dashboard.

Cloud Object Storage CRN

If you have multiple accounts, you will need to specify the CloudObjectStorageCRN explicitly. To find the appropriate value, you can:

  • Query the Services view. This will list your IBM Cloud Object Storage instances along with the CRN for each.
  • Locate the CRN directly in IBM Cloud. To do so, navigate to your IBM Cloud Dashboard. In the Resource List, Under Storage, select your Cloud Object Storage resource to get its CRN.

Connecting to Data

You can now set the following to connect to data:

  • InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
  • ApiKey: Set this to your API key which was noted during setup.
  • CloudObjectStorageCRN (Optional): Set this to the cloud object storage CRN you want to work with. While the connector attempts to retrieve this automatically, specifying this explicitly is recommended if you have more than Cloud Object Storage account.

When you connect, the connector completes the OAuth process.

  1. Extracts the access token and authenticates requests.
  2. Saves OAuth values in OAuthSettingsLocation to be persisted across connections.

Built-in Connection String Designer

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

java -jar cdata.jdbc.ibmcloudobjectstorage.jar

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

Below is a sample dbConnect call, including a typical JDBC connection string:

conn <- dbConnect(driver,"jdbc:ibmcloudobjectstorage:ApiKey=myApiKey;CloudObjectStorageCRN=MyInstanceCRN;Region=myRegion;OAuthClientId=MyOAuthClientId;OAuthClientSecret=myOAuthClientSecret;")

Schema Discovery

The driver models IBM Cloud Object Storage APIs as relational tables, views, and stored procedures. Use the following line to retrieve the list of tables:

dbListTables(conn)

Execute SQL Queries

You can use the dbGetQuery function to execute any SQL query supported by the IBM Cloud Object Storage API:

objects <- dbGetQuery(conn,"SELECT Key, Etag FROM Objects WHERE Bucket = 'someBucket'")

You can view the results in a data viewer window with the following command:

View(objects)

Plot IBM Cloud Object Storage Data

You can now analyze IBM Cloud Object Storage data with any of the data visualization packages available in the CRAN repository. You can create simple bar plots with the built-in bar plot function:

par(las=2,ps=10,mar=c(5,15,4,2)) barplot(objects$Etag, main="IBM Cloud Object Storage Objects", names.arg = objects$Key, horiz=TRUE)