Learn how to create a connection to Db2 Warehouse on Cloud (formerly dashDB for Analytics) data in IBM Watson Studio and load the data in a Scala notebook.


If you’re using Db2 Warehouse Private (formerly dashDB Local), see Use Db2 Warehouse Private with Spark and Notebooks for more videos.

Try the tutorial

Learn how to use Spark SQL to load and analyze Db2 Warehouse on Cloud data using a Scala notebook in in IBM Watson Studio.

Before you begin

Complete these tasks before starting this tutorial:

  • If you do not have the Db2 Warehouse on Cloud service provisioned, refer to the Get started with Db2 Warehouse on Cloud (formerly dashDB) on IBM Cloud video and tutorial to learn how to provision the service.
  • Download this SALES table, and import it into your Db2 Warehouse on Cloud service instance.
    Make sure you don’t use any dashes (-) in the table name. For example, do not use SALES-TABLE, but instead use SALES.
  • After you load the data into the table, go to your tables and verify the table definition and browse the data. There should be 104 rows of data on the table found. If the table is empty, then reload the data into the existing table.
  • Watch the Getting Started on IBM Cloud video to create a IBM Cloud account and add the IBM Analytics for Apache Spark service.

Procedure 1: Set up the connection to your Db2 Warehouse on Cloud instance

  1. Log in to IBM Watson Studio at http://datascience.ibm.com.
  2. From the menu, click Connections.
  3. Click Create a Connection.
  4. Type a name and description for the connection.
  5. Select the service type which is Data Service in this case.
  6. From the Instance drop-down box, select the Db2 Warehouse on Cloud instance.
  7. Select the database which is BLUDB in this case.
  8. Click Create Connection.

Procedure 2: Create a Scala notebook to analyze the Db2 Warehouse on Cloud data

  1. Open an existing project, or create a new project.
  2. From the Files slide out panel, click Connections. Now select the Db2 Warehouse on Cloud connection you created earlier, and click Apply to add the connection to the list of data assets.
  3. Create a new notebook, specifying a name, description, Spark service to use, Scala, and Spark 2.0.
  4. Paste the following SQL statement into the first cell in the notebook, and then click the Run icon on the toolbar. This first command contains SQLContext which is the entry point into all functionality in Spark SQL and is necessary to execute SQL queries.
    Command: val sqlContext = new org.apache.spark.sql.SQLContext(sc)
  5. Paste the following SQL statement into the second cell, and then click Run. Replace the JDBCURL with the JDBC URL for your Db2 Warehouse on Cloud instance, such as, jdbc:db2://bluemix05.bluforcloud.com:50000/BLUDB. Also replace account and password with the username and password for your Db2 Warehouse on Cloud instance. If you don’t know your credentials, click Insert to code to see your credentials. This second command loads the sales table from the Db2 Warehouse on Cloud account and assigns it to the dashdata variable.
    Command: val dashdata = sqlContext.load("jdbc", Map( "url" -> "<JDBCURL>:user=<account>;password=<password>;", "dbtable" -> "<account>.SALES"))
    For example,
    val dashdata = sqlContext.load("jdbc", Map( "url" -> "jdbc:db2://awh-yp-small02.services.dal.bluemix.net:50000/BLUDB:user=dash######;password=######;","dbtable" -> "dash######.SALES"))

  6. Paste the following SQL statement into the third cell, and then click Run. This third command takes the dashdata and registers it as a table called salesdata.
    Command: dashdata.registerTempTable("salesdata")
  7. Paste the following SQL statement into the fourth cell, and then click Run. This next command lets you take a look at that schema.
    Command: dashdata.printSchema
  8. Paste the following SQL statement into the fifth cell, and then click Run. The dashdata.collect command shows the data collected for each sales associate.
    Command: dashdata.collect
  9. Paste the following SQL statement into the sixth cell, and then click Run. Once the data is registered as a table, you can use SQL to process the data. This next line returns the SALESMAN_ID data from the salesdata table.
    Command: val results = sqlContext.sql("SELECT SALESMAN_ID from salesdata")
    results.collect
  10. Paste the following SQL statement into the seventh cell, and then click Run. This command retrieves the information for a specific sales person by ID.
    Command: val salesman = sqlContext.sql("select * from salesdata where SALESMAN_ID='NC100'")
    salesman.collect

Join The Discussion

Your email address will not be published. Required fields are marked *