0

In scala you can write a a function like:

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

I want to write a function like above, but i also want to use imlicit. something like:

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

Does anyone know, how I can do this?

Edit:

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()
  }
}
1
  • 1
    Again it would be easier if you just have a plain object, and you define your apply there, which can receive an implicit. Commented Sep 9, 2019 at 12:12

1 Answer 1

1

Try

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()
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry that ??? I added because I didn't know how to mark the place where I thought the imlpicit definition should be or how it should look like.
@SleepyX667 Your apply returns DataFrame but there is no DataFrame in (Configuration, ??? => SparkSession).
I added a edit comment to the question and correct the typo I made (you are right... the function should return a Dataframe
@SleepyX667 Maybe this should be object DoSomething extends (Configuration => DataFrame). If method is def apply(x: X): Y then class extends (X => Y).
@SleepyX667 You can't put implicit to function type. This feature will appear only in Scala 3 dotty.epfl.ch/docs/reference/contextual/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.