Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →How to work with IBM Cloud Object Storage Data in Apache Spark using SQL
Access and process IBM Cloud Object Storage Data in Apache Spark using the CData JDBC Driver.
Apache Spark is a fast and general engine for large-scale data processing. When paired with the CData JDBC Driver for IBM Cloud Object Storage, Spark can work with live IBM Cloud Object Storage data. This article describes how to connect to and query IBM Cloud Object Storage data from a Spark shell.
The CData JDBC Driver offers unmatched performance for interacting with live IBM Cloud Object Storage data due to optimized data processing built into the driver. When you issue complex SQL queries to IBM Cloud Object Storage, the driver pushes supported SQL operations, like filters and aggregations, directly to IBM Cloud Object Storage and utilizes the embedded SQL engine to process unsupported operations (often SQL functions and JOIN operations) client-side. With built-in dynamic metadata querying, you can work with and analyze IBM Cloud Object Storage data using native data types.
Install the CData JDBC Driver for IBM Cloud Object Storage
Download the CData JDBC Driver for IBM Cloud Object Storage installer, unzip the package, and run the JAR file to install the driver.
Start a Spark Shell and Connect to IBM Cloud Object Storage Data
- Open a terminal and start the Spark shell with the CData JDBC Driver for IBM Cloud Object Storage JAR file as the jars parameter:
$ spark-shell --jars /CData/CData JDBC Driver for IBM Cloud Object Storage/lib/cdata.jdbc.ibmcloudobjectstorage.jar
- With the shell running, you can connect to IBM Cloud Object Storage with a JDBC URL and use the SQL Context load() function to read a table.
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:
- Log in to your IBM Cloud account.
- 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:
- Log in to your IBM Cloud account.
- Navigate to the Platform API Keys page.
- On the middle-right corner click "Create an IBM Cloud API Key" to create a new API Key.
- 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.
- Extracts the access token and authenticates requests.
- 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.
Configure the connection to IBM Cloud Object Storage, using the connection string generated above.
scala> val ibmcloudobjectstorage_df = spark.sqlContext.read.format("jdbc").option("url", "jdbc:ibmcloudobjectstorage:ApiKey=myApiKey;CloudObjectStorageCRN=MyInstanceCRN;Region=myRegion;OAuthClientId=MyOAuthClientId;OAuthClientSecret=myOAuthClientSecret;").option("dbtable","Objects").option("driver","cdata.jdbc.ibmcloudobjectstorage.IBMCloudObjectStorageDriver").load()
- Once you connect and the data is loaded you will see the table schema displayed.
Register the IBM Cloud Object Storage data as a temporary table:
scala> ibmcloudobjectstorage_df.registerTable("objects")
-
Perform custom SQL queries against the Data using commands like the one below:
scala> ibmcloudobjectstorage_df.sqlContext.sql("SELECT Key, Etag FROM Objects WHERE Bucket = someBucket").collect.foreach(println)
You will see the results displayed in the console, similar to the following:
Using the CData JDBC Driver for IBM Cloud Object Storage in Apache Spark, you are able to perform fast and complex analytics on IBM Cloud Object Storage data, combining the power and utility of Spark with your data. Download a free, 30 day trial of any of the 200+ CData JDBC Drivers and get started today.