一、Flink基础 之 wordcount


一、搭建maven工程

1.1 pom 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.kino.flnk</groupId>
    <artifactId>flink</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <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>
    </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>
</project>

1.2 添加scala框架 和 scala文件夹

在这里插入图片描述


1.3 批处理wordcount

flink\src\main\scala\WordCount.scala

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

/**
  * @author kino
  * @date 2020/4/9 19:56
  * @version 1.0.0
  */
object WordCount {
    def main(args: Array[String]): Unit = {
        // 创建执行环境
        val env = ExecutionEnvironment.getExecutionEnvironment
        // 从文件中读取数据
        val inputPath = "D:\\Work\\idea\\flink\\src\\main\\resources\\hello.txt"
        val inputDS: DataSet[String] = env.readTextFile(inputPath)
        // 分词之后,对单词进行groupby分组,然后用sum进行聚合
        val wordCountDS =
            inputDS.flatMap(_.split(" "))
          .map((_,1))
        
        // 打印输出
        wordCountDS.print()
    }
}

1.4 流处理 wordcount

flink\src\main\scala\StreamWordCount.scala

import org.apache.flink.api.java.utils.ParameterTool
import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment}

/**
  * @author kino
  * @date 2020/4/9 19:57
  * @version 1.0.0
  */
object StreamWordCount {
    def main(args: Array[String]): Unit = {
        // 从外部命令中获取参数
        val params: ParameterTool =  ParameterTool.fromArgs(args)
        val host: String = params.get("host")
        val port: Int = params.getInt("port")

        // 创建流处理环境
        val env = StreamExecutionEnvironment.getExecutionEnvironment
        // 接收socket文本流
        val textDstream: DataStream[String] = env.socketTextStream(host, port)

        // flatMap和Map需要引用的隐式转换
        import org.apache.flink.api.scala._
        val dataStream: DataStream[(String, Int)] = textDstream.flatMap(_.split("\\s")).filter(_.nonEmpty).map((_, 1)).keyBy(0).sum(1)

        dataStream.print().setParallelism(1)

        // 启动executor,执行任务
        env.execute("Socket stream word count")
    }
}

测试——window用netcat命令进行发送测试
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值