H.Z. H.Z. - 12 days ago 5
Scala Question

How to pass sqlContext implicitly to a function?

I have a function in a Scala object which has the following signature

def f(v1:Int)(implicit sqlContext: SQLContext)


when I try to call this function from spark-shell I call it like

f(1)


and I expect the existing sqlContext gets passed to it implicitly but it doesn't. How can I make it to work so the sqlContext get passed to this function automatically?

Answer

You just need to add a SQLContext implicitly to the same context which you are calling your function:

implicit val sqlContext = new SQLContext() // just an example
// and then
f(1)

If you are using apacha Spark, you can use this import:

import sqlContext.implicits._