Search in sources :

Example 1 with InfiniteIntegerSource

use of org.apache.flink.test.util.InfiniteIntegerSource in project flink by apache.

the class CustomKvStateProgram method main.

public static void main(String[] args) throws Exception {
    final int parallelism = Integer.parseInt(args[0]);
    final String checkpointPath = args[1];
    final int checkpointingInterval = Integer.parseInt(args[2]);
    final String outputPath = args[3];
    final Optional<Boolean> unalignedCheckpoints = args.length > 4 ? Optional.of(Boolean.parseBoolean(args[4])) : Optional.empty();
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.setParallelism(parallelism);
    env.enableCheckpointing(checkpointingInterval);
    unalignedCheckpoints.ifPresent(value -> env.getCheckpointConfig().enableUnalignedCheckpoints(value));
    env.setStateBackend(new FsStateBackend(checkpointPath));
    DataStream<Integer> source = env.addSource(new InfiniteIntegerSource());
    source.map(new MapFunction<Integer, Tuple2<Integer, Integer>>() {

        private static final long serialVersionUID = 1L;

        @Override
        public Tuple2<Integer, Integer> map(Integer value) throws Exception {
            return new Tuple2<>(ThreadLocalRandom.current().nextInt(parallelism), value);
        }
    }).keyBy(new KeySelector<Tuple2<Integer, Integer>, Integer>() {

        private static final long serialVersionUID = 1L;

        @Override
        public Integer getKey(Tuple2<Integer, Integer> value) throws Exception {
            return value.f0;
        }
    }).flatMap(new ReducingStateFlatMap()).writeAsText(outputPath);
    env.execute();
}
Also used : InfiniteIntegerSource(org.apache.flink.test.util.InfiniteIntegerSource) RichFlatMapFunction(org.apache.flink.api.common.functions.RichFlatMapFunction) MapFunction(org.apache.flink.api.common.functions.MapFunction) Tuple2(org.apache.flink.api.java.tuple.Tuple2) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) FsStateBackend(org.apache.flink.runtime.state.filesystem.FsStateBackend)

Aggregations

MapFunction (org.apache.flink.api.common.functions.MapFunction)1 RichFlatMapFunction (org.apache.flink.api.common.functions.RichFlatMapFunction)1 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)1 FsStateBackend (org.apache.flink.runtime.state.filesystem.FsStateBackend)1 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)1 InfiniteIntegerSource (org.apache.flink.test.util.InfiniteIntegerSource)1