Flink Java与Scala的Api类型不匹配问题

  
由于之前写的程序是Java版本,之后改写Scala版本时候就把import 包直接复制到Scala文件的包上,然后报错:

Java程序引包如下:

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternSelectFunction;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.windowing.time.Time;

 import org.apache.flink.streaming.api.scala._ //scala拓展API需要导入的

然后编写Scala代码:

object CepApp {

  def main(args: Array[String]) {

    val env = StreamExecutionEnvironment.getExecutionEnvironment
    val input = env.fromElements(new TemperatureEvent("xyz", 22.0))

    val pattern:Pattern[TemperatureEvent,_] = Pattern.begin[TemperatureEvent]("first").subtype(classOf[TemperatureEvent]).where(new FilterFunction[TemperatureEvent] {
      override def filter(value: TemperatureEvent): Boolean = {
        if (value.temperature >= 26.0) true else false

      }
    }).within(Time.seconds(10))


    val patternStream: DataStream[Alert] = CEP.pattern[TemperatureEvent](input, pattern).select(new PatternSelectFunction[TemperatureEvent, Alert] {
      override def select(pattern: util.Map[String, TemperatureEvent]): Alert = {

        new Alert("Temperature Rise Detected ...")
      }
    })

    patternStream.print()
    env.execute("CEP on Temperature Sensor")

  }

}
然后运行报错:


Error:(40, 74) type mismatch;
 found   : org.apache.flink.streaming.api.scala.DataStream[com.daxin.stream.cep.TemperatureEvent]
 required: org.apache.flink.streaming.api.datastream.DataStream[com.daxin.stream.cep.TemperatureEvent]
    val patternStream: DataStream[Alert] = CEP.pattern[TemperatureEvent](input, pattern).select(new PatternSelectFunction[TemperatureEvent, Alert] {
                                                                         ^
错误信息:需要org.apache.flink.streaming.api.datastream.DataStream类型的实际发现org.apache.flink.streaming.api.datastream.DataStream.

最后排查发现是倒错包,实际上导包为:

import org.apache.flink.api.common.functions.FilterFunction
import org.apache.flink.cep.PatternSelectFunction
import org.apache.flink.cep.scala.CEP
import org.apache.flink.cep.scala.pattern.Pattern
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.api.scala._

即可正确执行!


所以当发现scala包下类型与非scala包下同名类型变量类型不兼容时候,一般就是导包问题!









你可能感兴趣的:(Flink Java与Scala的Api类型不匹配问题)