大多数刚刚使用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