flink could not find implicit value for evidence parameter of type

大多數剛剛使用Apache Flink的人極可能在編譯寫好的程序時遇到以下的錯誤:apache

Error:(15, 26) could not find implicit value for evidence parameter of type org.apache.flink.api.common.typeinfo.TypeInformation[Int]
    socketStockStream.map(_.toInt).print()
package com.iteblog.streaming
 
import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment}
 
 
object Iteblog{
  def main(args: Array[String]) {
    val env = StreamExecutionEnvironment.getExecutionEnvironment
    val socketStockStream:DataStream[String] = env.socketTextStream("www.iteblog.com", 9999)
    socketStockStream.map(_.toInt).print()
    env.execute("Stock stream")
  }
}

這種異常的發生一般是由於程序須要一個隱式參數(implicit parameter),咱們能夠看看上面程序用到的 map 在Flink中的實現:api

def map[R: TypeInformation](fun: T => R): DataStream[R] = {
  if (fun == null) {
    throw new NullPointerException("Map function must not be null.")
  }
  val cleanFun = clean(fun)
  val mapper = new MapFunction[T, R] {
    def map(in: T): R = cleanFun(in)
  }
 
  map(mapper)
}

在 map 的定義中有個 [R: TypeInformation] ,可是咱們程序並無指定任何有關隱式參數的定義,這時候編譯代碼沒法建立TypeInformation,因此出現上面提到的異常信息。解決這個問題有如下兩種方法
(1)、咱們能夠直接在代碼裏面加上如下的代碼:app

implicit val typeInfo = TypeInformation.of(classOf[Int])

而後再去編譯代碼就不會出現上面的異常。
(2)、可是這並非Flink推薦咱們去作的,推薦的作法是在代碼中引入一下包:socket

import org.apache.flink.streaming.api.scala._

若是數據是有限的(靜態數據集),咱們能夠引入如下包:ide

import org.apache.flink.api.scala._

而後便可解決上面的異常信息。scala

相關文章
相關標籤/搜索