Interface RichFunction
-
- All Superinterfaces:
org.apache.flink.api.common.functions.Function,Serializable
- All Known Implementing Classes:
AbstractRichFunction,BulkIterationBase.TerminationCriterionMapper,NoOpFunction,RichAggregateFunction,RichCoGroupFunction,RichCrossFunction,RichFilterFunction,RichFlatJoinFunction,RichFlatMapFunction,RichGroupCombineFunction,RichGroupReduceFunction,RichJoinFunction,RichMapFunction,RichMapPartitionFunction,RichReduceFunction
@Public public interface RichFunction extends org.apache.flink.api.common.functions.FunctionAn base interface for all rich user-defined functions. This class defines methods for the life cycle of the functions, as well as methods to access the context in which the functions are executed.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description voidclose()Tear-down method for the user code.IterationRuntimeContextgetIterationRuntimeContext()Gets a specialized version of theRuntimeContext, which has additional information about the iteration in which the function is executed.RuntimeContextgetRuntimeContext()Gets the context that contains information about the UDF's runtime, such as the parallelism of the function, the subtask index of the function, or the name of the task that executes the function.default voidopen(OpenContext openContext)Initialization method for the function.voidopen(Configuration parameters)Deprecated.This method is deprecated since Flink 1.19.voidsetRuntimeContext(RuntimeContext t)Sets the function's runtime context.
-
-
-
Method Detail
-
open
@Deprecated void open(Configuration parameters) throws Exception
Deprecated.This method is deprecated since Flink 1.19. The users are recommended to implementopen(OpenContext openContext)and implementopen(Configuration parameters)with an empty body instead. 1. If you implementopen(OpenContext openContext), theopen(OpenContext openContext)will be invoked and theopen(Configuration parameters)won't be invoked. 2. If you don't implementopen(OpenContext openContext), theopen(Configuration parameters)will be invoked in the default implementation of theopen(OpenContext openContext).Initialization method for the function. It is called before the actual working methods (like map or join) and thus suitable for one time setup work. For functions that are part of an iteration, this method will be invoked at the beginning of each iteration superstep.The configuration object passed to the function can be used for configuration and initialization. The configuration contains all parameters that were configured on the function in the program composition.
public class MyFilter extends RichFilterFunction<String> { private String searchString; public void open(Configuration parameters) { this.searchString = parameters.getString("foo"); } public boolean filter(String value) { return value.equals(searchString); } }By default, this method does nothing.
- Parameters:
parameters- The configuration containing the parameters attached to the contract.- Throws:
Exception- Implementations may forward exceptions, which are caught by the runtime. When the runtime catches an exception, it aborts the task and lets the fail-over logic decide whether to retry the task execution.- See Also:
Configuration, FLIP-344: Remove parameter in RichFunction#open
-
open
@PublicEvolving default void open(OpenContext openContext) throws Exception
Initialization method for the function. It is called before the actual working methods (like map or join) and thus suitable for one time setup work. For functions that are part of an iteration, this method will be invoked at the beginning of each iteration superstep.The openContext object passed to the function can be used for configuration and initialization. The openContext contains some necessary information that were configured on the function in the program composition.
public class MyFilter extends RichFilterFunction<String> { private String searchString; public void open(OpenContext openContext) { // initialize the value of searchString } public boolean filter(String value) { return value.equals(searchString); } }By default, this method does nothing.
1. If you implement
open(OpenContext openContext), theopen(OpenContext openContext)will be invoked and theopen(Configuration parameters)won't be invoked. 2. If you don't implementopen(OpenContext openContext), theopen(Configuration parameters)will be invoked in the default implementation of theopen(OpenContext openContext).- Parameters:
openContext- The context containing information about the context in which the function is opened.- Throws:
Exception- Implementations may forward exceptions, which are caught by the runtime. When the runtime catches an exception, it aborts the task and lets the fail-over logic decide whether to retry the task execution.
-
close
void close() throws ExceptionTear-down method for the user code. It is called after the last call to the main working methods (e.g. map or join). For functions that are part of an iteration, this method will be invoked after each iteration superstep.This method can be used for clean up work.
- Throws:
Exception- Implementations may forward exceptions, which are caught by the runtime. When the runtime catches an exception, it aborts the task and lets the fail-over logic decide whether to retry the task execution.
-
getRuntimeContext
RuntimeContext getRuntimeContext()
Gets the context that contains information about the UDF's runtime, such as the parallelism of the function, the subtask index of the function, or the name of the task that executes the function.The RuntimeContext also gives access to the
Accumulators and theDistributedCache.- Returns:
- The UDF's runtime context.
-
getIterationRuntimeContext
IterationRuntimeContext getIterationRuntimeContext()
Gets a specialized version of theRuntimeContext, which has additional information about the iteration in which the function is executed. This IterationRuntimeContext is only available if the function is part of an iteration. Otherwise, this method throws an exception.- Returns:
- The IterationRuntimeContext.
- Throws:
IllegalStateException- Thrown, if the function is not executed as part of an iteration.
-
setRuntimeContext
void setRuntimeContext(RuntimeContext t)
Sets the function's runtime context. Called by the framework when creating a parallel instance of the function.- Parameters:
t- The runtime context.
-
-