前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Flink 自定义source、sink 是如何起作用的

Flink 自定义source、sink 是如何起作用的

作者头像
shengjk1
发布2019-06-25 10:37:58
1.6K0
发布2019-06-25 10:37:58
举报
文章被收录于专栏:码字搬砖

自从学会自定义source之后,一直都比较好奇,为什么我实现一个 *SourceFunction,我自己定义的代码就可以跟 Flink很好的整合在一起? 下面以 RichParallelSourceFunction 为例,来具体看一下究竟是自定义 source 是如何执行的

首先看一下 Flink中的抽象类 AbstractUdfStreamOperator,专门负责Rich*Function的 open 和close方法

代码语言:javascript
复制
......

	// flink 提供的 Rich*Function 系列算子的 open 和 close 方法被执行的地方
	@Override
	public void open() throws Exception {
		super.open();
//关键性方法 负责执行我们重写的open方法
		FunctionUtils.openFunction(userFunction, new Configuration());
	}

//关键性方法 负责执行我们重写的close方法
	@Override
	public void close() throws Exception {
		super.close();
		functionsClosed = true;
		FunctionUtils.closeFunction(userFunction);
	}
......

再继续看一下StreamSource

代码语言:javascript
复制
......
//生成上下文之后,接下来就是把上下文交给 SourceFunction 去执行,调用用户重写的run方法开始正式运行
			userFunction.run(ctx);

			// if we get here, then the user function either exited after being done (finite source)
			// or the function was canceled or stopped. For the finite source case, we should emit
			// a final watermark that indicates that we reached the end of event-time
			if (!isCanceledOrStopped()) {
				ctx.emitWatermark(Watermark.MAX_WATERMARK);
			}
......

//执行我们自己重写的 cancel 方法
public void cancel() {
		// important: marking the source as stopped has to happen before the function is stopped.
		// the flag that tracks this status is volatile, so the memory model also guarantees
		// the happens-before relationship
		markCanceledOrStopped();
		userFunction.cancel();

		// the context may not be initialized if the source was never running.
		if (ctx != null) {
			ctx.close();
		}
	}
......

自此为止,我们自定义source function 的 open、close、cancel、run方法就都可以正常的调用运行了,然后就可以源源不断的产生数据了。

sink也是类似的。首先通过AbstractUdfStreamOperator类调用 open、close方法,然后还有 StreamSink调用 自定义中的 invoke 方法。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019年06月24日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
大数据
全栈大数据产品,面向海量数据场景,帮助您 “智理无数,心中有数”!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档