Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run spark-shell command in shell script

#!/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.

enter image description here

like image 421
Adarsh Avatar asked Jan 31 '26 12:01

Adarsh


1 Answers

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.

like image 112
Aravind Kumar Anugula Avatar answered Feb 02 '26 03:02

Aravind Kumar Anugula