Flink定期更新源

时间:2017-10-25 14:54:14

标签: scala apache-flink flink-streaming

我正在尝试为长期运行的flink作业实现外部配置。我的想法是创建自定义源,定期(每5分钟)通过http从外部服务轮询JSON编码的配置。

如何创建每N分钟执行一次操作的源? 如何将此配置重新广播给所有执行者?

1 个答案:

答案 0 :(得分:1)

首先,您需要创建一个事件类,它将定义事件流所具有的所有属性,然后生成所有getter,setter和其他方法。这个类的一个例子是

 public class qrsIntervalStreamEvent {

    public Integer Sensor_id;
    public long time;
    public Integer qrsInterval;


    public qrsIntervalStreamEvent(Integer sensor_id, long time, Integer qrsInterval) {
        Sensor_id = sensor_id;
        this.time = time;
        this.qrsInterval = qrsInterval;
    }


    public Integer getSensor_id() {
        return Sensor_id;
    }

    public void setSensor_id(Integer sensor_id) {
        Sensor_id = sensor_id;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public Integer getQrsInterval() {
        return qrsInterval;
    }

    public void setQrsInterval(Integer qrsInterval) {
        this.qrsInterval = qrsInterval;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof qrsIntervalStreamEvent)) return false;

        qrsIntervalStreamEvent that = (qrsIntervalStreamEvent) o;

        if (getTime() != that.getTime()) return false;
        if (getSensor_id() != null ? !getSensor_id().equals(that.getSensor_id()) : that.getSensor_id() != null)
            return false;
        return getQrsInterval() != null ? getQrsInterval().equals(that.getQrsInterval()) : that.getQrsInterval() == null;
    }

    @Override
    public int hashCode() {
        int result = getSensor_id() != null ? getSensor_id().hashCode() : 0;
        result = 31 * result + (int) (getTime() ^ (getTime() >>> 32));
        result = 31 * result + (getQrsInterval() != null ? getQrsInterval().hashCode() : 0);
        return result;
    }


    @Override
    public String toString() {
        return "StreamEvent{" +
                "Sensor_id=" + Sensor_id +
                ", time=" + time +
                ", qrsInterval=" + qrsInterval +
                '}';
    }


} //class

现在假设您要在x events / 5秒发送这些事件,然后您可以编写类似这样的代码

public class Qrs_interval_Gen extends RichParallelSourceFunction<qrsIntervalStreamEvent> {
@Override
public void run(SourceContext<qrsIntervalStreamEvent> sourceContext) throws Exception {


    int qrsInterval;
    int Sensor_id;
    long currentTime;
    Random random = new Random();

    Integer InputRate = 10;

    Integer Sleeptime = 1000 * 5 / InputRate  ;


    for(int i = 0 ; i <= 100000 ; i++){


        // int randomNum = rand.nextInt((max - min) + 1) + min;
        Sensor_id =  1;

        qrsInterval =  10 + random.nextInt((20-10)+ 1);
       // currentTime = System.currentTimeMillis();
        currentTime = i;

        //System.out.println("qrsInterval = " + qrsInterval + ", Sensor_id = "+ Sensor_id );
        try {
            Thread.sleep(Sleeptime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        qrsIntervalStreamEvent stream = new qrsIntervalStreamEvent(Sensor_id,currentTime,qrsInterval);

            sourceContext.collect(stream);

    } // for loop


}

    @Override
    public void cancel() {

    }
}

这里整个逻辑由

完成

如果您想发送x个事件/秒,那么您的睡眠时间将与此相反。例如,发送10个事件/秒

  

睡眠时间= 1000/10 = 100毫秒

同样,对于发送10个事件/ 5秒,睡眠时间将是

  

睡眠时间= 1000 * 5/10 = 500毫秒

希望它有所帮助,如果您有任何问题,请告诉我