2024年大数据最新【Flink】(05)Apache Flink 漫谈系列 —,2024年最新征服大数据开发面试官

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

     if (maxNumRetries == -1 || attempt < maxNumRetries) {
        LOG.warn("Lost connection to server socket. Retrying in " + delayBetweenRetries + " msecs...");
        Thread.sleep(delayBetweenRetries);
     }
     else {
        break;
     }
  }

}
/** 在最外层的循环都退出后,最后检查下缓存中是否还有数据,如果有,则向下游转发 */
if (buffer.length() > 0) {
ctx.collect(buffer.toString());
}
}


`run` 方法的逻辑如上,逻辑很清晰,就是从指定的hostname和port持续不断的读取数据,按行分隔符划分成一个个字符串,然后转发到下游。


`cancel` 方法的实现如下,就是将运行状态的标识isRunning属性设置为false,并根据需要关闭当前socket。



public void cancel() {
isRunning = false;
Socket theSocket = this.currentSocket;
/** 如果当前socket不为null,则进行关闭操作 */
if (theSocket != null) {
IOUtils.closeSocket(theSocket);
}
}


对SocketTextStreamFunction的实现清楚之后,回到 StreamExecutionEnvironment 中,看 `addSource` 方法。



public DataStreamSource addSource(SourceFunction function, String sourceName) {
return addSource(function, sourceName, null);
}

public DataStreamSource addSource(SourceFunction function, String sourceName, TypeInformation typeInfo) {
/** 如果传入的输出数据类型信息为null,则尝试提取输出数据的类型信息 */
if (typeInfo == null) {
if (function instanceof ResultTypeQueryable) {
/** 如果传入的function实现了ResultTypeQueryable接口, 则直接通过接口获取 */
typeInfo = ((ResultTypeQueryable) function).getProducedType();
} else {
try {
/** 通过反射机制来提取类型信息 */
typeInfo = TypeExtractor.createTypeInfo(
SourceFunction.class,
function.getClass(), 0, null, null);
} catch (final InvalidTypesException e) {
/** 提取失败, 则返回一个MissingTypeInfo实例 */
typeInfo = (TypeInformation) new MissingTypeInfo(sourceName, e);
}
}
}
/** 根据function是否是ParallelSourceFunction的子类实例来判断是否是一个并行数据源节点 */
boolean isParallel = function instanceof ParallelSourceFunction;
/** 闭包清理, 可减少序列化内容, 以及防止序列化出错 */
clean(function);
StreamSource<OUT, ?> sourceOperator;
/** 根据function是否是StoppableFunction的子类实例, 来决定构建不同的StreamOperator */
if (function instanceof StoppableFunction) {
sourceOperator = new StoppableStreamSource<>(cast2StoppableSourceFunction(function));
} else {
sourceOperator = new StreamSource<>(function);
}
/** 返回一个新构建的DataStreamSource实例 */
return new DataStreamSource<>(this, typeInfo, sourceOperator, isParallel, sourceName);
}


通过对 `addSource` 重载方法的依次调用,最后得到了一个 `DataStreamSource` 的实例。


`TypeInformation` 是Flink的类型系统中的核心类,用作函数输入和输出的类型都需要通过`TypeInformation`来表示,`TypeInformation`可以看做是数据类型的一个工具,可以通过它获取对应数据类型的序列化器和比较器等。


由于SocketTextStreamFunction不是继承自ParallelSourceFunction,且实现stoppableFunction接口,isParallel的值为false,以及sourceOperator变量对应的是一个StreamSource实例。


`StreamSource` 的类继承图如下所示:


![在这里插入图片描述](https://img-blog.csdnimg.cn/20200712125240438.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0JlaWlzQmVp,size_1,color_FFFFFF,t_70#pic_center)


上图可以看出 `StreamSource` 是 `StreamOperator` 接口的一个具体实现类,其构造函数的入参就是一个 `SourceFunction` 的子类实例,这里就是前面介绍过的


`SocketTextStreamFunciton` 的实例,构造过程如下:



public StreamSource(SRC sourceFunction) {
super(sourceFunction);
this.chainingStrategy = ChainingStrategy.HEAD;
}

public AbstractUdfStreamOperator(F userFunction) {
this.userFunction = requireNonNull(userFunction);
checkUdfCheckpointingPreconditions();
}

private void checkUdfCheckpointingPreconditions() {
if (userFunction instanceof CheckpointedFunction && userFunction instanceof ListCheckpointed) {
throw new IllegalStateException(“User functions are not allowed to implement AND ListCheckpointed.”);
}
}


把传入的 `userFunction` 赋值给自己的属性变量,并对传入的 `userFunction` 做了校验工作,然后将链接策略设置为HEAD。


Flink中为了优化执行效率,会对数据处理链中的相邻节点会进行合并处理,链接策略有三种:


* ALWAYS —— 尽可能的与前后节点进行链接;
* NEVER —— 不与前后节点进行链接;
* HEAD —— 只能与后面的节点链接,不能与前面的节点链接。


作为数据源的源头,是最顶端的节点了,所以只能采用HEAD或者NEVER,对于`StreamSource`,采用的是HEAD策略。


`StreamOperator` 是Flink中流操作符的基础接口,其抽象子类 `AbstractStreamOperator` 实现了一些公共方法,用户自定义的数据处理逻辑会被封装在 `StreamOperator` 的具体实现子类中。


在 `sourceOperator` 变量被赋值后,即开始进行 `DataStreamSource` 的实例构建,并作为数据源构造调用的返回结果。



return new DataStreamSource<>(this, typeInfo, sourceOperator, isParallel, sourceName);


`DataStreamSource` 的类继承图如下所示,是具有一个预定义输出类型的 `DataStream`。


![在这里插入图片描述](https://img-blog.csdnimg.cn/20200712131227744.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0JlaWlzQmVp,size_1,color_FFFFFF,t_70#pic_center)  
 在Flink中,DataStream描述了一个具有相同数据类型的数据流,其提供了数据操作的各种API,如map、reduce等,通过这些API,可以对数据流中的数据进行各种操作,DataStreamSource的构建过程如下:



public DataStreamSource(StreamExecutionEnvironment environment,
TypeInformation outTypeInfo, StreamSource<T, ?> operator,
boolean isParallel, String sourceName) {
super(environment, new SourceTransformation<>(sourceName, operator, outTypeInfo, environment.getParallelism()));
this.isParallel = isParallel;
if (!isParallel) {
setParallelism(1);
}
}

protected SingleOutputStreamOperator(StreamExecutionEnvironment environment, StreamTransformation transformation) {
super(environment, transformation);
}

public DataStream(StreamExecutionEnvironment environment, StreamTransformation transformation) {
this.environment = Preconditions.checkNotNull(environment, “Execution Environment must not be null.”);
this.transformation = Preconditions.checkNotNull(transformation, “Stream Transformation must not be null.”);
}


可见构建过程就是初始化了DataStream中的`environment`和`transformation`这两个属性。


其中 `transformation` 赋值的是 `SourceTranformation` 的一个实例,`SourceTransformation`是 `StreamTransformation` 的子类,而`StreamTransformation`则描述了创建一个`DataStream`的操作。对于每个DataStream,其底层都是有一个StreamTransformation的具体实例的,所以在DataStream在构造初始时会为其属性transformation设置一个具体的实例。并且DataStream的很多接口的调用都是直接调用的StreamTransformation的相应接口,如并行度、id、输出数据类型信息、资源描述等。


通过上述过程,根据指定的hostname和port进行数据产生的数据源就构造完成了,获得的是一个`DataStreamSource`的实例,描述的是一个输出数据类型是String的数据流的源。


在上述的数据源的构建过程中,出现 **Function(SourceFunction)、StreamOperator、StreamTransformation、DataStream** 这四个接口:


* **Function**接口:用户通过继承该接口的不同子类来实现用户自己的数据处理逻辑,如上述中实现了SourceFunction这个子类,来实现从指定hostname和port来接收数据,并转发字符串的逻辑;
* **StreamOperator**接口:数据流操作符的基础接口,该接口的具体实现子类中,会有保存用户自定义数据处理逻辑的函数的属性,负责对userFunction的调用,以及调用时传入所需参数,比如在StreamSource这个类中,在调用SourceFunction的run方法时,会构建一个SourceContext的具体实例,作为入参,用于run方法中,进行数据的转发;
* **StreamTransformation**接口:该接口描述了构建一个DataStream的操作,以及该操作的并行度、输出数据类型等信息,并有一个属性,用来持有StreamOperator的一个具体实例;
* **DataStream**:描述的是一个具有相同数据类型的数据流,底层是通过具体的StreamTransformation来实现,其负责提供各种对流上的数据进行操作转换的API接口。


通过上述的关系,最终用户自定义数据处理逻辑的函数,以及并行度、输出数据类型等就都包含在了DataStream中,而DataStream也就可以很好的描述一个具体的数据流了。


上述四个接口的包含关系是这样的:`Function –> StreamOperator –> StreamTransformation –> DataStream`。


通过数据源的构造,理清Flink数据流中的几个接口的关系后,接下来再来看如何在数据源上进行各种操作,达到最终的数据统计分析的目的。


### 四、操作数据流


进行具体的转换操作:



DataStream windowCounts = text
.flatMap(new FlatMapFunction<String, WordWithCount>() {
@Override
public void flatMap(String value, Collector out) {
for (String word : value.split(“\s”)) {
out.collect(new WordWithCount(word, 1L));
}
}
})
.keyBy(“word”)
.timeWindow(Time.seconds(5), Time.seconds(1))
.reduce(new ReduceFunction() {
@Override
public WordWithCount reduce(WordWithCount a, WordWithCount b) {
return new WordWithCount(a.word, a.count + b.count);
}
});


**这段逻辑中,对数据流做了四次操作,分别是flatMap、keyBy、timeWindow、reduce,接下来分别介绍每个转换都做了些什么操作。**


#### 4.1 flatMap 转换


`flatMap`的入参是一个`FlatMapFunction`的具体实现,功能就是将接收到的字符串,按空格切割成不同单词,然后每个单词构建一个WordWithCount实例,然后向下游转发,用于后续的数据统计。然后调用DataStream的flatMap方法,进行数据流的转换,如下:



public SingleOutputStreamOperator flatMap(FlatMapFunction<T, R> flatMapper) {
TypeInformation outType = TypeExtractor.getFlatMapReturnTypes(clean(flatMapper),
getType(), Utils.getCallLocationName(), true);
/** 根据传入的flatMapper这个Function,构建StreamFlatMap这个StreamOperator的具体子类实例 */
return transform(“Flat Map”, outType, new StreamFlatMap<>(clean(flatMapper)));
}

public SingleOutputStreamOperator transform(String operatorName, TypeInformation outTypeInfo, OneInputStreamOperator<T, R> operator) {
/** 读取输入转换的输出类型, 如果是MissingTypeInfo, 则及时抛出异常, 终止操作 */
transformation.getOutputType();
OneInputTransformation<T, R> resultTransform = new OneInputTransformation<>(
this.transformation,
operatorName,
operator,
outTypeInfo,
environment.getParallelism());
@SuppressWarnings({ “unchecked”, “rawtypes” })
SingleOutputStreamOperator returnStream = new SingleOutputStreamOperator(environment, resultTransform);
getExecutionEnvironment().addOperator(resultTransform);
return returnStream;
}


整个构建过程,与构建数据源的过程相似。



> 
> a、先根据传入的flatMapper这个Function构建一个StreamOperator的具体子类StreamFlatMap的实例;  
>  b、根据a中构建的StreamFlatMap的实例,构建出OneInputTransFormation这个StreamTransformation的子类的实例;  
>  c、再构建出DataStream的子类SingleOutputStreamOperator的实例。
> 
> 
> 


除了构建出了 `SingleOutputStreamOperator` 这个实例为并返回外,还有代码:



getExecutionEnvironment().addOperator(resultTransform);

public void addOperator(StreamTransformation<?> transformation) {
Preconditions.checkNotNull(transformation, “transformation must not be null.”);
this.transformations.add(transformation);
}


就是将上述构建的`OneInputTransFormation`的实例,添加到了`StreamExecutionEnvironment`的属性`transformations`这个类型为`List`。


#### 4.2 keyBy 转换


这里的keyBy转换,入参是一个字符串”word”,意思是按照WordWithCount中的word字段进行分区操作。



public KeyedStream<T, Tuple> keyBy(String… fields) {
return keyBy(new Keys.ExpressionKeys<>(fields, getType()));
}


先根据传入的字段名数组,以及数据流的输出数据类型信息,构建出用来描述key的ExpressionKeys的实例,ExpressionKeys有两个属性:



/** key字段的列表, FlatFieldDescriptor 描述了每个key, 在所在类型中的位置以及key自身的数据类信息 */
private List keyFields;
/** 包含key的数据类型的类型信息, 与构造函数入参中的字段顺序一一对应 */
private TypeInformation<?>[] originalKeyTypes;


在获取key的描述之后,继续调用keyBy的重载方法:



private KeyedStream<T, Tuple> keyBy(Keys keys) {
return new KeyedStream<>(this, clean(KeySelectorUtil.getSelectorForKeys(keys,
getType(), getExecutionConfig())));
}


这里首先构建了一个KeySelector的子类ComparableKeySelector的实例,作用就是从具体的输入实例中,提取出key字段对应的值(可能是多个key字段)组成的元组(Tuple)。


对于这里的例子,就是从每个WordWithCount实例中,提取出word字段的值。


然后构建一个KeyedStream的实例,KeyedStream也是DataStream的子类。构建过程如下:



public KeyedStream(DataStream dataStream, KeySelector<T, KEY> keySelector) {
this(dataStream, keySelector, TypeExtractor.getKeySelectorTypes(keySelector, dataStream.getType()));
}

public KeyedStream(DataStream dataStream, KeySelector<T, KEY> keySelector, TypeInformation keyType) {
super(
dataStream.getExecutionEnvironment(),
new PartitionTransformation<>(
dataStream.getTransformation(),
new KeyGroupStreamPartitioner<>(keySelector, StreamGraphGenerator.DEFAULT_LOWER_BOUND_MAX_PARALLELISM)));
this.keySelector = keySelector;
this.keyType = validateKeyType(keyType);
}


在进行父类构造函数调用之前,先基于keySelector构造了一个KeyGroupStreamPartitioner的实例,再进一步构造了一个PartitionTransformation实例。


这里与flatMap的转换略有不同:



> 
> a、flatMap中,根据传入的flatMapper这个Function构建的是StreamOperator这个接口的子类的实例,而keyBy中,则是根据keySelector构建了ChannelSelector接口的子类实例;  
>  b、keyBy中构建的StreamTransformation实例,并没有添加到StreamExecutionEnvironment的属性transformations这个列表中。
> 
> 
> 


ChannelSelector只有一个接口,根据传入的数据流中的具体数据记录,以及下个节点的并行度来决定该条记录需要转发到哪个通道。



public interface ChannelSelector {
int[] selectChannels(T record, int numChannels);
}
KeyGroupStreamPartitioner中该方法的实现如下:
public int[] selectChannels(
SerializationDelegate<StreamRecord> record,
int numberOfOutputChannels) {
K key;
try {
/** 通过keySelector从传入的record中提取出对应的key */
key = keySelector.getKey(record.getInstance().getValue());
} catch (Exception e) {
throw new RuntimeException("Could not extract key from " + record.getInstance().getValue(), e);
}
/** 根据提取的key,最大并行度,以及输出通道数,决定出record要转发到的通道编号 */
returnArray[0] = KeyGroupRangeAssignment.assignKeyToParallelOperator(key, maxParallelism, numberOfOutputChannels);
return returnArray;
}


再进一步看一下KeyGroupRangerAssignment的assignKeyToParallelOperator方法的实现逻辑。



public static int assignKeyToParallelOperator(Object key, int maxParallelism, int parallelism) {
return computeOperatorIndexForKeyGroup(maxParallelism, parallelism, assignToKeyGroup(key, maxParallelism));
}

public static int assignToKeyGroup(Object key, int maxParallelism) {
return computeKeyGroupForKeyHash(key.hashCode(), maxParallelism);
}

public static int computeKeyGroupForKeyHash(int keyHash, int maxParallelism) {
return MathUtils.murmurHash(keyHash) % maxParallelism;
}

public static int computeOperatorIndexForKeyGroup(int maxParallelism, int parallelism, int keyGroupId) {
return keyGroupId * parallelism / maxParallelism;
}



> 
> a、先通过key的hashCode,算出maxParallelism的余数,也就是可以得到一个[0, maxParallelism)的整数;  
>  b、在通过公式 keyGroupId \* parallelism / maxParallelism ,计算出一个[0, parallelism)区间的整数,从而实现分区功能。
> 
> 
> 


#### 4.3 timeWindow 转换


这里timeWindow转换的入参是两个时间,第一个参数表示窗口长度,第二个参数表示窗口滑动的时间间隔。



public WindowedStream<T, KEY, TimeWindow> timeWindow(Time size, Time slide) {
if (environment.getStreamTimeCharacteristic() == TimeCharacteristic.ProcessingTime) {
return window(SlidingProcessingTimeWindows.of(size, slide));
} else {
return window(SlidingEventTimeWindows.of(size, slide));
}
}


根据环境配置的数据流处理时间特征构建不同的WindowAssigner的具体实例。


WindowAssigner的功能就是对于给定的数据流中的记录,决定出该记录应该放入哪些窗口中,并提供触发器等供。默认的时间特征是ProcessingTime,所以这里会构建一个SlidingProcessingTimeWindow实例,来看下SlidingProcessingTimeWindow类的assignWindows方法的实现逻辑。



public Collection assignWindows(Object element, long timestamp, WindowAssignerContext context) {
/** 根据传入的WindowAssignerContext获取当前处理时间 */
timestamp = context.getCurrentProcessingTime();
List windows = new ArrayList<>((int) (size / slide));
/** 获取最近一次的窗口的开始时间 */
long lastStart = TimeWindow.getWindowStartWithOffset(timestamp, offset, slide);
/** 循环找出满足条件的所有窗口 */
for (long start = lastStart;
start > timestamp - size;
start -= slide) {
windows.add(new TimeWindow(start, start + size));
}
return windows;
}


看一下根据给定时间戳获取最近一次的窗口的开始时间的实现逻辑。



public static long getWindowStartWithOffset(long timestamp, long offset, long windowSize) {
return timestamp - (timestamp - offset + windowSize) % windowSize;
}


通过一个例子来解释上述代码的逻辑。比如:



> 
> a、timestamp = 1520406257000 // 2018-03-07 15:04:17  
>  b、offset = 0  
>  c、windowSize = 60000  
>  d、(timestamp - offset + windowSize) % windowSize = 17000  
>  e、说明在时间戳 1520406257000 之前最近的窗口是在 17000 毫秒的地方  
>  f、timestamp - (timestamp - offset + windowSize) % windowSize = 1520406240000 // 2018-03-07 15:04:00  
>  g、这样就可以保证每个时间窗口都是从整点开始, 而offset则是由于时区等原因需要时间调整而设置。
> 
> 
> 


通过上述获取WindowAssigner的子类实例后,调用window方法:



public WindowedStream<T, KEY, W> window(WindowAssigner<? super T, W> assigner) {
return new WindowedStream<>(this, assigner);
}


比keyBy转换的逻辑还简单,就是构建了一个WindowedStream实例,然后返回,就结束了。而WindowedStream是一个新的数据流,不是DataStream的子类。


WindowedStream描述一个数据流中的元素会基于key进行分组,并且对于每个key,对应的元素会被划分到多个时间窗口内。然后窗口会基于触发器,将对应窗口中的数据转发到下游节点。


#### 4.4 reduce 转换


reduce转换的入参是一个ReduceFunction的具体实现,这里的逻辑就是对收到的WordWithCount实例集合,将其中word字段相同的实际的count值累加。



public SingleOutputStreamOperator reduce(ReduceFunction function) {
if (function instanceof RichFunction) {
throw new UnsupportedOperationException("ReduceFunction of reduce can not be a RichFunction. " +
“Please use reduce(ReduceFunction, WindowFunction) instead.”);
}
/** 闭包清理 */
function = input.getExecutionEnvironment().clean(function);
return reduce(function, new PassThroughWindowFunction<K, W, T>());
}

public SingleOutputStreamOperator reduce(
ReduceFunction reduceFunction,
WindowFunction<T, R, K, W> function) {

TypeInformation inType = input.getType();
TypeInformation resultType = getWindowFunctionReturnType(function, inType);
return reduce(reduceFunction, function, resultType);
}

public SingleOutputStreamOperator reduce(ReduceFunction reduceFunction, ProcessWindowFunction<T, R, K, W> function, TypeInformation resultType) {
if (reduceFunction instanceof RichFunction) {
throw new UnsupportedOperationException(“ReduceFunction of apply can not be a RichFunction.”);
}
function = input.getExecutionEnvironment().clean(function);
reduceFunction = input.getExecutionEnvironment().clean(reduceFunction);
String callLocation = Utils.getCallLocationName();
String udfName = “WindowedStream.” + callLocation;
String opName;
KeySelector<T, K> keySel = input.getKeySelector();
OneInputStreamOperator<T, R> operator;
if (evictor != null) {
@SuppressWarnings({“unchecked”, “rawtypes”})
TypeSerializer<StreamRecord> streamRecordSerializer =
(TypeSerializer<StreamRecord>) new StreamElementSerializer(input.getType().createSerializer(getExecutionEnvironment().getConfig()));
ListStateDescriptor<StreamRecord> stateDesc =
new ListStateDescriptor<>(“window-contents”, streamRecordSerializer);
opName = “TriggerWindow(” + windowAssigner + ", " + stateDesc + ", " + trigger + ", " + evictor + ", " + udfName + “)”;
operator =

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

stateDesc =
new ListStateDescriptor<>(“window-contents”, streamRecordSerializer);
opName = “TriggerWindow(” + windowAssigner + ", " + stateDesc + ", " + trigger + ", " + evictor + ", " + udfName + “)”;
operator =

[外链图片转存中…(img-LrZvRvHx-1715263136222)]
[外链图片转存中…(img-F7xXOUQe-1715263136222)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值