H.Z. H.Z. - 1 year ago 109
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


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 Source

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

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

import sqlContext.implicits._
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download