並行度爲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