Flink的一个常见小坑 missing parameter type(从源码分析为啥换一个导入就可以)

25 阅读3分钟

img img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 使用Scala编码时,Flink接入的Kafka数据,不能使用幂名函数进行操作,只能自定义函数进行对DS的处理

2 对应的解决办法

2.1 对POJO类进行完整定义

        使用@Lombok对class进行注解

2.2 查看是否StreamingExecutionEnviroment导错

JAVA程序对应的Enviroment导入:

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

Scala程序对应的Enviromment导入:

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

3 那为什么引用不同的类就会报错呢

底层的原因是什么呢?我带着这个好奇心去源码里面看了一下。

入口函数:

 val environment: StreamExecutionEnvironment = StreamExecutionEnvironment
      .createLocalEnvironmentWithWebUI()

函数点击进去看到函数签名(进入到org.apache.flink.streaming.api.scala.StreamExecutionEnvironment):

  /**
   * Creates a [[StreamExecutionEnvironment]] for local program execution that also starts the
   * web monitoring UI.
   *
   * The local execution environment will run the program in a multi-threaded fashion in
   * the same JVM as the environment was created in. It will use the parallelism specified in the
   * parameter.
   *
   * If the configuration key 'rest.port' was set in the configuration, that particular
   * port will be used for the web UI. Otherwise, the default port (8081) will be used.
   *
   * @param config optional config for the local execution
   * @return The created StreamExecutionEnvironment
   */
  @PublicEvolving
  def createLocalEnvironmentWithWebUI(config: Configuration = null): StreamExecutionEnvironment = {
    val conf: Configuration = if (config == null) new Configuration() else config
    //调用JAVAEnv创建一个JavaEnv返回。
    //那么这个错误会和哪里有关系呢?
    new StreamExecutionEnvironment(JavaEnv.createLocalEnvironmentWithWebUI(conf))
  }

点进来一看,这里没有什么问题呀,这里也确实没有问题。那么问题在哪呢

真正的问题根源:

 /**
   * Create a DataStream using a user defined source function for arbitrary
   * source functionality. By default sources have a parallelism of 1. 
   * To enable parallel execution, the user defined source should implement 
   * ParallelSourceFunction or extend RichParallelSourceFunction. 
   * In these cases the resulting source will have the parallelism of the environment. 
   * To change this afterwards call DataStreamSource.setParallelism(int)
   *
   */
  def addSource[T: TypeInformation](function: SourceFunction[T]): DataStream[T] = {
    require(function != null, "Function must not be null.")


![img](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/c1507855513442debdf5727b710d5471~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3MzM5MTQ5MjgwNjA=:q75.awebp?rk3s=f64ab15b&x-expires=1770710765&x-signature=vhHD2Nb6MhSI9BFYmlZ%2FG3Hu7nw%3D)
![img](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/b11e095d282e47bbac919f2eea2c58db~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3MzM5MTQ5MjgwNjA=:q75.awebp?rk3s=f64ab15b&x-expires=1770710765&x-signature=Z%2FR3QLKROWwO9Dcbd3HOxCPWGRA%3D)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://gitee.com/vip204888)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**