当前位置:网站首页>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
边栏推荐
- This paper analyzes the top ten Internet of things applications in 2020!
- On the concurrency of update operation
- 啥是数据库范式
- nat转换的ip跟端口ip不相同的解决方法
- laravel8更新之维护模式改进
- 后端程序员必备:分布式事务基础篇
- laravel8更新之速率限制改进
- What is the database paradigm
- 谷歌开源能翻译101种语言的AI模型,只比Facebook多一种
- I used Python to find out all the people who deleted my wechat and deleted them automatically
猜你喜欢

Do these mistakes in your resume affect your annual salary of one million?

laravel8更新之速率限制改进

Welcome to offer, grade P7, face-to-face sharing, 10000 words long text to take you through the interview process
![[Python 1-6] Python tutorial 1 -- number](/img/3b/00bc81122d330c9d59909994e61027.jpg)
[Python 1-6] Python tutorial 1 -- number

Essential for back-end programmers: distributed transaction Basics

Recurrence of Apache kylin Remote Code Execution Vulnerability (cve-2020-1956)

二叉树的四种遍历方应用

3、 The parameters of the function

【Python 1-6】Python教程之——数字

Interpretation of deepmind's latest paper: the causal reasoning algorithm in discrete probability tree is proposed for the first time
随机推荐
Builder pattern
android基础-CheckBox(复选框)
Eight ways to optimize if else code
打工人,打工魂,抽终身会员,成为人上人!
学习记录并且简单分析
Gopherchina 2020 Conference
软件开发中如何与人协作? | 每日趣闻
Chapter 5 programming
我用 Python 找出了删除我微信的所有人并将他们自动化删除了
How does Alibaba cloud's maxcompute add (original ODPs) work?
阿里云视频云技术专家 LVS 演讲全文:《“云端一体”的智能媒体生产制作演进之路》
Ali tear off the e-commerce label
Millet and oppo continue to soar in the European market, and Xiaomi is even closer to apple
Alibaba cloud accelerates its growth and further consolidates its leading edge
Interpretation of deepmind's latest paper: the causal reasoning algorithm in discrete probability tree is proposed for the first time
擅长To C的腾讯,如何借腾讯云在这几个行业云市场占有率第一?
Blockchain weekly: the development of digital currency is written into the 14th five year plan; Biden invited senior adviser of MIT digital currency program to join the presidential transition team; V
Suitable for C / C + + novice learning some projects, do not give me to miss!
AQS解析
Flink从入门到真香(6、Flink实现UDF函数-实现更细粒度的控制流)