今天尝试了flink读取文件的测试程序编写,代码比较简单,途中遇到了一个问题,自定义封装类型的时候编译抛出如下异常:
Error:(10, 20) could not find implicit value for evidence parameter of type org.apache.flink.api.common.typeinfo.TypeInformation[whTest.readFile.SexAndAge]
dataStream.map(formatPerson(_)).print()
这个问题是因为程序需要一个隐式参数(implicit parameter)。我们可以看看上面程序用到的 map 在Flink中的实现:
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)、我们可以直接在代码里面加上