当前位置:网站首页>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
边栏推荐
- Android Basics - check box
- 模板引擎的整理归纳
- 新型存算一体芯片诞生,利好人工智能应用~
- Talking about, check the history of which famous computer viruses, 80% of the people do not know!
- What are the necessary laws and regulations to know when entering the Internet?
- Python basic syntax
- 第五章编程题
- 进入互联网得知道的必备法律法规有哪些?
- Flink从入门到真香(3、从集合和文件中读取数据)
- Xiaoqingtai officially set foot on the third day of no return
猜你喜欢

laravel8更新之维护模式改进

Solution of DEV-C + + unable to debug in Windows Environment

The progress bar written in Python is so wonderful~
![[开源] .Net 使用 ORM 访问 华为GaussDB数据库](/img/f8/50715c25a9d49b010cba2ff442c04e.jpg)
[开源] .Net 使用 ORM 访问 华为GaussDB数据库

Huawei has an absolute advantage in the 5g mobile phone market, and the market share of Xiaomi is divided by the market survey organization

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

打工人,打工魂,抽终身会员,成为人上人!

Flink: from introduction to Zhenxiang (6. Flink implements UDF function - realizes more fine-grained control flow)

3、 The parameters of the function

进入互联网得知道的必备法律法规有哪些?
随机推荐
别再在finally里面释放资源了,解锁个新姿势!
我用 Python 找出了删除我微信的所有人并将他们自动化删除了
AI周报:允许“员工自愿降薪”;公司回应:员工内心高兴满意;虎牙HR将员工抬出公司;瑞典禁用华为中兴5G设备
关于adb连接手机offline的问题解决
It's just right. It's the ideal state
Do these mistakes in your resume affect your annual salary of one million?
Development of uni app imitating wechat app
Windows下快递投递柜、寄存柜的软件初探
二叉树的四种遍历方应用
I used Python to find out all the people who deleted my wechat and deleted them automatically
这几个C++的坑,一旦踩中了,加班是肯定避免不了了!
Millet and oppo continue to soar in the European market, and Xiaomi is even closer to apple
适合c/c++新手学习的一些项目,别给我错过了!
Chapter 5 programming
Flink from introduction to Zhenxiang (7. Sink data output file)
Eight ways to optimize if else code
构建者模式(Builder pattern)
Builder pattern
Stm32uberide download and install - GPIO basic configuration operation - debug (based on CMSIS DAP debug)
python基础教程python opencv pytesseract 验证码识别的实现