简体   繁体   中英

Scala WindowFunction does not compile

I have been writing a prototype application using Apache Flink. In the process, I have chosen to use org.apache.flink.streaming.api.functions.windowing.WindowFunction for a particular Use-Case. However, while writing the body of the apply() function, I am facing this error (the code below is not from the application I am writing - my datatypes are different - it is from the sample code available in Flink's documentation site):

import scala.collection.Iterable
import scala.collection.Map
import org.apache.flink.streaming.api.functions.windowing.WindowFunction
import org.apache.flink.streaming.api.windowing.windows.{TimeWindow}
import org.apache.flink.util.Collector
import scala.collection.JavaConversions._

class MyWindowFunction extends WindowFunction[(String, Long), String, String, TimeWindow] {

  def apply(key: String, window: TimeWindow, input: Iterable[(String, Long)], out: Collector[String]): Unit = {
    var count = 0L
    for (in <- input) {
      count = count + 1
    }
    out.collect(s"Window $window count: $count")
  }
}

The compiler is complaining:

    Error:(16, 7) class MyWindowFunction needs to be abstract, since method apply in trait WindowFunction of type 
(x$1: String, x$2: org.apache.flink.streaming.api.windowing.windows.TimeWindow, 
x$3: Iterable[(String, Long)], 
x$4: org.apache.flink.util.Collector[String])Unit is not defined
    class MyWindowFunction extends WindowFunction[(String, Long), String, String, TimeWindow] {

I have checked the order of the parameters in apply() ; they seem to be correct.

For some reason, I am failing to spot the exact source of the error. Could someone please nudge me to the solution?

I have found the cause of this error.

What was not clear to me was the fact that Apache Flink's API expects a java.lang.Iterable, instead of its Scala equivalent:

class MyWindowFunction extends 
      WindowFunction[(String, Long), String, String, TimeWindow] {

  override 
  def apply(
      key: String, 
      w: TimeWindow, 
      iterable: Iterable[(String, Long)],  // from java.lang.Iterable
      collector: Collector[String]): Unit = {

      // ....
  }
}

So, I had to import appropriately:

import java.lang.Iterable   // From Java
import java.util.Map        // From Java

import org.apache.flink.streaming.api.functions.windowing.WindowFunction
import org.apache.flink.streaming.api.windowing.windows.TimeWindow
import org.apache.flink.util.Collector

import scala.collection.JavaConversions._  // Implicit conversions

 class MyWindowFunction 
   extends WindowFunction[(String, Long), String, String, TimeWindow] {

   override 
   def apply(
       key: String, 
       w: TimeWindow, 
       iterable: Iterable[(String, Long)], 
       collector: Collector[String]): Unit = {

     // ....

  }
}

All was well!

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.

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