当前位置:网站首页>Flink function (1): rich function
Flink function (1): rich function
2022-07-24 06:16:00 【sf_ www】
rich function, namely “ rich ” function , That is, compared with the general function Come on , It provides more functions . It defines some methods of function life cycle , And you can get the context of the function runtime . There are methods open, close, getRuntimeContext, setRuntimeContext. What is particularly useful is that we can get keyed state, To do more control , such as keyed State Of TTL Set up .(State It's using RuntimeContext Access to the , Therefore, it can only be used in rich functions )
All transformations that require user-defined functions can convert rich Function as parameter (All transformations that require a user-defined function can instead take as argument a rich function. ) because RichFunction extends Function.
The following is a description of some useful places in the official documents :
Rich functions provide, in addition to the user-defined function (map, reduce, etc), four methods: open, close, getRuntimeContext, and setRuntimeContext. These are useful for parameterizing the function (see Passing Parameters to Functions), creating and finalizing local state, accessing broadcast variables (see Broadcast Variables), and for accessing runtime information such as accumulators and counters (see Accumulators and Counters), and information on iterations (see Iterations).
The key functions are described below :
void open(Configuration parameters) throws Exception;In function (function, such as map join etc. ) Before calling ,open() Method (method) First called , Used to initialize the operation , Therefore, it is suitable for one-time setting . As part of the function iteration , This method will be called before each upper iteration (For functions that are part of an iteration, this method will be invoked at the beginning of each iteration uperstep.).
void close() throws Exception;Is the last method called in the life cycle , Do some cleaning .
RuntimeContext getRuntimeContext();Get runtime context
void setRuntimeContext(RuntimeContext t);Set runtime context . Each parallel operator subtask has a runtime context , The context records some information about the operation of this operator , Including the current parallelism of the operator 、 Operator subtask sequence number 、 Broadcast data 、 accumulator 、 Monitoring data . most important of all , We can get state data from context ;
Example implementation Count Of TTL Set up
package com.xx.bigdata.flink;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.state.StateTtlConfig;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
public class TTLCountMapFunction extends RichMapFunction<Tuple2<String, Integer>, Tuple2<String, Integer>> {
private transient ValueState<Integer> state;
@Override
public Tuple2<String, Integer> map(Tuple2<String, Integer> in) throws Exception {
Integer currentCount = state.value();
if(null == currentCount) {
currentCount = 0;
}
Integer nowCount = currentCount + in.f1;
state.update(nowCount);
return new Tuple2<>(in.f0, nowCount);
}
public void open(Configuration parameters) throws Exception {
StateTtlConfig ttlConfig = StateTtlConfig
.newBuilder(Time.seconds(300))
.setUpdateType(StateTtlConfig.UpdateType.OnCreateAndWrite)
.setStateVisibility(StateTtlConfig.StateVisibility.NeverReturnExpired)
.build();
ValueStateDescriptor<Integer> stateDescriptor = new ValueStateDescriptor<>("count-state", Integer.class);
stateDescriptor.enableTimeToLive(ttlConfig);
state = getRuntimeContext().getState(stateDescriptor);
}
}边栏推荐
- Sequential stack C language stack entry and exit traversal
- Basic knowledge of unity and the use of some basic APIs
- Write the list to txt and directly remove the comma in the middle
- ue4换装系统 1.换装系统的基本原理
- IP课(OSPF)综合实验
- 公网使用Microsoft Remote Desktop远程桌面,随时远程办公
- 不租服务器,自建个人商业网站(4)
- 常见十大漏洞总结(原理、危害、防御)
- 如何建立一个仪式感点满的网站,并发布到公网 2-2
- Unity 3D frame rate statistics script
猜你喜欢

Channel attention and spatial attention module

Lua Foundation

如何建立一个仪式感点满的网站,并发布到公网 1-2

UE4: what is the gameplay framework

初识图形学

【无需公网IP】为远程桌面树莓派配置固定的公网TCP端口地址

公网使用Microsoft Remote Desktop远程桌面,随时远程办公

Hololens 2 development 101: create the first hololens 2 Application

Using keras to realize LSTM time series prediction based on attention mechanism

IP作业(2)RIP
随机推荐
不租服务器,自建个人商业网站(2)
论文阅读-Endmember-Guided Unmixing Network (EGU-Net) 端元指导型高光谱解混网络
IP作业(2)RIP
Dameng database_ Trigger, view, materialized view, sequence, synonym, auto increment, external link and other basic operations
IP课笔记(5)
Simple but easy to use: using keras 2 to realize multi-dimensional time series prediction based on LSTM
unity最新版本的Text(TMP)UI文本怎么显示中文
HoloLens 2 开发:开发环境部署
Kernel pwn 基础教程之 Heap Overflow
How does the latest version of text (TMP) UI text of unity display in Chinese
Foundation of JUC concurrent programming (4) -- thread group and thread priority
ue4 换装系统3.最终成果
The kernel apps to have died. it will restart automatically
Channel attention and spatial attention module
day5-jvm
Dameng database_ Common commands
Detailed explanation of KMP code distribution
Synergy LAN realizes multi host shared keyboard and mouse (AMD, arm)
MySQL foundation - constraints
HoloLens2开发:使用MRTK并且模拟眼动追踪