大多数刚刚使用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
在中的实现:
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,所以出现上面提到的异常信息。解决这个问题有以下两种方法
implicit val typeInfo = TypeInformation.of(classOf[Int])
然后再去编译代码就不会出现上面的异常。
(2)、但是这并不是Flink推荐我们去做的,推荐的做法是在代码中引入一下包:import org.apache.flink.streaming.api.scala._
如果数据是有限的(静态数据集),我们可以引入以下包:
import org.apache.flink.api.scala._
然后即可解决上面的异常信息。