惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
AWS News Blog
AWS News Blog
V
Vulnerabilities – Threatpost
D
Darknet – Hacking Tools, Hacker News & Cyber Security
量子位
博客园 - 叶小钗
AI
AI
T
Tor Project blog
Forbes - Security
Forbes - Security
W
WeLiveSecurity
博客园_首页
爱范儿
爱范儿
J
Java Code Geeks
B
Blog
G
GRAHAM CLULEY
aimingoo的专栏
aimingoo的专栏
Cloudbric
Cloudbric
C
CXSECURITY Database RSS Feed - CXSecurity.com
TaoSecurity Blog
TaoSecurity Blog
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
有赞技术团队
有赞技术团队
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Simon Willison's Weblog
Simon Willison's Weblog
云风的 BLOG
云风的 BLOG
Google DeepMind News
Google DeepMind News
H
Help Net Security
博客园 - 三生石上(FineUI控件)
C
Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Palo Alto Networks Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园 - 司徒正美
The Last Watchdog
The Last Watchdog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
S
Secure Thoughts
Spread Privacy
Spread Privacy
F
Fortinet All Blogs
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
H
Hackread – Cybersecurity News, Data Breaches, AI and More
A
About on SuperTechFans
Security Latest
Security Latest
Webroot Blog
Webroot Blog
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog

Dr34m's Blog

I Expect You To Die(我希望你死) 1,2,3 完整中文攻略 Linux换国内源 && docker安装 && 换加速镜像 Vue3 + Element-Plus 极简速查 随笔 pyinstaller打包的程序执行报错Failed to extract xxxx decompression resulted in return code -1 taoSync排除项简易教程 apscheduler的cron配置项 如何在绿联NAS中使用TaoSync同步我的文件到各个网盘 GB28181抓包记录 JS计算字节大小,把字节转换为KB/MB/GB/TB等 在Python中使用onvif管理摄像头,包括设备发现,获取RTSP地址,获取设备信息,截图,云台控制与缩放,设置时间 编写bat脚本实现对vue项目构建并压缩 内网穿透工具frp快速使用 CentOS 7.9 安装基础开发环境jdk redis nacos nginx mysql Pyinstaller 逆向 VUE实现复制与粘贴_获取剪切板内容 Electron + xterm.js + node-pty + vue 实现本地终端 child_process exec 中文乱码 Windows&Linux 解决方法 yum 更新 gcc CentOS安装并使用conda 将fluid主题博客的静态资源由第三方改为本地存储 一个上手即用的通用公众号/小程序/h5/app框架 python批量转化doc到docx SpringBoot单元测试注入空指针 TensorFlow 预测出现NaN的一种可能以及解决方法 随笔 python归一化数据 windows下python安装sasl遇到的问题及解决 将对象List中的某个字段放到新的List中[转载] Python http.server 本地服务支持跨域 TensorFlow入门 windows本地配置spark开发环境 dataX使用 Apache Hive 学习笔记 hadoop学习笔记 element-ui 自带事件添加自定义参数 windows 11安装安卓应用程序 centos7安装zookeeper centos7安装jdk8 用idea开发spring项目过程中热重启 docker学习笔记 CentOS7安装k8s集群 通过Github Actions部署静态网站到腾讯云COS,并自动刷新CDN 用Typora编写Hexo博客时图片的处理 Hexo + Github Actions 提交代码自动部署 云服务器 腾讯云COS github-pages 常用的Linux进程基本命令 python实现微信jsapi签名 VsCode开发Python常用配置 mysql获取当期日期是该年第几周 LocalDate获取当前周周一日期 删除node_modules重新安装 基于firewalld端口转发 pip 安装 tensorflow MemoryError Java过滤html标签 mysql清空表,并让自增从0开始 JavaScript获取当前周或下n周的周n的日期 java复制不同实体类中相同的字段 前端下载二进制文件 mysql获取最近一段时间数据 python - pip换源 svn提交后jenkins自动部署 常见nginx反向代理配置 h5实现一种自动滚动的告警列表 nginx代理网站子目录到本地目录 centos下hexo + svn + jenkins实现博客自动部署 centos中jenkins配置环境变量 centos安装jenkins centos安装svn服务器 Ubuntu 20.04 换国内源 Vs Code编写md文件实现实时预览 Python自动处理依赖 海滨校园助手api使用文档 hexo-yilia主题相册 Java实现文件重命名,Java文件追加写入,java读取图片尺寸 Hexo安装配置并托管至github 新起点,新征程 在ubuntu安装jdk并配置环境变量 一个不到300行的C语言消灭敌机游戏 搭建L(Linux)+A(Apache)+M(MySQL)+P(PHP)网站环境,并安装Discuz 搭建L(Linux)+N(Nginx)+M(MySQL)+P(PHP)网站环境,并安装Wordpress NSA工具包验证之RDP漏洞利用 NSA工具包验证之IIS6.0漏洞利用 NSA工具包验证之SMB漏洞利用 SEO Ultimate 7.6.5.9汉化版(中文版)免费下载 在腾讯云服务器搭建FBCTF平台 FBCTF汉化简体中文免费下载,FBCTF更新缓存代码 wordpress发送邮件设置以及常见问题解决 短网址 一个小白的自学建站史(菜鸟建站入门) 危山 水调歌头 水调歌头
Flink学习笔记
Dr3@m · 2022-01-20 · via Dr34m's Blog

本文基于B站视频教程尚硅谷Java版Flink(武老师清华硕士,原IBM-CDL负责人),本文软件版本,行文顺序等可能与视频略有不同

一、概念

1. 是什么

Apache Flink是一个框架和分布式处理引擎,用于对无界和有界数据流进行状态计算。Flink 被设计为在所有常见的集群环境中运行以内存中的速度任何规模执行计算

2. 目标

  • 低延时
  • 高吞吐
  • 结果的准确性和良好的容错性

二、使用

1. 使用前准备

pom依赖

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
<?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.ifnxs.bigdata</groupId>
<artifactId>FlinkTutorial</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<flink.version>1.12.1</flink.version>
<scala.binary.version>2.12</scala.binary.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-java</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-scala_${scala.binary.version}</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-clients_${scala.binary.version}</artifactId>
<version>${flink.version}</version>
</dependency>
</dependencies>

</project>

flink-streaming-scala_2.12 => org.apache.flink:flink-runtime_2.12:1.12.1 => com.typesafe.akka:akka-actor_2.12:2.5.21,akka就是用scala实现的。即使这里我们用java语言,还是用到了scala实现的包

hello.txt内容

1
2
3
4
5
6
hello world
hello spark
hello flink
how are you
fine thank you
and you

2. 批处理实现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
38
39
package com.ifnxs.bigdata.wc;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

// 批处理
public class WordCount {
public static void main(String[] args) throws Exception {
// 创建执行环境
ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

// 从文件中读取数据
String inputPath = "D:\\learn\\bigdata\\FlinkTutorial\\src\\main\\resources\\hello.txt";
DataSet<String> inputDataSet = env.readTextFile(inputPath);

// 对数据集进行处理,按空格分词展开,转换成(word, 1)二元组进行统计
DataSet<Tuple2<String, Integer>> resultSet = inputDataSet.flatMap(new MyFlatMapper())
.groupBy(0) // 按照第一个位置的word分组
.sum(1); // 按照第二个位置上的数据求和
resultSet.print();
}

// 自定义类,实现FlatMapFunction接口
public static class MyFlatMapper implements FlatMapFunction<String, Tuple2<String, Integer>> {

@Override
public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
// 按空格分词
String[] words = value.split(" ");
// 遍历所有word,包成二元组输出
for (String word : words) {
out.collect(new Tuple2<>(word, 1));
}
}
}
}

输出

1
2
3
4
5
6
7
8
9
10
(flink,1)
(world,1)
(hello,3)
(and,1)
(fine,1)
(how,1)
(spark,1)
(you,3)
(are,1)
(thank,1)

3. 流处理实现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
package com.ifnxs.bigdata.wc;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.client.program.StreamContextEnvironment;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

// 流处理WordCount
public class StreamWordCount {
public static void main(String[] args) throws Exception {
// 创建流处理执行环境
StreamExecutionEnvironment env = StreamContextEnvironment.getExecutionEnvironment();

// 设置并行度,默认值 = 当前计算机的CPU逻辑核数(设置成1即单线程处理)
// env.setMaxParallelism(32);

// 从文件中读取数据
String inputPath = "D:\\learn\\bigdata\\FlinkTutorial\\src\\main\\resources\\hello.txt";
DataStream<String> inputDataStream = env.readTextFile(inputPath);

// 基于数据流进行转换计算
DataStream<Tuple2<String, Integer>> resultStream = inputDataStream.flatMap(new WordCount.MyFlatMapper())
.keyBy(item -> item.f0)
.sum(1);
resultStream.print();

// 执行任务
env.execute();
}
}

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
1> (spark,1)
8> (are,1)
11> (how,1)
10> (you,1)
9> (world,1)
13> (flink,1)
5> (hello,1)
5> (hello,2)
5> (hello,3)
9> (fine,1)
6> (thank,1)
10> (you,2)
10> (you,3)
15> (and,1)

每行开头的数字代表执行的线程,比如我的电脑是8核心16线程,默认并行度为16

同一个字符串,前面输出的编号是一样的,因为key => hashcode,同一个key的hash值固定,分配给相对应的线程处理

4. 模拟流实现

linux系统通过nc -lk <port>打开一个socket服务,用于模拟实时的流数据

1
nc -lk 7777

如果CentOS没有安装nc可以通过下边的命令安装

1
yum -y install nc

代码修改如下

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
package com.ifnxs.bigdata.wc;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.client.program.StreamContextEnvironment;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

// 流处理WordCount
public class StreamWordCount {
public static void main(String[] args) throws Exception {
// 创建流处理执行环境
StreamExecutionEnvironment env = StreamContextEnvironment.getExecutionEnvironment();

// 设置并行度,默认值 = 当前计算机的CPU逻辑核数(设置成1即单线程处理)
// env.setMaxParallelism(32);

// 从文件中读取数据
// String inputPath = "D:\\learn\\bigdata\\FlinkTutorial\\src\\main\\resources\\hello.txt";
// DataStream<String> inputDataStream = env.readTextFile(inputPath);

// 从socket文本流读取数据
DataStream<String> inputDataStream = env.socketTextStream("localhost", 7777);

// 基于数据流进行转换计算
DataStream<Tuple2<String, Integer>> resultStream = inputDataStream.flatMap(new WordCount.MyFlatMapper())
.keyBy(item -> item.f0)
.sum(1);
resultStream.print();

// 执行任务
env.execute();
}
}

执行代码,然后发送socket消息,查看运行情况

因工作不需要笔记暂停

参考:尚硅谷Flink入门到实战-学习笔记