当前位置:网站首页>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
边栏推荐
- Builder pattern
- rabbitmq(一)-基础入门
- I used Python to find out all the people who deleted my wechat and deleted them automatically
- 构建者模式(Builder pattern)
- Talking about, check the history of which famous computer viruses, 80% of the people do not know!
- Examples of unconventional aggregation
- This year's salary is 35W +! Why is the salary of Internet companies getting higher and higher?
- This time Kwai tiktok is faster than shaking.
- GopherChina 2020大会
- Golang ICMP Protocol detects viable hosts
猜你喜欢
随机推荐
vim-配置教程+源码
区块链周报:数字货币发展写入十四五规划;拜登邀请MIT数字货币计划高级顾问加入总统过渡团队;委内瑞拉推出国营加密交易所
华为在5G手机市场占据绝对优势,市调机构对小米的市占出现分歧
How to solve the difference between NAT IP and port IP
What is SVG?
Eight ways to optimize if else code
进入互联网得知道的必备法律法规有哪些?
重返全球第三,小米做对了什么?
CSP考试须知与各种小技巧
How to cooperate with people in software development? |Daily anecdotes
I used Python to find out all the people who deleted my wechat and deleted them automatically
阿里云加速增长,进一步巩固领先优势
.NET 大数据量并发解决方案
Ubuntu20.04下访问FTP服务器乱码问题+上传文件
Google's AI model, which can translate 101 languages, is only one more than Facebook
AQS解析
Comics: looking for the best time to buy and sell stocks
Returning to the third place in the world, what did Xiaomi do right?
Flink从入门到真香(7、Sink数据输出-文件)
Chapter 5 programming