簡體   English   中英

Scala WindowFunction無法編譯

[英]Scala WindowFunction does not compile

我一直在使用Apache Flink編寫原型應用程序。 在此過程中,我選擇將org.apache.flink.streaming.api.functions.windowing.WindowFunction用於特定的用例。 但是,在編寫apply()函數的主體時,我遇到了此錯誤(以下代碼並非來自我正在編寫的應用程序-我的數據類型不同-它來自Flink文檔站點中的示例代碼):

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")
  }
}

編譯器抱怨:

    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] {

我已經檢查了apply()中參數的順序; 他們似乎是正確的。

由於某些原因,我無法找出錯誤的確切來源。 有人可以將我推向解決方案嗎?

我已找到此錯誤的原因。

我不清楚的是,Apache Flink的API需要一個java.lang.Iterable,而不是它的Scala等同物:

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 = {

      // ....
  }
}

因此,我必須適當地導入:

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 = {

     // ....

  }
}

一切都好!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM