1

i'm writing a spark-scala program in intelligi, my code is basically to bring the table from oracle and store them in hdfs as text files insert_df.rdd.saveAsTextFile("hdfs://path"). I have tried this way, but it didn't work val script_sh = "///samplepath/file_creation_script.sh".!

But i have some conversions to make to the text file that I generated, I wrote a shell script to that. I dont want to run the spark jar file and the .sh file separately.

Please let me know if there is any way that i can call shell script through the program.

1
  • can you try df.write.text("/hdfs/path") Commented Oct 27, 2016 at 1:44

2 Answers 2

2

If you want to save the output of your command to a variable you need use:

import sys.process._
val result = "/path/to/your/script.sh".!! ("!" just execute the command)

And this can work like workaround:

import java.io.{BufferedReader, InputStreamReader}

  val p = new ProcessBuilder("/bin/bash","/path/to/your/script")
  val p2 = p.start()
  val br = new BufferedReader(new InputStreamReader(p2.getInputStream()))

  var line:String = ""
  while ({line = br.readLine();  line!= null}) {
    println(line)
  }
Sign up to request clarification or add additional context in comments.

Comments

0

I would say try using spark APIs only.

Still if you want to trigger a shell script from spark (1) and (2) worked for me

  1. In client mode:

Just run the shell script from spark code

val cmd = "home/some_script.sh"

cmd!

  1. In cluster mode:

I usually use Oozie , keep the .sh file in /lib folder of the workflow , this will copy the script to the containers.

from code invoke

val cmd = "./some_script.sh"

cmd!

For python "." wasnt required

val cmd = "python some_script.py"

cmd!

  1. If using spark-submit Use --files to copy the script to containers .

Haven't tried 3 , one may try and share.

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.