0

I am newbie to spark and scala. I wanted to execute some spark code from inside a bash script. I wrote the following code.

Scala code was written in a separate .scala file as follows.

Scala Code:

import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark.SparkConf

object SimpleApp {
  def main(args: Array[String]) {
    val conf = new SparkConf().setAppName("Simple Application")
    val sc = new SparkContext(conf)
    println("x="+args(0),"y="+args(1))
  }
}

This is the bash script that invokes the Apache-spark/scala code.

Bash Code

#!/usr/bin/env bash
Absize=File_size1
AdBsize=File_size2
for i in `seq 2 $ABsize`
do
    for j in `seq 2 $ADsize`
    do
        Abi=`sed -n ""$i"p" < File_Path1`
        Adj=`sed -n ""$j"p" < File_Path2`
        scala SimpleApp.scala $Abi $adj
    done
done

But then I get the following errors.

Errors:

error:object apache is not a member of package org
import org.apache.spark.SparkContext
          ^
error: object apache is not a member of package org
import org.apache.spark.SparkContext._
           ^
error: object apache is not a member of package org
import org.apache.spark.SparkConf
           ^
error: not found:type SparkConf
val conf = new SparkConf().setAppName("Simple Application")              ^
 error: not found:type SparkContext

The above code works perfectly if the scala file is written without any spark function (That is a pure scala file), but fails when there are apache-spark imports.

What would be a good way to run and execute this from bash script? Will I have to call spark shell to execute the code?

7
  • 3
    Try to provide spark-core dependency in class-path like scala -classpath "*.jar" YourFile.scala Commented Nov 15, 2016 at 10:07
  • I used the following command scala -classpath simple-project_2.11-1.0.jar SimpleApp.scala $Abi $adj but still there is a same error @FaigB Commented Nov 15, 2016 at 11:43
  • 1
    If you have spark locally use in classpath jar from $spark_home/lib/spark-core_2.10-{version}.jar OR download from mvnrepository.com/artifact/org.apache.spark/spark-core_2.10 and put on classpath Commented Nov 15, 2016 at 12:49
  • 1
    Typically, you submit spark jobs with the spark-submit command Commented Nov 15, 2016 at 17:27
  • 2
    you have 2 choices set up spark with environment variable and run as @puhlen told with spark-submit -class SimpleApp simple-project_2.11-1.0.jar $Abi $adj OR list all required ClassNotFoundException libs in classpath Commented Nov 15, 2016 at 20:43

1 Answer 1

1

set up spark with environment variable and run as @puhlen told with spark-submit -class SimpleApp simple-project_2.11-1.0.jar $Abi $adj

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.