当前位置:网站首页>Flink的sink实战之一:初探
Flink的sink实战之一:初探
2020-11-08 16:12:00 【程序员欣宸】
欢迎访问我的GitHub
https://github.com/zq2599/blog_demos
内容:所有原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;
关于sink
下图来自Flink官方,红框中就是sink,可见实时数据从Source处开始,在Transformation阶段完成业务逻辑后在sink结束,因此sink可以用来处理计算结果,例如控制台输出或者保存数据库: 
关于《Flink的sink实战》系列文章
本文是《Flink的sink实战》的第一篇,旨在初步了解sink,通过对基本API和addSink方法的分析研究,为后续的编码实战打好基础;
全系列链接
从一段实例代码开始
- 下面是个简单的flink应用代码,红框中的print方法就是sink操作:

- 下图是官方给出的sink方式,都是DataStream类的API,直接调用即可实现sink,刚才代码中的print就是其中一个:

- 接下来看看上图中API的源码,先看print方法,在DataStream.java中,如下,实际上是调用了addSink方法,入参是PrintSinkFunction:

- 另一个常用API是writeAsText,源码如下,调用了writeUsingOutputFormat方法:

- 追踪<font color="blue">writeUsingOutputFormat</font>发现也是调用了addSink,入参是<font color="blue">OutputFormatSinkFunction</font>:

- print和writeAsText背后都在调用addSink,那么另一个常用的<font color="blue">writeAsCsv</font>方法呢?莫非也是调用addSink?打开一看果然,和writeAsText一样调用了<font color="blue">writeUsingOutputFormat</font>,而该方法里面就是在调用addSink:

- 综上所述,data sink的关键就是<font color="blue">addSink</font>的入参,即<font color="blue">SinkFunction</font>接口的实现,通过类图直观看到常见的sink能力是如何实现的:
8. 从上图可见抽象类<font color="blue">RichSinkFunction</font>与各种sink能力的关系十分紧密,我们应该重点关注它,在类图上展示方法签名,如下图:
9. 如上图所示,<font color="blue">RichSinkFunction</font>本身没有内容,但是它实现<font color="blue">SinkFunction</font>,继承<font color="blue">AbstractRichFunction</font>,是<font color="blue">RichFunction</font>和<font color="blue">SinkFunction</font>这两种特性的结合; 10. <font color="blue">RichFunction</font>的特性在前面的《Flink的DataSource三部曲》中已经了解,就是资源的open和close; 11. <font color="blue">SinkFunction</font>的特性呢?显然是用来处理计算结果的,类图上显示的是两个invoke方法,来看看官方的<font color="blue">PrintSinkFunction.java</font>:
12. <font color="blue">writer.write(record)</font>的源码在PrintSinkOutputWriter.java,如下所示: 
小结
至此,我们已经对Flink的sink有了基本了解:
- 负责实时计算结果的处理(如输出或持久化);
- 主要实现方式是调用DataStream.addSink方法;
- 各种sink能力的实现,主要途径是实现addSink方法的入参定义的接口;
后面的章节,一起进行sink方面的编码实战吧,实战的方向:体验官方提供的sink能力,自定义sink能力实现;
欢迎关注公众号:程序员欣宸
微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界... https://github.com/zq2599/blog_demos
版权声明
本文为[程序员欣宸]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4198380/blog/4707942
边栏推荐
- I used Python to find out all the people who deleted my wechat and deleted them automatically
- Xiaoqingtai officially set foot on the third day of no return
- WebGL 水波及焦散(刻蚀)的渲染总结
- Arduino IDE搭建ESP8266开发环境,文件下载过慢解决方法 | ESP-01制作WiFi开关教程,改造宿舍灯
- What are the necessary laws and regulations to know when entering the Internet?
- Flink from introduction to Zhenxiang (7. Sink data output file)
- 我用 Python 找出了删除我微信的所有人并将他们自动化删除了
- Development of uni app imitating wechat app
- 啥是数据库范式
- Solution of DEV-C + + unable to debug in Windows Environment
猜你喜欢

Millet and oppo continue to soar in the European market, and Xiaomi is even closer to apple

python基础教程python opencv pytesseract 验证码识别的实现

.NET 大数据量并发解决方案

CSP考试须知与各种小技巧

On the concurrency of update operation

Suitable for C / C + + novice learning some projects, do not give me to miss!

nat转换的ip跟端口ip不相同的解决方法

Flink: from introduction to Zhenxiang (3. Reading data from collection and file)

阿里云加速增长,进一步巩固领先优势

Comics: looking for the best time to buy and sell stocks
随机推荐
Elasticsearch learning one (basic introduction)
Introduction to mongodb foundation of distributed document storage database
大龄程序员没有出路吗?
Suitable for C / C + + novice learning some projects, do not give me to miss!
非常规聚合问题举例
Ubuntu20.04下访问FTP服务器乱码问题+上传文件
[开源] .Net 使用 ORM 访问 华为GaussDB数据库
How to make a correct summary for 7 years?
Implementation of verification code recognition in Python opencv pytesseract
这几个C++的坑,一旦踩中了,加班是肯定避免不了了!
Stm32uberide download and install - GPIO basic configuration operation - debug (based on CMSIS DAP debug)
3、 The parameters of the function
漫画:寻找股票买入卖出的最佳时机(整合版)
How to cooperate with people in software development? |Daily anecdotes
[Python 1-6] Python tutorial 1 -- number
10 common software architecture patterns
GopherChina 2020大会
DeepMind 最新论文解读:首次提出离散概率树中的因果推理算法
wanxin finance
B站stm32视频学习