Search in sources :

Example 11 with RuntimeUDFContext

use of org.apache.flink.api.common.functions.util.RuntimeUDFContext in project flink by apache.

the class RichInputFormatTest method testCheckRuntimeContextAccess.

@Test
public void testCheckRuntimeContextAccess() {
    final SerializedInputFormat<Value> inputFormat = new SerializedInputFormat<Value>();
    final TaskInfo taskInfo = new TaskInfo("test name", 3, 1, 3, 0);
    inputFormat.setRuntimeContext(new RuntimeUDFContext(taskInfo, getClass().getClassLoader(), new ExecutionConfig(), new HashMap<String, Future<Path>>(), new HashMap<String, Accumulator<?, ?>>(), new UnregisteredMetricsGroup()));
    assertEquals(inputFormat.getRuntimeContext().getIndexOfThisSubtask(), 1);
    assertEquals(inputFormat.getRuntimeContext().getNumberOfParallelSubtasks(), 3);
}
Also used : TaskInfo(org.apache.flink.api.common.TaskInfo) Path(org.apache.flink.core.fs.Path) UnregisteredMetricsGroup(org.apache.flink.metrics.groups.UnregisteredMetricsGroup) HashMap(java.util.HashMap) Value(org.apache.flink.types.Value) RuntimeUDFContext(org.apache.flink.api.common.functions.util.RuntimeUDFContext) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) Test(org.junit.Test)

Example 12 with RuntimeUDFContext

use of org.apache.flink.api.common.functions.util.RuntimeUDFContext in project flink by apache.

the class RichOutputFormatTest method testCheckRuntimeContextAccess.

@Test
public void testCheckRuntimeContextAccess() {
    final SerializedOutputFormat<Value> inputFormat = new SerializedOutputFormat<Value>();
    final TaskInfo taskInfo = new TaskInfo("test name", 3, 1, 3, 0);
    inputFormat.setRuntimeContext(new RuntimeUDFContext(taskInfo, getClass().getClassLoader(), new ExecutionConfig(), new HashMap<String, Future<Path>>(), new HashMap<String, Accumulator<?, ?>>(), new UnregisteredMetricsGroup()));
    assertEquals(inputFormat.getRuntimeContext().getIndexOfThisSubtask(), 1);
    assertEquals(inputFormat.getRuntimeContext().getNumberOfParallelSubtasks(), 3);
}
Also used : TaskInfo(org.apache.flink.api.common.TaskInfo) Path(org.apache.flink.core.fs.Path) UnregisteredMetricsGroup(org.apache.flink.metrics.groups.UnregisteredMetricsGroup) HashMap(java.util.HashMap) Value(org.apache.flink.types.Value) RuntimeUDFContext(org.apache.flink.api.common.functions.util.RuntimeUDFContext) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) Test(org.junit.Test)

Example 13 with RuntimeUDFContext

use of org.apache.flink.api.common.functions.util.RuntimeUDFContext in project flink by apache.

the class FlatMapOperatorCollectionTest method testExecuteOnCollection.

private void testExecuteOnCollection(FlatMapFunction<String, String> udf, List<String> input, boolean mutableSafe) throws Exception {
    ExecutionConfig executionConfig = new ExecutionConfig();
    if (mutableSafe) {
        executionConfig.disableObjectReuse();
    } else {
        executionConfig.enableObjectReuse();
    }
    final TaskInfo taskInfo = new TaskInfo("Test UDF", 4, 0, 4, 0);
    // run on collections
    final List<String> result = getTestFlatMapOperator(udf).executeOnCollections(input, new RuntimeUDFContext(taskInfo, null, executionConfig, new HashMap<String, Future<Path>>(), new HashMap<String, Accumulator<?, ?>>(), new UnregisteredMetricsGroup()), executionConfig);
    Assert.assertEquals(input.size(), result.size());
    Assert.assertEquals(input, result);
}
Also used : TaskInfo(org.apache.flink.api.common.TaskInfo) Path(org.apache.flink.core.fs.Path) UnregisteredMetricsGroup(org.apache.flink.metrics.groups.UnregisteredMetricsGroup) HashMap(java.util.HashMap) RuntimeUDFContext(org.apache.flink.api.common.functions.util.RuntimeUDFContext) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig)

Example 14 with RuntimeUDFContext

use of org.apache.flink.api.common.functions.util.RuntimeUDFContext in project flink by apache.

the class MapOperatorTest method testMapWithRuntimeContext.

@Test
public void testMapWithRuntimeContext() {
    try {
        final String taskName = "Test Task";
        final AtomicBoolean opened = new AtomicBoolean();
        final AtomicBoolean closed = new AtomicBoolean();
        final MapFunction<String, Integer> parser = new RichMapFunction<String, Integer>() {

            @Override
            public void open(Configuration parameters) throws Exception {
                opened.set(true);
                RuntimeContext ctx = getRuntimeContext();
                assertEquals(0, ctx.getIndexOfThisSubtask());
                assertEquals(1, ctx.getNumberOfParallelSubtasks());
                assertEquals(taskName, ctx.getTaskName());
            }

            @Override
            public Integer map(String value) {
                return Integer.parseInt(value);
            }

            @Override
            public void close() throws Exception {
                closed.set(true);
            }
        };
        MapOperatorBase<String, Integer, MapFunction<String, Integer>> op = new MapOperatorBase<String, Integer, MapFunction<String, Integer>>(parser, new UnaryOperatorInformation<String, Integer>(BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.INT_TYPE_INFO), taskName);
        List<String> input = new ArrayList<String>(asList("1", "2", "3", "4", "5", "6"));
        final HashMap<String, Accumulator<?, ?>> accumulatorMap = new HashMap<String, Accumulator<?, ?>>();
        final HashMap<String, Future<Path>> cpTasks = new HashMap<>();
        final TaskInfo taskInfo = new TaskInfo(taskName, 1, 0, 1, 0);
        ExecutionConfig executionConfig = new ExecutionConfig();
        executionConfig.disableObjectReuse();
        List<Integer> resultMutableSafe = op.executeOnCollections(input, new RuntimeUDFContext(taskInfo, null, executionConfig, cpTasks, accumulatorMap, new UnregisteredMetricsGroup()), executionConfig);
        executionConfig.enableObjectReuse();
        List<Integer> resultRegular = op.executeOnCollections(input, new RuntimeUDFContext(taskInfo, null, executionConfig, cpTasks, accumulatorMap, new UnregisteredMetricsGroup()), executionConfig);
        assertEquals(asList(1, 2, 3, 4, 5, 6), resultMutableSafe);
        assertEquals(asList(1, 2, 3, 4, 5, 6), resultRegular);
        assertTrue(opened.get());
        assertTrue(closed.get());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : Accumulator(org.apache.flink.api.common.accumulators.Accumulator) UnregisteredMetricsGroup(org.apache.flink.metrics.groups.UnregisteredMetricsGroup) Configuration(org.apache.flink.configuration.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) MapFunction(org.apache.flink.api.common.functions.MapFunction) RichMapFunction(org.apache.flink.api.common.functions.RichMapFunction) TaskInfo(org.apache.flink.api.common.TaskInfo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RichMapFunction(org.apache.flink.api.common.functions.RichMapFunction) RuntimeUDFContext(org.apache.flink.api.common.functions.util.RuntimeUDFContext) Future(java.util.concurrent.Future) RuntimeContext(org.apache.flink.api.common.functions.RuntimeContext) Test(org.junit.Test)

Example 15 with RuntimeUDFContext

use of org.apache.flink.api.common.functions.util.RuntimeUDFContext in project flink by apache.

the class CoGroupOperatorCollectionTest method testExecuteOnCollection.

@Test
public void testExecuteOnCollection() {
    try {
        List<Tuple2<String, Integer>> input1 = Arrays.asList(new Tuple2Builder<String, Integer>().add("foo", 1).add("foobar", 1).add("foo", 1).add("bar", 1).add("foo", 1).add("foo", 1).build());
        List<Tuple2<String, Integer>> input2 = Arrays.asList(new Tuple2Builder<String, Integer>().add("foo", 1).add("foo", 1).add("bar", 1).add("foo", 1).add("barfoo", 1).add("foo", 1).build());
        ExecutionConfig executionConfig = new ExecutionConfig();
        final HashMap<String, Accumulator<?, ?>> accumulators = new HashMap<String, Accumulator<?, ?>>();
        final HashMap<String, Future<Path>> cpTasks = new HashMap<>();
        final TaskInfo taskInfo = new TaskInfo("Test UDF", 4, 0, 4, 0);
        final RuntimeContext ctx = new RuntimeUDFContext(taskInfo, null, executionConfig, cpTasks, accumulators, new UnregisteredMetricsGroup());
        {
            SumCoGroup udf1 = new SumCoGroup();
            SumCoGroup udf2 = new SumCoGroup();
            executionConfig.disableObjectReuse();
            List<Tuple2<String, Integer>> resultSafe = getCoGroupOperator(udf1).executeOnCollections(input1, input2, ctx, executionConfig);
            executionConfig.enableObjectReuse();
            List<Tuple2<String, Integer>> resultRegular = getCoGroupOperator(udf2).executeOnCollections(input1, input2, ctx, executionConfig);
            Assert.assertTrue(udf1.isClosed);
            Assert.assertTrue(udf2.isClosed);
            Set<Tuple2<String, Integer>> expected = new HashSet<Tuple2<String, Integer>>(Arrays.asList(new Tuple2Builder<String, Integer>().add("foo", 8).add("bar", 2).add("foobar", 1).add("barfoo", 1).build()));
            Assert.assertEquals(expected, new HashSet<Tuple2<String, Integer>>(resultSafe));
            Assert.assertEquals(expected, new HashSet<Tuple2<String, Integer>>(resultRegular));
        }
        {
            executionConfig.disableObjectReuse();
            List<Tuple2<String, Integer>> resultSafe = getCoGroupOperator(new SumCoGroup()).executeOnCollections(Collections.<Tuple2<String, Integer>>emptyList(), Collections.<Tuple2<String, Integer>>emptyList(), ctx, executionConfig);
            executionConfig.enableObjectReuse();
            List<Tuple2<String, Integer>> resultRegular = getCoGroupOperator(new SumCoGroup()).executeOnCollections(Collections.<Tuple2<String, Integer>>emptyList(), Collections.<Tuple2<String, Integer>>emptyList(), ctx, executionConfig);
            Assert.assertEquals(0, resultSafe.size());
            Assert.assertEquals(0, resultRegular.size());
        }
    } catch (Throwable t) {
        t.printStackTrace();
        Assert.fail(t.getMessage());
    }
}
Also used : Accumulator(org.apache.flink.api.common.accumulators.Accumulator) UnregisteredMetricsGroup(org.apache.flink.metrics.groups.UnregisteredMetricsGroup) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) TaskInfo(org.apache.flink.api.common.TaskInfo) Tuple2(org.apache.flink.api.java.tuple.Tuple2) RuntimeUDFContext(org.apache.flink.api.common.functions.util.RuntimeUDFContext) Future(java.util.concurrent.Future) List(java.util.List) RuntimeContext(org.apache.flink.api.common.functions.RuntimeContext) Tuple2Builder(org.apache.flink.api.java.tuple.builder.Tuple2Builder) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

TaskInfo (org.apache.flink.api.common.TaskInfo)17 RuntimeUDFContext (org.apache.flink.api.common.functions.util.RuntimeUDFContext)17 UnregisteredMetricsGroup (org.apache.flink.metrics.groups.UnregisteredMetricsGroup)17 HashMap (java.util.HashMap)15 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)13 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)9 Path (org.apache.flink.core.fs.Path)7 Future (java.util.concurrent.Future)6 Accumulator (org.apache.flink.api.common.accumulators.Accumulator)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 RuntimeContext (org.apache.flink.api.common.functions.RuntimeContext)5 Configuration (org.apache.flink.configuration.Configuration)5 HashSet (java.util.HashSet)4 List (java.util.List)4 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)4 MetricGroup (org.apache.flink.metrics.MetricGroup)4 Collector (org.apache.flink.util.Collector)4 InvalidProgramException (org.apache.flink.api.common.InvalidProgramException)3 Map (java.util.Map)2