flink 自定义多并行度的source源

自己定义一个多并行度的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")
  }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值