Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Community Articles

Find and share helpful community-sourced technical articles.
Announcements
Share your experience with Cloudera on G2 and get a $25 Amazon Gift card.
Hi, I'm CLEO! Something exciting is coming to the Community. Stay Tuned!
avatar
Rising Star

I have been playing quite a bit with CDSW lately. Here is a quick article on how to setup a CDSW project in scala connecting to an external RDBMS

Step 1: Create a new CDSW Project

Using the CDSW UI, create a new Scala Project:

103482-screen-shot-2019-02-13-at-41728-pm.png

Step 2: Reference the external Jar in your spark-defaults.conf

Open your project, and edit your spark-defaults.conf to add an external jar:

spark.jars=http://central.maven.org/maven2/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar

103483-screen-shot-2019-02-13-at-41812-pm.png

Step 3: Create a simple Scala file to connect to the DB

Create a new file and add this code in it:

val sqlContext = new org.apache.spark.sql.SQLContext(sc)

val df = sqlContext.read.format("jdbc").option("url", "jdbc:mysql://[YOUR_SERVER_IP]:3306/[YOUR_DB]").option("driver", "com.mysql.jdbc.Driver").option("dbtable", "[YOUR_TABLE]").option("user", "[YOUR_USER]").option("password", "[YOUR_PWD]").load()

df.show()

Step 4: Run your application

Launch a session and run your code:

103484-screen-shot-2019-02-13-at-42817-pm.png

2,490 Views