自己定义一个多并行度的source,需要自己实现一个ParallelSourceFunction接口
import org.apache.flink.streaming.api.functions.source.ParallelSourceFunction;
/**
* interface ParallelSourceFunction<OUT> extends SourceFunction<OUT>
*/
public class MyParalleSource implements ParallelSourceFunction <Long>{
private Long count=1l;
private boolean isFlag=true;
/**
* 主要的方法:
* 启动一个source源
* 大部分情况下,都需要在这个run方法中实现一个循环,这样可以循环产生数据了
*
* @param sct
* @throws Exception
*/
public void run(SourceContext<Long> sct) throws Exception {
while(isFlag){
sct.collect(count);
count++;
//sleep() 单位是毫秒级的
Thread.sleep(1000);
}
}
public void cancel() {
isFlag=false;
}
}
具体操作实现类:
import javafx.scene.chart.PieChart;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
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 org.apache.flink.streaming.api.windowing.time.Time;
public class StreamingDemoWithParalleSource {
public static void main(String[] args) throws Exception{
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStreamSource<Long> text = env.addSource(new MyParalleSource()).setParallelism(2);
/**
* 在没有设置数据源的并行度的时候,默认的并行度是和你的电脑的 cpu的cores多少是相同的
* 我的电脑是八核的
* 结果为:
*接收到的数据为1
* 接收到的数据为1
* 接收到的数据为1
* 接收到的数据为1
* 接收到的数据为1
* 接收到的数据为1
* 接收到的数据为1
* 接收到的数据为1
* 接收到的数据为2
* 接收到的数据为2
* 接收到的数据为2
* 接收到的数据为2
* 接收到的数据为2
* 接收到的数据为2
* 接收到的数据为2
* 接收到的数据为2
* 24
*/
/**
* 在设置数据源的并行度为2的时候
* 结果为:
* 接收到的数据为6
* 接收到的数据为6
* 接收到的数据为7
* 接收到的数据为7
* 26
* 接收到的数据为8
* 接收到的数据为8
* 接收到的数据为9
* 接收到的数据为9
* 34
*/
DataStream<Long> num = text.map(new MapFunction<Long, Long>() {
public Long map(Long value) throws Exception {
System.out.println("接收到的数据为" + value);
return value;
}
});
// shuffle 进行重新分区
// num.shuffle();
// num.rebalance();
DataStream<Long> sum = num.timeWindowAll(Time.seconds(2)).sum(0);
sum.print().setParallelism(1);
env.execute("parallelSource");
}
}
scala代码实现案例
具体实现类
package streaming
import org.apache.flink.streaming.api.functions.source.{ParallelSourceFunction, SourceFunction}
class MyParallelScala extends ParallelSourceFunction[Long]{
var count=1l
var isFlag=true
override def run(ctx: SourceFunction.SourceContext[Long]) = {
while(isFlag){
ctx.collect(count)
count+=1
Thread.sleep(1000)
}
}
override def cancel()= {
isFlag=false
}
}
具体的操作类
package streaming
import org.apache.flink.streaming.api.scala.{StreamExecutionEnvironment, _}
import org.apache.flink.streaming.api.windowing.time.Time
object StrreamingDemoWithMyParallelSourceScala {
def main(args: Array[String]): Unit = {
val env = StreamExecutionEnvironment.getExecutionEnvironment
//这里设置多并行度
val text = env.addSource(new MyParallelScala).setParallelism(2)
val mapdata=text.map(data=>{
println("接收到的数据"+data)
data
})
val num = mapdata.timeWindowAll(Time.seconds(2)).sum(0)
num.print().setParallelism(1)
env.execute("StrreamingDemoWithMyParallelSourceScala")
}
}