Class AbstractRuntimeUDFContext
- java.lang.Object
-
- org.apache.flink.api.common.functions.util.AbstractRuntimeUDFContext
-
- All Implemented Interfaces:
RuntimeContext
- Direct Known Subclasses:
RuntimeUDFContext
@Internal public abstract class AbstractRuntimeUDFContext extends Object implements RuntimeContext
A standalone implementation of theRuntimeContext, created by runtime UDF operators.
-
-
Constructor Summary
Constructors Constructor Description AbstractRuntimeUDFContext(TaskInfo taskInfo, UserCodeClassLoader userCodeClassLoader, ExecutionConfig executionConfig, Map<String,Accumulator<?,?>> accumulators, Map<String,Future<Path>> cpTasks, org.apache.flink.metrics.groups.OperatorMetricGroup metrics)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <V,A extends Serializable>
voidaddAccumulator(String name, Accumulator<V,A> accumulator)Add this accumulator.<V,A extends Serializable>
Accumulator<V,A>getAccumulator(String name)Get an existing accumulator object.<IN,ACC,OUT>
AggregatingState<IN,OUT>getAggregatingState(AggregatingStateDescriptor<IN,ACC,OUT> stateProperties)Gets a handle to the system's key/value aggregating state.StringgetAllocationIDAsString()intgetAttemptNumber()Gets the attempt number of this parallel subtask.DistributedCachegetDistributedCache()Returns theDistributedCacheto get the local temporary file copies of files otherwise not locally accessible.DoubleCountergetDoubleCounter(String name)Convenience function to create a counter object for doubles.ExecutionConfiggetExecutionConfig()Returns theExecutionConfigfor the currently executing job.HistogramgetHistogram(String name)Convenience function to create a counter object for histograms.intgetIndexOfThisSubtask()Gets the number of this parallel subtask.IntCountergetIntCounter(String name)Convenience function to create a counter object for integers.<T> ListState<T>getListState(ListStateDescriptor<T> stateProperties)Gets a handle to the system's key/value list state.LongCountergetLongCounter(String name)Convenience function to create a counter object for longs.<UK,UV>
MapState<UK,UV>getMapState(MapStateDescriptor<UK,UV> stateProperties)Gets a handle to the system's key/value map state.intgetMaxNumberOfParallelSubtasks()Gets the number of max-parallelism with which the parallel task runs.org.apache.flink.metrics.groups.OperatorMetricGroupgetMetricGroup()Returns the metric group for this parallel subtask.intgetNumberOfParallelSubtasks()Gets the parallelism with which the parallel task runs.<T> ReducingState<T>getReducingState(ReducingStateDescriptor<T> stateProperties)Gets a handle to the system's key/value reducing state.<T> ValueState<T>getState(ValueStateDescriptor<T> stateProperties)Gets a handle to the system's key/value state.StringgetTaskName()Returns the name of the task in which the UDF runs, as assigned during plan construction.StringgetTaskNameWithSubtasks()Returns the name of the task, appended with the subtask indicator, such as "MyTask (3/6)#1", where 3 would be (RuntimeContext.getIndexOfThisSubtask()+ 1), and 6 would beRuntimeContext.getNumberOfParallelSubtasks(), and 1 would beRuntimeContext.getAttemptNumber().ClassLoadergetUserCodeClassLoader()Gets the ClassLoader to load classes that are not in system's classpath, but are part of the jar file of a user job.voidregisterUserCodeClassLoaderReleaseHookIfAbsent(String releaseHookName, Runnable releaseHook)Registers a custom hook for the user code class loader release.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.flink.api.common.functions.RuntimeContext
getBroadcastVariable, getBroadcastVariableWithInitializer, getExternalResourceInfos, getJobId, hasBroadcastVariable
-
-
-
-
Constructor Detail
-
AbstractRuntimeUDFContext
public AbstractRuntimeUDFContext(TaskInfo taskInfo, UserCodeClassLoader userCodeClassLoader, ExecutionConfig executionConfig, Map<String,Accumulator<?,?>> accumulators, Map<String,Future<Path>> cpTasks, org.apache.flink.metrics.groups.OperatorMetricGroup metrics)
-
-
Method Detail
-
getExecutionConfig
public ExecutionConfig getExecutionConfig()
Description copied from interface:RuntimeContextReturns theExecutionConfigfor the currently executing job.- Specified by:
getExecutionConfigin interfaceRuntimeContext
-
getTaskName
public String getTaskName()
Description copied from interface:RuntimeContextReturns the name of the task in which the UDF runs, as assigned during plan construction.- Specified by:
getTaskNamein interfaceRuntimeContext- Returns:
- The name of the task in which the UDF runs.
-
getNumberOfParallelSubtasks
public int getNumberOfParallelSubtasks()
Description copied from interface:RuntimeContextGets the parallelism with which the parallel task runs.- Specified by:
getNumberOfParallelSubtasksin interfaceRuntimeContext- Returns:
- The parallelism with which the parallel task runs.
-
getMaxNumberOfParallelSubtasks
public int getMaxNumberOfParallelSubtasks()
Description copied from interface:RuntimeContextGets the number of max-parallelism with which the parallel task runs.- Specified by:
getMaxNumberOfParallelSubtasksin interfaceRuntimeContext- Returns:
- The max-parallelism with which the parallel task runs.
-
getIndexOfThisSubtask
public int getIndexOfThisSubtask()
Description copied from interface:RuntimeContextGets the number of this parallel subtask. The numbering starts from 0 and goes up to parallelism-1 (parallelism as returned byRuntimeContext.getNumberOfParallelSubtasks()).- Specified by:
getIndexOfThisSubtaskin interfaceRuntimeContext- Returns:
- The index of the parallel subtask.
-
getMetricGroup
public org.apache.flink.metrics.groups.OperatorMetricGroup getMetricGroup()
Description copied from interface:RuntimeContextReturns the metric group for this parallel subtask.- Specified by:
getMetricGroupin interfaceRuntimeContext- Returns:
- The metric group for this parallel subtask.
-
getAttemptNumber
public int getAttemptNumber()
Description copied from interface:RuntimeContextGets the attempt number of this parallel subtask. First attempt is numbered 0.- Specified by:
getAttemptNumberin interfaceRuntimeContext- Returns:
- Attempt number of the subtask.
-
getTaskNameWithSubtasks
public String getTaskNameWithSubtasks()
Description copied from interface:RuntimeContextReturns the name of the task, appended with the subtask indicator, such as "MyTask (3/6)#1", where 3 would be (RuntimeContext.getIndexOfThisSubtask()+ 1), and 6 would beRuntimeContext.getNumberOfParallelSubtasks(), and 1 would beRuntimeContext.getAttemptNumber().- Specified by:
getTaskNameWithSubtasksin interfaceRuntimeContext- Returns:
- The name of the task, with subtask indicator.
-
getIntCounter
public IntCounter getIntCounter(String name)
Description copied from interface:RuntimeContextConvenience function to create a counter object for integers.- Specified by:
getIntCounterin interfaceRuntimeContext
-
getLongCounter
public LongCounter getLongCounter(String name)
Description copied from interface:RuntimeContextConvenience function to create a counter object for longs.- Specified by:
getLongCounterin interfaceRuntimeContext
-
getHistogram
public Histogram getHistogram(String name)
Description copied from interface:RuntimeContextConvenience function to create a counter object for histograms.- Specified by:
getHistogramin interfaceRuntimeContext
-
getDoubleCounter
public DoubleCounter getDoubleCounter(String name)
Description copied from interface:RuntimeContextConvenience function to create a counter object for doubles.- Specified by:
getDoubleCounterin interfaceRuntimeContext
-
addAccumulator
public <V,A extends Serializable> void addAccumulator(String name, Accumulator<V,A> accumulator)
Description copied from interface:RuntimeContextAdd this accumulator. Throws an exception if the accumulator already exists in the same Task. Note that the Accumulator name must have an unique name across the Flink job. Otherwise you will get an error when incompatible accumulators from different Tasks are combined at the JobManager upon job completion.- Specified by:
addAccumulatorin interfaceRuntimeContext
-
getAccumulator
public <V,A extends Serializable> Accumulator<V,A> getAccumulator(String name)
Description copied from interface:RuntimeContextGet an existing accumulator object. The accumulator must have been added previously in this local runtime context.Throws an exception if the accumulator does not exist or if the accumulator exists, but with different type.
- Specified by:
getAccumulatorin interfaceRuntimeContext
-
getUserCodeClassLoader
public ClassLoader getUserCodeClassLoader()
Description copied from interface:RuntimeContextGets the ClassLoader to load classes that are not in system's classpath, but are part of the jar file of a user job.- Specified by:
getUserCodeClassLoaderin interfaceRuntimeContext- Returns:
- The ClassLoader for user code classes.
-
registerUserCodeClassLoaderReleaseHookIfAbsent
public void registerUserCodeClassLoaderReleaseHookIfAbsent(String releaseHookName, Runnable releaseHook)
Description copied from interface:RuntimeContextRegisters a custom hook for the user code class loader release.The release hook is executed just before the user code class loader is being released. Registration only happens if no hook has been registered under this name already.
- Specified by:
registerUserCodeClassLoaderReleaseHookIfAbsentin interfaceRuntimeContext- Parameters:
releaseHookName- name of the release hook.releaseHook- release hook which is executed just before the user code class loader is being released
-
getDistributedCache
public DistributedCache getDistributedCache()
Description copied from interface:RuntimeContextReturns theDistributedCacheto get the local temporary file copies of files otherwise not locally accessible.- Specified by:
getDistributedCachein interfaceRuntimeContext- Returns:
- The distributed cache of the worker executing this instance.
-
getState
@PublicEvolving public <T> ValueState<T> getState(ValueStateDescriptor<T> stateProperties)
Description copied from interface:RuntimeContextGets a handle to the system's key/value state. The key/value state is only accessible if the function is executed on a KeyedStream. On each access, the state exposes the value for the key of the element currently processed by the function. Each function may have multiple partitioned states, addressed with different names.Because the scope of each value is the key of the currently processed element, and the elements are distributed by the Flink runtime, the system can transparently scale out and redistribute the state and KeyedStream.
The following code example shows how to implement a continuous counter that counts how many times elements of a certain key occur, and emits an updated count for that element on each occurrence.
DataStream<MyType> stream = ...; KeyedStream<MyType> keyedStream = stream.keyBy("id"); keyedStream.map(new RichMapFunction<MyType, Tuple2<MyType, Long>>() { private ValueState<Long> state; public void open(Configuration cfg) { state = getRuntimeContext().getState( new ValueStateDescriptor<Long>("count", LongSerializer.INSTANCE, 0L)); } public Tuple2<MyType, Long> map(MyType value) { long count = state.value() + 1; state.update(count); return new Tuple2<>(value, count); } });- Specified by:
getStatein interfaceRuntimeContext- Type Parameters:
T- The type of value stored in the state.- Parameters:
stateProperties- The descriptor defining the properties of the stats.- Returns:
- The partitioned state object.
-
getListState
@PublicEvolving public <T> ListState<T> getListState(ListStateDescriptor<T> stateProperties)
Description copied from interface:RuntimeContextGets a handle to the system's key/value list state. This state is similar to the state accessed viaRuntimeContext.getState(ValueStateDescriptor), but is optimized for state that holds lists. One can add elements to the list, or retrieve the list as a whole.This state is only accessible if the function is executed on a KeyedStream.
DataStream<MyType> stream = ...; KeyedStream<MyType> keyedStream = stream.keyBy("id"); keyedStream.map(new RichFlatMapFunction<MyType, List<MyType>>() { private ListState<MyType> state; public void open(Configuration cfg) { state = getRuntimeContext().getListState( new ListStateDescriptor<>("myState", MyType.class)); } public void flatMap(MyType value, Collector<MyType> out) { if (value.isDivider()) { for (MyType t : state.get()) { out.collect(t); } } else { state.add(value); } } });- Specified by:
getListStatein interfaceRuntimeContext- Type Parameters:
T- The type of value stored in the state.- Parameters:
stateProperties- The descriptor defining the properties of the stats.- Returns:
- The partitioned state object.
-
getReducingState
@PublicEvolving public <T> ReducingState<T> getReducingState(ReducingStateDescriptor<T> stateProperties)
Description copied from interface:RuntimeContextGets a handle to the system's key/value reducing state. This state is similar to the state accessed viaRuntimeContext.getState(ValueStateDescriptor), but is optimized for state that aggregates values.This state is only accessible if the function is executed on a KeyedStream.
DataStream<MyType> stream = ...; KeyedStream<MyType> keyedStream = stream.keyBy("id"); keyedStream.map(new RichMapFunction<MyType, List<MyType>>() { private ReducingState<Long> state; public void open(Configuration cfg) { state = getRuntimeContext().getReducingState( new ReducingStateDescriptor<>("sum", (a, b) -> a + b, Long.class)); } public Tuple2<MyType, Long> map(MyType value) { state.add(value.count()); return new Tuple2<>(value, state.get()); } });- Specified by:
getReducingStatein interfaceRuntimeContext- Type Parameters:
T- The type of value stored in the state.- Parameters:
stateProperties- The descriptor defining the properties of the stats.- Returns:
- The partitioned state object.
-
getAggregatingState
@PublicEvolving public <IN,ACC,OUT> AggregatingState<IN,OUT> getAggregatingState(AggregatingStateDescriptor<IN,ACC,OUT> stateProperties)
Description copied from interface:RuntimeContextGets a handle to the system's key/value aggregating state. This state is similar to the state accessed viaRuntimeContext.getState(ValueStateDescriptor), but is optimized for state that aggregates values with different types.This state is only accessible if the function is executed on a KeyedStream.
DataStream<MyType> stream = ...; KeyedStream<MyType> keyedStream = stream.keyBy("id"); AggregateFunction<...> aggregateFunction = ... keyedStream.map(new RichMapFunction<MyType, List<MyType>>() { private AggregatingState<MyType, Long> state; public void open(Configuration cfg) { state = getRuntimeContext().getAggregatingState( new AggregatingStateDescriptor<>("sum", aggregateFunction, Long.class)); } public Tuple2<MyType, Long> map(MyType value) { state.add(value); return new Tuple2<>(value, state.get()); } });- Specified by:
getAggregatingStatein interfaceRuntimeContext- Type Parameters:
IN- The type of the values that are added to the state.ACC- The type of the accumulator (intermediate aggregation state).OUT- The type of the values that are returned from the state.- Parameters:
stateProperties- The descriptor defining the properties of the stats.- Returns:
- The partitioned state object.
-
getMapState
@PublicEvolving public <UK,UV> MapState<UK,UV> getMapState(MapStateDescriptor<UK,UV> stateProperties)
Description copied from interface:RuntimeContextGets a handle to the system's key/value map state. This state is similar to the state accessed viaRuntimeContext.getState(ValueStateDescriptor), but is optimized for state that is composed of user-defined key-value pairsThis state is only accessible if the function is executed on a KeyedStream.
DataStream<MyType> stream = ...; KeyedStream<MyType> keyedStream = stream.keyBy("id"); keyedStream.map(new RichMapFunction<MyType, List<MyType>>() { private MapState<MyType, Long> state; public void open(Configuration cfg) { state = getRuntimeContext().getMapState( new MapStateDescriptor<>("sum", MyType.class, Long.class)); } public Tuple2<MyType, Long> map(MyType value) { return new Tuple2<>(value, state.get(value)); } });- Specified by:
getMapStatein interfaceRuntimeContext- Type Parameters:
UK- The type of the user keys stored in the state.UV- The type of the user values stored in the state.- Parameters:
stateProperties- The descriptor defining the properties of the stats.- Returns:
- The partitioned state object.
-
getAllocationIDAsString
@Internal @VisibleForTesting public String getAllocationIDAsString()
-
-