spark (2)

We have decided to extend the Early Bird Registration up to and including 10th October. This means you will be able to take advantage of the low rate for a little bit longer.

Register by October 10th. Save an additional €225 using EARLYEMEA discount code.

http://www.idug.org/p/cm/ld/fid=926

 

Read more…

My colleague Param (param.bng@in.ibm.com) and I (pallavipr@in.ibm.com) are exploring various aspects of Spark integration with DB2 and DB2 Connect drivers. We have decided to write a series of articles capturing our experimentation for the benefit of others as we did not find any article that focuses on different aspects of DB2 access via Spark.

Our first article in the series covered DB2 access via Spark Scala shell. This second article focuses on accessing DB2 data from via standalone Scala and Java program in Eclipse using DB2 JDBC driver and DataFrames API. Below are the detailed step by step instructions. Note that same instructions will apply to DB2 on all platforms (z/OS, LUW, I) as well as Informix.

  1. Confirm that you have Java installed by running java -version from Windows command line. JDK version 1.7 or 1.8 is recommended.

  2. Install Spark on local machine by downloading spark from https://spark.apache.org/downloads.html.

  3. We chose pre-built binaries as shown in Screenshot 1 (instead of source code download) to avoid building spark in early experimentation phase.

    9524596065?profile=originalScreenshot 1

  4. Unzip the installation file to a local directory (say C:/spark).

  5. Download Scala Eclipse IDE from http://scala-ide.org/download/sdk.html

  6. Unzip scala-SDK-4.1.0-vfinal-2.11-win32.win32.x86_64.zip into a folder (say c:\Eclipse_Scala)

  7. Find eclipse.exe from eclipse folder and run. Make sure you have 64-bit Java installed by running java -version from cmd prompt. Incompatibility between 64 bit Eclipse package and 32-bit Java will give an error and Eclipse would not start.

  8. Choose a workspace for your Scala project as shown in Screenshot 2.

    9524597452?profile=originalScreenshot 2

  9. Create a new Scala project using File->New Scala Project.

  10. Add Spark libraries downloaded in Step 6 to the newly created Scala project as shown in Screenshot 3.

    9524597469?profile=originalScreenshot 3

  11. You may see an error about more than 1 scala libraries as shown in Screenshot 4 since Spark has its own copy of Scala library.

9524597259?profile=originalScreenshot 4



  1. Remove Scala reference from the Java build path as shown in Screenshot 5 to remove the error.     9524597269?profile=originalScreenshot 5

  2. You may see another error “The version of scala library found in the build path of DB2SparkAccess (2.10.4) is prior to the one provided by scala IDE (2.11.6). Setting a Scala Installation Choice to match”. Right click Project->Properties->Scala Compiler and change project setting to 2.10 as shown in Screenshot 6.

    9524597501?profile=originalScreenshot 6

  3. After clicking OK, project gets rebuilt and you will only see a warning about different Scala versions that you can ignore.

  4. Now you can right click DB2SparkAccess project and choose New Scala App as shown in Screenshot 7. Enter application name and click Finish.

9524597088?profile=original

Screenshot 7

  1. Copy the following source code into the new Scala application you have created (.scala file) and modify the database credentials to yours.

    import org.apache.spark.sql.SQLContext

import org.apache.spark.SparkConf

import org.apache.spark.SparkContext

object DB2SparkScala extends App {

val conf = new SparkConf()

.setMaster("local[1]")

.setAppName("GetEmployee")

.set("spark.executor.memory", "1g")

val sc = new SparkContext(conf)

val sqlContext = new SQLContext(sc)

val employeeDF = sqlContext.load("jdbc", Map(

"url" -> "jdbc:db2://localhost:50000/sample:currentSchema=pallavipr;user=pallavipr;password=XXXX;",

"driver" -> "com.ibm.db2.jcc.DB2Driver",

"dbtable" -> "pallavipr.employee"))

employeeDF.show();

}

  1. Right click the application and select Run As-> Scala application as shown in Screenshot 8-

    9524598055?profile=originalScreenshot 8

  2. You may see the following exception - Exception in thread "main" java.lang.ClassNotFoundException: com.ibm.db2.jcc.DB2Driver. To get rid of the above exception, select Project->Properties and configure Java Build Path to include the IBM DB2 JDBC driver (db2jcc.jar or db2jcc4.jar) as shown in Screenshot 9. JDBC driver can be downloaded from http://www-01.ibm.com/support/docview.wss?uid=swg21385217

    9524598076?profile=originalScreenshot 9

  3. Now click on your Scala application and select Run As->Scala Application again and you should see the employee data retrieved from DB2 table as shown in Screenshot 10.

    9524597862?profile=originalScreenshot 10

  4. To perform similar access via a standalone Java program, Click on Project->New->Other as shown in Screenshot 11.

    9524597879?profile=originalScreenshot 11

  5. Select Java->Class and click Next that takes you to Screenshot 12.

    9524598261?profile=originalScreenshot 12

  6. Enter a name for your Java class and click Finish as shown in Screenshot 13 -

    9524597691?profile=originalScreenshot 13

  7. Paste the following code into your newly created class (.java file) with database credentials changed to yours.

import java.util.HashMap;

import java.util.Map;

import org.apache.spark.SparkConf;

import org.apache.spark.api.java.JavaSparkContext;

import org.apache.spark.sql.DataFrame;

import org.apache.spark.sql.SQLContext;

public class DB2SparkJava {

public static void main(String[] args) {

SparkConf conf = new SparkConf().setAppName("Simple Application");

conf.setMaster("local[1]");

conf.set("spark.executor.memory", "1g");

JavaSparkContext sc = new JavaSparkContext(conf);

SQLContext sqlContext = new SQLContext(sc);

Map<String, String> options = new HashMap<String, String>();

options.put(

"url",

"jdbc:db2://localhost:50000/sample:currentSchema=pallavipr;user=pallavipr;password=XXXX;");

options.put("driver", "com.ibm.db2.jcc.DB2Driver");

options.put("dbtable", "pallavipr.employee");

DataFrame jdbcDF = sqlContext.load("jdbc", options);

jdbcDF.show();

}

}

  1. Right click your newly created Java application. Select Run As → Java application. You should see similar results as Step 20.

Read more…

astorino_steven: Our flexible and fast AI-powered #PlanningAnalytics solution forecasts accuracy and consistency allowing businesses real-time insights - without the spreadsheet. IBM Planning Analytics is now available as a Service on AWS. #IBM #BusinessAnalytics #AWS

astorino_steven: #DataGovernance is an important step in any organization’s data strategy. Establishing policies, procedures, and standards for managing data ensures that data is being used in a way that is consistent with the businesses goals and values. #Data #IBM