#!/bin/sh
spark-shell
import org.apache.spark.sql.SparkSession
val url="jdbc:mysql://localhost:3306/slow_and_tedious"
val prop = new java.util.Properties
prop.setProperty("user",”scalauser”)
prop.setProperty("password","scalauser123")
val people = spark.read.jdbc(url,"sat",prop)
The above commands are used to make a connection between Mysql and Spark using JDBC. But instead of writing these commands everytime I thought of make a script but when I run the above script it throws this error.

Create scala file named as test.scala with your code like below
import org.apache.spark.sql.SparkSession
val url="jdbc:mysql://localhost:3306/slow_and_tedious"
val prop = new java.util.Properties
prop.setProperty("user",”scalauser”)
prop.setProperty("password","scalauser123")
val people = spark.read.jdbc(url,"sat",prop)
Login to spark-shell using following command.
spark-shell --jars mysql-connector.jar
you can use following command to execute the code which you created above.
scala> :load /path/test.scala
shell script every time it launch sparkContext which takes more time to execute.
If you use above command it will just execute the code which is there in test.scala.
Since sparkContext will be loaded when you logging into spark-shell, time can be saved when you execute script.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With