并行度为1 的source 主要用于学习和试验使用

并行度为1 的source 主要用于学习和试验使用

代码

package com.it.flink.second;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSink;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Arrays;

/*并行度为1 的source 主要用于学习和试验使用*/
public class SourceDemo {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //将客户端的集合并行化成一个抽象的数据集,通常是用来做测试和实验
        //fromElements是一个有界的数据量,虽然是一个实时计算程序,但是数据处理完,程序就会推出
        DataStreamSource<Integer> ints = env.fromCollection(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));

        //获取DataStreamSource 的并行度
        int p1 = ints.getParallelism();
        System.out.println("并行度1----> "+p1);

        SingleOutputStreamOperator<Integer> filter = ints.filter(new FilterFunction<Integer>() {
            @Override
            public boolean filter(Integer integer) throws Exception {
                return integer % 2 == 0;
            }
        });

        filter.setParallelism(3);
         int p2 = filter.getParallelism();
        System.out.println("并行度2----> "+p2);

        //sink
        filter.print();

        //启动程序
        env.execute("this is a test");
    }

}


运行结果

并行度1----> 1
并行度2---->

  • CPU的core +1 编码
    8> 4
    7> 6
    6> 2
    7> 8
发布了33 篇原创文章 · 获赞 12 · 访问量 3355

猜你喜欢

转载自blog.csdn.net/IT_BULL/article/details/104111714
今日推荐