当前位置:网站首页>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
边栏推荐
- Improvement of maintenance mode of laravel8 update
- Drink soda, a bottle of soda water 1 yuan, two empty bottles can change a bottle of soda, give 20 yuan, how much soda can you
- 阿里云的MaxCompute数加(原ODPS)用的怎样?
- How to make a correct summary for 7 years?
- AI weekly: employees are allowed to voluntarily reduce salary; company response: employees are happy and satisfied; tiger tooth HR takes employees out of the company; Sweden forbids Huawei ZTE 5g equi
- Golang ICMP Protocol detects viable hosts
- Learn to record and analyze
- What is the database paradigm
- This time Kwai tiktok is faster than shaking.
- 模板引擎的整理归纳
猜你喜欢
Don't release resources in finally, unlock a new pose!
技术总监7年总结,如何进行正确的沟通?
基于阿里云日志服务快速打造简版业务监控看板
Alibaba cloud accelerates its growth and further consolidates its leading edge
后端程序员必备:分布式事务基础篇
关于update操作并发问题
.NET 大数据量并发解决方案
Python basic syntax
On the software of express delivery cabinet and deposit cabinet under Windows
漫画:寻找股票买入卖出的最佳时机(整合版)
随机推荐
擅长To C的腾讯,如何借腾讯云在这几个行业云市场占有率第一?
Flink从入门到真香(3、从集合和文件中读取数据)
Drink soda, a bottle of soda water 1 yuan, two empty bottles can change a bottle of soda, give 20 yuan, how much soda can you
Returning to the third place in the world, what did Xiaomi do right?
What is SVG?
.NET 大数据量并发解决方案
nat转换的ip跟端口ip不相同的解决方法
What is the database paradigm
学习记录并且简单分析
Examples of unconventional aggregation
Tips and skills of CSP examination
wanxin finance
Golang ICMP协议探测存活主机
On the confirmation of original data assets
啥是数据库范式
Flink: from introduction to Zhenxiang (3. Reading data from collection and file)
Flink从入门到真香(7、Sink数据输出-文件)
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
打工人,打工魂,抽终身会员,成为人上人!
别再在finally里面释放资源了,解锁个新姿势!