demo代码:
object SocketWindowWordCount { def main(args: Array[String]): Unit = { val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment val text: DataStream[String] = env.socketTextStream("10.202.42.36",9000, '\n') val windowCounts = text.flatMap(w => w.split("\\s")).map(w => WordWithCount(w, 1)).keyBy("word").timeWindow(Time.seconds(5), Time.seconds(1)).sum("count") windowCounts.print().setParallelism(1) env.execute("Socket Window WordCount") } case class WordWithCount(value: String, i: Long) }编译报错:
报错分析:这种异常的发生通常是因为程序需要一个隐式参数(implicit parameter),参考map或flatMap在flink中的源码:
/** * Creates a new DataStream by applying the given function to every element and flattening * the results. */ def flatMap[R: TypeInformation](fun: T => TraversableOnce[R]): DataStream[R] = { if (fun == null) { throw new NullPointerException("FlatMap function must not be null.") } val cleanFun = clean(fun) val flatMapper = new FlatMapFunction[T, R] { def flatMap(in: T, out: Collector[R]) { cleanFun(in) foreach out.collect } } flatMap(flatMapper) }
/** * Creates a new DataStream by applying the given function to every element of this DataStream. */ 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) }方法的定义中有个
[R: TypeInformation]
,但程序并没有指定任何有关隐式参数的定义,编译代码无法创建TypeInformation,所以出现上面提到的异常信息。
解决方案:
1) 我们可以直接在代码里面加上以下的代码:
implicit
val
typeInfo
=
TypeInformation.of(classOf[Int])
|
然后再去编译代码就不会出现上面的异常。
2) 但是这并不是Flink推荐我们去做的,推荐的做法是在代码中引入一下包:
import
org.apache.flink.streaming.api.scala.
_
|
如果数据是有限的(静态数据集),我们可以引入以下包:
import
org.apache.flink.api.scala.
_
|
然后即可解决上面的异常信息。