I have a test script to read a text file provided as a parameter like below:
test.scala:
$ cat test.scala
import scala.io.Source
val filename = args(0)
for (line <- Source.fromFile(filename).getLines) {
println(line)
}
I want to read a text file below:
$ cat test.txt
test1
test2
test3
I need to run the scala in commandline like below:
spark-shell -i test.scala test.txt
I expect test.txt is recognized as args(0)
, but I see the output like:
:26: error: not found: value args val filename = args(0)
Can anyone enlighten me on what is the right usage to do this? Thank you very much.
UPDATE:
cat test.scala
import scala.io.Source
val args = spark.sqlContext.getConf("spark.driver.args").split(",")
val filename = args(0)
for (line <- Source.fromFile(filename).getLines) {
println(line)
}
Test result:
spark-shell -i test.scala --conf spark.driver.args="test.txt"
SQL context available as sqlContext. Loading test.scala... import scala.io.Source <console>:26: error: not found: value spark val args = spark.sqlContext.getConf("spark.driver.args").split(",")