1.模拟数据生成
package com.claroja;
import org.apache.flink.streaming.api.functions.source.RichParallelSourceFunction;
import java.util.Random;
public class SensorSource extends RichParallelSourceFunction<SensorReading> {
Boolean running = true;
@Override
public void run(SourceContext<SensorReading> sourceContext) throws Exception {
Random rand = new Random();
while(running){
Long curTime = System.currentTimeMillis();
for (int i = 0; i < 10; i++){
Double curTemp = rand.nextGaussian();
SensorReading sensorReading = new SensorReading("sensor_"+i,curTime,curTemp);
sourceContext.collect(sensorReading);
}
Thread.sleep(100);
}
}
@Override
public void cancel() {
running = false;
}
}
2.POJO
package com.claroja;
public class SensorReading {
public SensorReading() {
}
public SensorReading(String id, Long timestamp, Double temperature) {
this.id = id;
this.timestamp = timestamp;
this.temperature = temperature;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Long getTimestamp() {
return timestamp;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
public Double getTemperature() {
return temperature;
}
public void setTemperature(Double temperature) {
this.temperature = temperature;
}
public String id;
public Long timestamp;
public Double temperature;
@Override
public String toString() {
return "SensorReading{" +
"id='" + id + '\'' +
", timestamp=" + timestamp +
", temperature=" + temperature +
'}';
}
}