use of storm.trident.operation.builtin.Debug in project jstorm by alibaba.
the class TridentMinMaxOfVehiclesTest method testTridentMinMaxOfVehicles.
@Test
public void testTridentMinMaxOfVehicles() {
Fields driverField = new Fields(Driver.FIELD_NAME);
Fields vehicleField = new Fields(Vehicle.FIELD_NAME);
Fields fields = new Fields(Vehicle.FIELD_NAME, Driver.FIELD_NAME);
Random random = new Random(System.currentTimeMillis());
List<Values> vehicleContent = new ArrayList<Values>();
List<Values> driverContent = new ArrayList<Values>();
int maxSpeed = -1, minSpeed = 10000;
double maxEfficiency = -1, minEfficiency = 10000;
for (int i = 0; i < SPOUT_BATCH_SIZE; i++) {
int speed = random.nextInt(10000);
maxSpeed = Math.max(speed, maxSpeed);
minSpeed = Math.min(speed, minSpeed);
double efficiency = random.nextDouble() * 10000;
maxEfficiency = Math.max(efficiency, maxEfficiency);
minEfficiency = Math.min(efficiency, minEfficiency);
vehicleContent.add(new Values(new Vehicle("vehicle-" + (i + 1), speed, efficiency)));
driverContent.add(new Values(new Driver("driver-" + (i + 1), i + 1)));
}
ShuffleValuesBatchSpout spout = new ShuffleValuesBatchSpout(fields, vehicleContent, driverContent);
TridentTopology tridentTopology = new TridentTopology();
Stream vehiclesStream = tridentTopology.newStream("spout", spout).each(fields, new Debug("#### vehicles"));
Stream slowVehiclesStream = vehiclesStream.min(new SpeedComparator()).each(vehicleField, new Debug("#### slowest vehicle")).peek(new SpeedValidator(minSpeed));
Stream slowDriversStream = slowVehiclesStream.project(driverField).each(driverField, new Debug("#### slowest driver"));
vehiclesStream.max(new SpeedComparator()).each(vehicleField, new Debug("#### fastest vehicle")).peek(new SpeedValidator(maxSpeed)).project(driverField).each(driverField, new Debug("#### fastest driver"));
vehiclesStream.minBy(Vehicle.FIELD_NAME, new EfficiencyComparator()).each(vehicleField, new Debug("#### least efficient vehicle")).peek(new EfficiencyValidator(minEfficiency));
vehiclesStream.maxBy(Vehicle.FIELD_NAME, new EfficiencyComparator()).each(vehicleField, new Debug("#### most efficient vehicle")).peek(new EfficiencyValidator(maxEfficiency));
Map config = new HashMap();
config.put(Config.TOPOLOGY_NAME, "TridentMinMaxOfVehiclesTest");
// use the assert in the body of consumer.accept() to validate
JStormUnitTestRunner.submitTopology(tridentTopology.build(), null, 120, null);
}
use of storm.trident.operation.builtin.Debug in project jstorm by alibaba.
the class TridentMinMaxOfDevicesTopology method buildDevicesTopology.
/**
* Creates a topology with device-id and count (which are whole numbers) as
* tuple fields in a stream and it finally generates result stream based on
* min amd max with device-id and count values.
*/
public static StormTopology buildDevicesTopology() {
String deviceID = "device-id";
String count = "count";
Fields allFields = new Fields(deviceID, count);
RandomNumberGeneratorSpout spout = new RandomNumberGeneratorSpout(allFields, 10, 1000);
TridentTopology topology = new TridentTopology();
Stream devicesStream = topology.newStream("devicegen-spout", spout).each(allFields, new Debug("##### devices"));
devicesStream.minBy(deviceID).each(allFields, new Debug("#### device with min id"));
devicesStream.maxBy(count).each(allFields, new Debug("#### device with max count"));
return topology.build();
}
use of storm.trident.operation.builtin.Debug in project jstorm by alibaba.
the class TridentMinMaxOfDevicesTopology method buildVehiclesTopology.
/**
* Creates a topology which demonstrates min/max operations on tuples of
* stream which contain vehicle and driver fields with values
* {@link TridentMinMaxOfDevicesTopology.Vehicle} and
* {@link TridentMinMaxOfDevicesTopology.Driver} respectively.
*/
public static StormTopology buildVehiclesTopology() {
Fields driverField = new Fields(Driver.FIELD_NAME);
Fields vehicleField = new Fields(Vehicle.FIELD_NAME);
Fields allFields = new Fields(Vehicle.FIELD_NAME, Driver.FIELD_NAME);
FixedBatchSpout spout = new FixedBatchSpout(allFields, 10, Vehicle.generateVehicles(20));
spout.setCycle(true);
TridentTopology topology = new TridentTopology();
Stream vehiclesStream = topology.newStream("spout1", spout).each(allFields, new Debug("##### vehicles"));
Stream slowVehiclesStream = vehiclesStream.min(new SpeedComparator()).each(vehicleField, new Debug("#### slowest vehicle"));
Stream slowDriversStream = slowVehiclesStream.project(driverField).each(driverField, new Debug("##### slowest driver"));
vehiclesStream.max(new SpeedComparator()).each(vehicleField, new Debug("#### fastest vehicle")).project(driverField).each(driverField, new Debug("##### fastest driver"));
vehiclesStream.max(new EfficiencyComparator()).each(vehicleField, new Debug("#### efficient vehicle"));
return topology.build();
}
use of storm.trident.operation.builtin.Debug in project jstorm by alibaba.
the class TridentMinMaxOfVehiclesTopology method buildVehiclesTopology.
/**
* Creates a topology which demonstrates min/max operations on tuples of
* stream which contain vehicle and driver fields with values
* {@link TridentMinMaxOfVehiclesTopology.Vehicle} and
* {@link TridentMinMaxOfVehiclesTopology.Driver} respectively.
*/
public static StormTopology buildVehiclesTopology() {
Fields driverField = new Fields(Driver.FIELD_NAME);
Fields vehicleField = new Fields(Vehicle.FIELD_NAME);
Fields allFields = new Fields(Vehicle.FIELD_NAME, Driver.FIELD_NAME);
FixedBatchSpout spout = new FixedBatchSpout(allFields, 10, Vehicle.generateVehicles(20));
spout.setCycle(true);
TridentTopology topology = new TridentTopology();
Stream vehiclesStream = topology.newStream("spout1", spout).each(allFields, new Debug("##### vehicles"));
Stream slowVehiclesStream = vehiclesStream.min(new SpeedComparator()).each(vehicleField, new Debug("#### slowest vehicle"));
Stream slowDriversStream = slowVehiclesStream.project(driverField).each(driverField, new Debug("##### slowest driver"));
vehiclesStream.max(new SpeedComparator()).each(vehicleField, new Debug("#### fastest vehicle")).project(driverField).each(driverField, new Debug("##### fastest driver"));
vehiclesStream.minBy(Vehicle.FIELD_NAME, new EfficiencyComparator()).each(vehicleField, new Debug("#### least efficient vehicle"));
vehiclesStream.maxBy(Vehicle.FIELD_NAME, new EfficiencyComparator()).each(vehicleField, new Debug("#### most efficient vehicle"));
return topology.build();
}
use of storm.trident.operation.builtin.Debug in project jstorm by alibaba.
the class TridentMinMaxOfDevicesTest method testTridentMinMaxOfDevices.
@Test
public void testTridentMinMaxOfDevices() {
Fields fields = new Fields("device-id", "count");
List<Values> content = new ArrayList<Values>();
for (int i = 0; i < SPOUT_BATCH_SIZE; i++) content.add(new Values(i + 1));
ShuffleValuesBatchSpout spout = new ShuffleValuesBatchSpout(fields, content, content);
TridentTopology tridentTopology = new TridentTopology();
Stream stream = tridentTopology.newStream("device-gen-spout", spout).each(fields, new Debug("#### devices"));
stream.minBy("device-id").each(fields, new AssertMinDebug());
stream.maxBy("count").each(fields, new AssertMaxDebug());
Map config = new HashMap();
config.put(Config.TOPOLOGY_NAME, "TridentMinMaxOfDevicesTest");
// the test can pass if the 2 AssertDebug pass throughout the test
JStormUnitTestRunner.submitTopology(tridentTopology.build(), config, 120, null);
}
Aggregations