I need a trait with takes a parameter and uses it for creation of implicit object. In the following way.
trait SparkSessionTrait(name:String) {
implicit val sparkSession = SparkSession
.builder()
.appName("AcoE Workflow_${name}")
.master("yarn")
.enableHiveSupport()
.getOrCreate()
}
How can i pass the args(0) as a parameter to the SParkSessionTrait
We can do if it is a case class but in my case it is an Object
object Test extends SparkSessionTrait{
def main(args: Array[String]): Unit = {
val name = args(0)
sparkSession.catalog.clearCache()
}
The way you want to do it simply doesn't make sense.
trait SparkSessionTrait(name:String)
isn't allowed (but will be in Scala 3 with some limitations), it needs to be a class
.
But the more important issue is that if you extend a class with parameters (or trait in Scala 3, that doesn't matter), these parameters need to be known at construction time. Ie you must have object Text extends SomeSessionTrait(someName)
. By the time main
method is called, it's far too late, you can't set name
from there.
One way to change the program and make it compile is
class SparkSessionTrait(name:String) {
implicit val sparkSession = SparkSession
.builder()
.appName("AcoE Workflow_${name}")
.master("yarn")
.enableHiveSupport()
.getOrCreate()
}
object Test {
def main(args: Array[String]): Unit = {
val name = args(0)
val sessionTrait = new SparkSessionTrait(name)
import sessionTrait._
sparkSession.catalog.clearCache()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.