Scala+;将函数和另一个值作为参数传递
在scala代码中,我有一个助手类,它设置SparkSession,如下所示:Scala+;将函数和另一个值作为参数传递,scala,function,Scala,Function,在scala代码中,我有一个助手类,它设置SparkSession,如下所示: def withSpark(func: SparkSession => Unit): Unit = { val session = SparkSession.builder() .master("local[*]") .appName("temp checks") .config("spark.ui.enabled
def withSpark(func: SparkSession => Unit): Unit = {
val session = SparkSession.builder()
.master("local[*]")
.appName("temp checks")
.config("spark.ui.enabled", "false")
.getOrCreate()
session.sparkContext.setCheckpointDir(System.getProperty("java.io.tmpdir"))
try {
func(session)
} finally {
session.stop()
System.clearProperty("spark.driver.port")
}
}
def withSpark(func: SparkSession => Unit, spark_master: String): Unit = {
val session = SparkSession.builder()
.master(spark_master)
.appName("temp checks")
.config("spark.ui.enabled", "false")
.getOrCreate()
session.sparkContext.setCheckpointDir(System.getProperty("java.io.tmpdir"))
try {
func(session)
} finally {
session.stop()
System.clearProperty("spark.driver.port")
}
}
我在主代码中使用这个util,如下所示:
withSpark {
session => <do required stuff here>
}
主要代码如下:
withSpark("yarn") {
session => <do required stuff here>
}
withSpark(“纱线”){
会话=>
}
但是,我不能这样做。有人能帮我吗
提前感谢。可用于提供此类语法
def withSpark(spark_master: String)(func: SparkSession => Unit) = ???
注意函数在最后一个参数列表中的位置。现在我们可以表达
withSpark("yarn") {
session => <do required stuff here>
}
withSpark(“纱线”){
会话=>
}
您是如何期望这样的声明像您希望的那样工作的?此外,这是基本语法,如果您打算使用Spark,至少尝试学习Scala的基础。