Flink——入门WordCount程序

Flink是什么?

Apache Flink is a framework and distributed processing engine for stateful computations over unbounded and bounded data streams. Flink has been designed to run in all common cluster environments, perform computations at in-memory speed and at any scale. (官网原文)

Flink是一个框架、分布式的处理引擎。主要用于对无限制和有限制的数据流进行有状态的计算。在Flink中,一切都是由流组成:离线数据是有限制的流;实时数据时无限制的流。Flink最大的优点就是低延迟、高吞吐且可以保证精准一次性的状态一致性,他是一个真真正正的流式处理框架。

下面使用scala编写flink批处理版本和flink流式处理版本的WordCount程序:

 1、新建maven工程,导入依赖

 <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.flink/flink-scala -->
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-scala_2.11</artifactId>
            <version>1.7.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.flink/flink-streaming-scala -->
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-streaming-scala_2.11</artifactId>
            <version>1.7.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.flink/flink-clients -->
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-clients_2.11</artifactId>
            <version>1.7.2</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- 该插件用于将Scala代码编译成class文件 -->
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.4.6</version>
                <executions>
                    <execution>
                        <!-- 声明绑定到maven的compile阶段 -->
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

2、编写批处理版本wordcount

package com.wei.scala

import org.apache.flink.api.scala._

object WordCount {
  /**
   *  批处理
   * @param args
   */
  def main(args: Array[String]): Unit = {

    //创建执行环境
    val env: ExecutionEnvironment = ExecutionEnvironment.getExecutionEnvironment

    //读取数据文件
    val dataSet = env.readTextFile("C:\\idea\\Flink\\src\\main\\resources\\hello.txt")

    //切分文本内容
    val result =dataSet.flatMap(_.split(" "))
      .map((_,1))
      .groupBy(0)
      .sum(1)

    result.print()
  }
}

3、编写流式处理版本wordcount

package com.wei.scala

import org.apache.flink.streaming.api.scala._
object WordCount2 {
  def main(args: Array[String]): Unit = {

    //创建执行环境
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    //接收一个socket文本流
    val dataStream: DataStream[String] = env.socketTextStream("hdp-1",9999)

    //对流入的数据进行处理
    val result: DataStream[(String, Int)] = dataStream.flatMap(_.split(" ")).filter(_.nonEmpty).map((_,1)).keyBy(0).sum(1)

    //输出结果
    result.print()

    //启动executor
    env.execute("my first wordCount job")

  }

}

在虚拟机上启动一个netcat端口,用于发送数据,程序利用socketTextStream接收一个文本流,经过处理并且输出结果:

猜你喜欢

转载自blog.csdn.net/qq_45648512/article/details/106018463