Search in sources :

Example 1 with PrintSinkFunction

use of org.apache.flink.streaming.api.functions.sink.PrintSinkFunction in project flink by apache.

the class StickyAllocationAndLocalRecoveryTestJob method main.

public static void main(String[] args) throws Exception {
    final ParameterTool pt = ParameterTool.fromArgs(args);
    final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.setParallelism(pt.getInt("parallelism", 1));
    env.setMaxParallelism(pt.getInt("maxParallelism", pt.getInt("parallelism", 1)));
    env.enableCheckpointing(pt.getInt("checkpointInterval", 1000));
    env.setRestartStrategy(RestartStrategies.fixedDelayRestart(Integer.MAX_VALUE, pt.getInt("restartDelay", 0)));
    if (pt.getBoolean("externalizedCheckpoints", false)) {
        env.getCheckpointConfig().setExternalizedCheckpointCleanup(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
    }
    String checkpointDir = pt.getRequired("checkpointDir");
    env.getCheckpointConfig().setCheckpointStorage(checkpointDir);
    boolean killJvmOnFail = pt.getBoolean("killJvmOnFail", false);
    String stateBackend = pt.get("stateBackend", "hashmap");
    if ("hashmap".equals(stateBackend)) {
        env.setStateBackend(new HashMapStateBackend());
    } else if ("rocks".equals(stateBackend)) {
        boolean incrementalCheckpoints = pt.getBoolean("incrementalCheckpoints", false);
        env.setStateBackend(new EmbeddedRocksDBStateBackend(incrementalCheckpoints));
    } else {
        throw new IllegalArgumentException("Unknown backend: " + stateBackend);
    }
    // make parameters available in the web interface
    env.getConfig().setGlobalJobParameters(pt);
    // delay to throttle down the production of the source
    long delay = pt.getLong("delay", 0L);
    // the maximum number of attempts, before the job finishes with success
    int maxAttempts = pt.getInt("maxAttempts", 3);
    // size of one artificial value
    int valueSize = pt.getInt("valueSize", 10);
    env.addSource(new RandomLongSource(maxAttempts, delay)).keyBy((KeySelector<Long, Long>) aLong -> aLong).flatMap(new StateCreatingFlatMap(valueSize, killJvmOnFail)).addSink(new PrintSinkFunction<>());
    env.execute("Sticky Allocation And Local Recovery Test");
}
Also used : ParameterTool(org.apache.flink.api.java.utils.ParameterTool) RichFlatMapFunction(org.apache.flink.api.common.functions.RichFlatMapFunction) RuntimeContext(org.apache.flink.api.common.functions.RuntimeContext) PrintSinkFunction(org.apache.flink.streaming.api.functions.sink.PrintSinkFunction) RestartStrategies(org.apache.flink.api.common.restartstrategy.RestartStrategies) FunctionSnapshotContext(org.apache.flink.runtime.state.FunctionSnapshotContext) EmbeddedRocksDBStateBackend(org.apache.flink.contrib.streaming.state.EmbeddedRocksDBStateBackend) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ListState(org.apache.flink.api.common.state.ListState) ParameterTool(org.apache.flink.api.java.utils.ParameterTool) CheckpointListener(org.apache.flink.api.common.state.CheckpointListener) Collector(org.apache.flink.util.Collector) RichParallelSourceFunction(org.apache.flink.streaming.api.functions.source.RichParallelSourceFunction) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) KeySelector(org.apache.flink.api.java.functions.KeySelector) Iterator(java.util.Iterator) CheckpointedFunction(org.apache.flink.streaming.api.checkpoint.CheckpointedFunction) ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) FunctionInitializationContext(org.apache.flink.runtime.state.FunctionInitializationContext) Set(java.util.Set) IOException(java.io.IOException) Preconditions(org.apache.flink.util.Preconditions) Serializable(java.io.Serializable) List(java.util.List) ValueState(org.apache.flink.api.common.state.ValueState) HashMapStateBackend(org.apache.flink.runtime.state.hashmap.HashMapStateBackend) CheckpointConfig(org.apache.flink.streaming.api.environment.CheckpointConfig) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) EmbeddedRocksDBStateBackend(org.apache.flink.contrib.streaming.state.EmbeddedRocksDBStateBackend) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) HashMapStateBackend(org.apache.flink.runtime.state.hashmap.HashMapStateBackend)

Example 2 with PrintSinkFunction

use of org.apache.flink.streaming.api.functions.sink.PrintSinkFunction in project flink by apache.

the class PrintSinkFunctionTest method testPrintSinkStdOut.

@Test
public void testPrintSinkStdOut() throws Exception {
    PrintSinkFunction<String> printSink = new PrintSinkFunction<>();
    printSink.setRuntimeContext(new MockStreamingRuntimeContext(false, 1, 0));
    printSink.open(new Configuration());
    printSink.invoke("hello world!", SinkContextUtil.forTimestamp(0));
    assertEquals("Print to System.out", printSink.toString());
    assertEquals("hello world!" + line, arrayOutputStream.toString());
    printSink.close();
}
Also used : MockStreamingRuntimeContext(org.apache.flink.streaming.util.MockStreamingRuntimeContext) Configuration(org.apache.flink.configuration.Configuration) PrintSinkFunction(org.apache.flink.streaming.api.functions.sink.PrintSinkFunction) Test(org.junit.Test)

Example 3 with PrintSinkFunction

use of org.apache.flink.streaming.api.functions.sink.PrintSinkFunction in project flink by apache.

the class PrintSinkFunctionTest method testPrintSinkWithPrefix.

@Test
public void testPrintSinkWithPrefix() throws Exception {
    PrintSinkFunction<String> printSink = new PrintSinkFunction<>();
    printSink.setRuntimeContext(new MockStreamingRuntimeContext(false, 2, 1));
    printSink.open(new Configuration());
    printSink.invoke("hello world!", SinkContextUtil.forTimestamp(0));
    assertEquals("Print to System.out", printSink.toString());
    assertEquals("2> hello world!" + line, arrayOutputStream.toString());
    printSink.close();
}
Also used : MockStreamingRuntimeContext(org.apache.flink.streaming.util.MockStreamingRuntimeContext) Configuration(org.apache.flink.configuration.Configuration) PrintSinkFunction(org.apache.flink.streaming.api.functions.sink.PrintSinkFunction) Test(org.junit.Test)

Example 4 with PrintSinkFunction

use of org.apache.flink.streaming.api.functions.sink.PrintSinkFunction in project flink by apache.

the class PrintSinkFunctionTest method testPrintSinkStdErr.

@Test
public void testPrintSinkStdErr() throws Exception {
    PrintSinkFunction<String> printSink = new PrintSinkFunction<>(true);
    printSink.setRuntimeContext(new MockStreamingRuntimeContext(false, 1, 0));
    printSink.open(new Configuration());
    printSink.invoke("hello world!", SinkContextUtil.forTimestamp(0));
    assertEquals("Print to System.err", printSink.toString());
    assertEquals("hello world!" + line, arrayErrorStream.toString());
    printSink.close();
}
Also used : MockStreamingRuntimeContext(org.apache.flink.streaming.util.MockStreamingRuntimeContext) Configuration(org.apache.flink.configuration.Configuration) PrintSinkFunction(org.apache.flink.streaming.api.functions.sink.PrintSinkFunction) Test(org.junit.Test)

Example 5 with PrintSinkFunction

use of org.apache.flink.streaming.api.functions.sink.PrintSinkFunction in project flink by apache.

the class DataStreamStateTTLTestProgram method main.

public static void main(String[] args) throws Exception {
    final ParameterTool pt = ParameterTool.fromArgs(args);
    final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    setupEnvironment(env, pt);
    setBackendWithCustomTTLTimeProvider(env);
    TtlTestConfig config = TtlTestConfig.fromArgs(pt);
    StateTtlConfig ttlConfig = StateTtlConfig.newBuilder(config.ttl).cleanupFullSnapshot().build();
    env.addSource(new TtlStateUpdateSource(config.keySpace, config.sleepAfterElements, config.sleepTime)).name("TtlStateUpdateSource").keyBy(TtlStateUpdate::getKey).flatMap(new TtlVerifyUpdateFunction(ttlConfig, config.reportStatAfterUpdatesNum)).name("TtlVerifyUpdateFunction").addSink(new PrintSinkFunction<>()).name("PrintFailedVerifications");
    env.execute("State TTL test job");
}
Also used : ParameterTool(org.apache.flink.api.java.utils.ParameterTool) PrintSinkFunction(org.apache.flink.streaming.api.functions.sink.PrintSinkFunction) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) StateTtlConfig(org.apache.flink.api.common.state.StateTtlConfig)

Aggregations

PrintSinkFunction (org.apache.flink.streaming.api.functions.sink.PrintSinkFunction)9 Configuration (org.apache.flink.configuration.Configuration)5 MockStreamingRuntimeContext (org.apache.flink.streaming.util.MockStreamingRuntimeContext)5 Test (org.junit.Test)5 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)4 ParameterTool (org.apache.flink.api.java.utils.ParameterTool)3 List (java.util.List)2 ValueState (org.apache.flink.api.common.state.ValueState)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1 RandomStringUtils (org.apache.commons.lang3.RandomStringUtils)1 MapFunction (org.apache.flink.api.common.functions.MapFunction)1