com.nextbreakpoint.flink.dummies.SampledTemperatureIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.nextbreakpoint.flink.dummies Show documentation
Show all versions of com.nextbreakpoint.flink.dummies Show documentation
Collection of dummy jobs for Apache Flink
/*
* This file is part of Flink Dummies
* https://github.com/nextbreakpoint/flink-dummies
*/
package com.nextbreakpoint.flink.dummies;
import lombok.Getter;
import org.apache.flink.util.SplittableIterator;
import java.time.Clock;
import java.time.Instant;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
public class SampledTemperatureIterator extends SplittableIterator {
private final AtomicInteger count = new AtomicInteger(0);
@Getter
private final int maxCount;
@Getter
private final long maxDelay;
@Getter
private final List sensors;
public SampledTemperatureIterator(int maxCount, long maxDelay, List sensors) {
this.maxCount = maxCount;
this.maxDelay = maxDelay;
this.sensors = Objects.requireNonNull(sensors);
}
@Override
public Iterator[] split(int numPartitions) {
return new Iterator[] { this };
}
@Override
public int getMaximumNumberOfSplits() {
return 1;
}
@Override
public boolean hasNext() {
return maxCount <= 0 || count.get() < maxCount;
}
@Override
public SampledTemperature next() {
if (maxDelay > 0) {
try {
// simulate a variable latency
Thread.sleep((long) Math.rint(Math.random() * maxDelay));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
return createElement(count.addAndGet(1), Instant.now(Clock.systemUTC()));
}
private SampledTemperature createElement(int index, Instant timestamp) {
final TemperatureSensor sensor = sensors.get(index % sensors.size());
final int temperature = Utilities.sampleTemperature(sensor, timestamp);
return SampledTemperature.builder()
.withMeasureId(UUID.randomUUID().toString())
.withSensorId(sensor.getSensorId())
.withLocationId(sensor.getLocationId())
.withValue(temperature)
.withTimestamp(timestamp)
.build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy