Monday, February 1, 2016

Connecting to any DB2 or (SQLDB powerwd by db2 on Bluemix) from IBM Bluemix - Juypter Notebooks on Spark

  1. Create an account in bluemix(ibm offers 30 days free trial) - https://console.ng.bluemix.net/registration/

  2. Create a spark service (https://www.ng.bluemix.net/docs/services/AnalyticsforApacheSpark/index.html)

  3. Now create notebook with scala as language.
    1. Import the two classes SparkConf and SparkContext
      import org.apache.spark.sql.{DataFrame, SQLContext} 
    2. Simply replace url with your db2 url.
      dbtable with name of the table for which you want to create dataframe.
      replace user and password for your db2 database server.
      val url = "jdbc:db2://75.126.155.153:50000/SQLDB"
      val dbtable = "USER13878.MYTABLE"
      val user = "USER13878"
      val password = "<enter your password>"
      val options = scala.collection.Map("url" -> url, "driver" -> "com.ibm.db2.jcc.DB2Driver", "dbtable" ->dbtable,"user"->user,"password"->password)

    3. Now create new SQLContext from your new Spark Context which has db2 driver loaded
      val sqlContext = new SQLContext(sc)

    4. Create a dataframereader from your SQLContext for your table
      val dataFrameReader = sqlContext.read.format("jdbc").options(options)

    5. Call the load method to create DataFrame for your table.
      val tableDataFrame = dataFrameReader.load()

    6. Call show() method to display the table contents in the Notebook
      tableDataFrame.show()


  4. You have successfully created a dataframe for your db2 table , now you can do further processing according to your need.
  5.  

No comments:

Post a Comment