Q:

How to Load .sql file to Scala?

I have a long SQL query stored in a data.sql file. I want to execute this file in my Scala code. 
With a sqlQuery string, I use a spark.sql(sqlQuery) to execute sql. But with a .sql file, how should I execute it? 
Thank you very much!

A:

You can try this flow (Spark 1.6.2, Scala 2.10.5)
Create a file with the query
$ cat query.sql
select * from mydb.mytable

If you have HDFS, you can upload file to HDFS (say at /hdfs/path/query.sql)
If you have it locally, then say it is at /local/path/query.sql
Run in spark shell
$ spark-shell
:
:
Created sql context (with Hive support)..
SQL context available as sqlContext.

(If loading from local)
scala> val myQueryFile = sc.textFile("file:///local/path/query.sql")

(If loading from HDFS)
scala> val myQueryFile = sc.textFile("/hdfs/path/query.sql")

Execute and view result
scala> val myQuery = myQueryFile.take(1)
scala> val myResultDf = sqlContext.sql(myQuery(0))
scala> myResultDf.show

A:

You could simply use scala.io.Source:
import scala.io.Source

val sqlQuery = Source.fromFile("path/to/data.sql").mkString //read file
spark.sql(sqlQuery) //execute query

