繁体   English   中英

Scala:在Function接口中使用隐式

[英]Scala: Use implicits in with Function interface

在scala中,您可以编写一个类似以下的函数:

object Add extends ((Int, Int) => Int) {
  def apply(a: Int, b: Int) = a + b
}

我想写一个像上面的函数,但是我也想使用隐式。 就像是:

object DoSomething extends (Configuration, ??? => Dataframe) {
  override def apply(config: Configuration)(implicit sparkSession: SparkSession): DataFrame = {
    ...
  }
}

有谁知道,我该怎么做?

编辑:

object DoSomething extends (Configuration => SparkSession) {
  override def apply(config: Configuration)(implicit sparkSession: SparkSession): DataFrame = {
    val bootstrapServers = configuration.bootstrapServers
    val topic = configuration.topic

    sparkSession.readStream
      .format("kafka")
      .option("kafka.bootstrap.servers", bootstrapServers)
      .option("subscribe", topic)
      .load()
  }
}

尝试

class DoSomething(implicit sparkSession: SparkSession) extends (Configuration => DataFrame) {
  override def apply(config: Configuration): DataFrame = {
    val bootstrapServers = configuration.bootstrapServers
    val topic = configuration.topic

    sparkSession.readStream
      .format("kafka")
      .option("kafka.bootstrap.servers", bootstrapServers)
      .option("subscribe", topic)
      .load()
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM