学习上传Flink Job任务:实现WordCount

首先实现Job任务的Jar包

  • 使用Idea创建maven项目
  • 修改pom.xml文件:添加依赖
	<!- 根据自己使用的scala与flink的版本修改版本号 ->
	<dependency>
      <groupId>org.apache.flink</groupId>
      <artifactId>flink-scala_2.11</artifactId>
      <version>1.7.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.flink</groupId>
      <artifactId>flink-streaming-scala_2.11</artifactId>
      <version>1.7.2</version>
    </dependency>
    <!- 添加插件,方便后面将依赖一起打包使用 ->
	<build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    </plugins>

  </build>

  • 创建scala Object实例FlinkStream
import org.apache.flink.api.java.utils.ParameterTool
import org.apache.flink.streaming.api.scala._

object FlinkStream {
    
    
  def main(args: Array[String]): Unit = {
    
    
    //创建流处理环境
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
    //从外部命令读取参数,作为主机名和端口号
    val paramTool: ParameterTool = ParameterTool.fromArgs(args)
    val host:String=paramTool.get("host")
    val port:Int=paramTool.getInt("port")
    //配置并行度,这里可以本篇学习暂时不用使用
    //env.setParallelism(2)
    //监听端口
    val inputDataStream: DataStream[String] = env.socketTextStream(host,port)
    //对获取的数据进行WordCount
    val resultDataSet: DataStream[(String, Int)] = inputDataStream
      .flatMap(_.split(" "))
      .filter(_.nonEmpty)
      .map((_,1))
      .keyBy(0)
      .sum(1)

    //输出结果
    resultDataSet.print()
    //启动流处理任务,并命名任务名称
    env.execute("stream word count")
  }
}

  • 编译->打包
    在这里插入图片描述

在Linux上启动Flink服务

  • 启动命令(在Flink目录下执行):./bin/start-cluster.sh
  • 启动后的进程:
    在这里插入图片描述
  • 启动端口:nc -lk 7777
  • 查看Web UI(建议Chrome浏览器):IP地址:8081
    在这里插入图片描述

上传Job任务

  • 上传
    在这里插入图片描述

  • 选择打好的Jar包
    在这里插入图片描述

  • 查看上传的jar包并进行配置
    在这里插入图片描述

  • 指定Class以及配置host与port
    在这里插入图片描述

  • 查看Plan,没有问题就可以submit了
    在这里插入图片描述

  • 然后就可以看到以下的界面,Task允许状态都是正常的
    在这里插入图片描述

开始测试

  • 在端口7777下输入数据
hello word
hello Flink
hello java
hello scala
hello word
hello kafka
  • 查看输出数据
    在这里插入图片描述
    在这里插入图片描述

到这里Flink上传Jar包新建Job任务就完成了

猜你喜欢

转载自blog.csdn.net/weixin_38468167/article/details/111842482
今日推荐