当前位置:   article > 正文

Flink批处理和流处理两种方式实现WordCount代码示例_streamexecutionenvironment wordcount

streamexecutionenvironment wordcount

使用scala实现批处理和流处理的wordcount示例

编写scala版本批处理wordcount

1.新建maven项目
2.导入pom坐标

<dependency>
    <groupId>org.apache.flink</groupId>
    <artifactId>flink-streaming-scala_2.11</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>org.apache.flink</groupId>
    <artifactId>flink-connector-kafka-0.10_2.11</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>org.apache.flink</groupId>
    <artifactId>flink-table_2.10</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>org.apache.flink</groupId>
    <artifactId>flink-scala_2.10</artifactId>
    <version>1.3.2</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

3.编写scala版本批处理wordcount代码

package com.qcj.wc_demo

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

/**
  * 简单的批处理word count例子
  */
object WordCount {
  def main(args: Array[String]) {
    //批处理程序,需要创建ExecutionEnvironment
    val env = ExecutionEnvironment.getExecutionEnvironment
    //fromElements(elements:_*) --- 从一个给定的对象序列中创建一个数据流,所有的对象必须是相同类型的。
    val text = env.fromElements(
      "Who's there?",
      "I think I hear them. Stand, ho! Who's there?","hah")

    val counts = text.flatMap { _.toLowerCase.split("\\W+") filter { _.nonEmpty } }
      .map { (_, 1) }
      .groupBy(0)//根据第一个元素分组
      .sum(1)

    //打印
    counts.print()
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

4.运行结果
在这里插入图片描述

编写scala版本流处理wordcount

1.安装nc
a.下载netcat。下载地址这里
b.解压压缩包到指定路径下
c.配置path环境变量

我的path变量:
E:\software\netcat-1.11
  • 1
  • 2

d.打开命令界面:Windows+R cmd。输入nc 命令即可.

2.编写scala版本流处理wordcount

package com.qcj.wc_demo

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.time.Time

/**
  * 实时接收word count例子
  *
  * 运行准备
  * 1.windows上安装netcat,下载解压,配置路径path就行了:
  *   参考博客:  https://blog.csdn.net/qq_37585545/article/details/82250984
  * 2.运行前先cmd启动nc:就可以输入数据了
  *   nc -lL -p 9999
  * 3.之后运行此程序,记住要先启动nc.后运行程序,不然会报错
  */
object WordCount2 {
  def main(args: Array[String]) {
    /*
    在Flink程序中首先需要创建一个StreamExecutionEnvironment
    (如果你在编写的是批处理程序,需要创建ExecutionEnvironment),它被用来设置运行参数。
    当从外部系统读取数据的时候,它也被用来创建源(sources)。
     */
    val env = StreamExecutionEnvironment.getExecutionEnvironment
    val text = env.socketTextStream("localhost", 9999)

    val counts = text.flatMap { _.toLowerCase.split("\\W+") filter { _.nonEmpty } }//nonEmpty非空的
      .map { (_, 1) }
      .keyBy(0)//通过Tuple的第一个元素进行分组
      .timeWindow(Time.seconds(5))//Windows 根据某些特性将每个key的数据进行分组 (例如:在5秒内到达的数据).
      .sum(1)

    //将结果流在终端输出
    counts.print
    //开始执行计算
    env.execute("Window Stream WordCount")
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

3.cmd窗口运行nc
输入命令:nc -lL -p 9999 回车等待启动程序
在这里插入图片描述
4.运行程序,输入测试数据
在这里插入图片描述

例子github地址

例子github地址

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/696145
推荐阅读
相关标签
  

闽ICP备14008679号