use of storm.trident.Stream in project jstorm by alibaba.
the class GroupedStream method partitionAggregate.
@Override
public IAggregatableStream partitionAggregate(Fields inputFields, Aggregator agg, Fields functionFields) {
Aggregator groupedAgg = new GroupedAggregator(agg, _groupFields, inputFields, functionFields.size());
Fields allInFields = TridentUtils.fieldsUnion(_groupFields, inputFields);
Fields allOutFields = TridentUtils.fieldsConcat(_groupFields, functionFields);
Stream s = _stream.partitionAggregate(allInFields, groupedAgg, allOutFields);
return new GroupedStream(s, _groupFields);
}
use of storm.trident.Stream 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.Stream in project jstorm by alibaba.
the class TridentWindowingInmemoryStoreTopology method buildTopology.
public static StormTopology buildTopology(WindowsStoreFactory windowStore, WindowConfig windowConfig) throws Exception {
FixedBatchSpout spout = new FixedBatchSpout(new Fields("sentence"), 3, new Values("the cow jumped over the moon"), new Values("the man went to the store and bought some candy"), new Values("four score and seven years ago"), new Values("how many apples can you eat"), new Values("to be or not to be the person"));
spout.setCycle(true);
TridentTopology topology = new TridentTopology();
Stream stream = topology.newStream("spout1", spout).parallelismHint(16).each(new Fields("sentence"), new Split(), new Fields("word")).window(windowConfig, windowStore, new Fields("word"), new CountAsAggregator(), new Fields("count")).peek(new Consumer() {
@Override
public void accept(TridentTuple input) {
LOG.info("Received tuple: [{}]", input);
}
});
return topology.build();
}
use of storm.trident.Stream 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.Stream in project jstorm by alibaba.
the class TridentTumblingCountWindowTest method testTridentTumblingCountWindow.
@Test
public void testTridentTumblingCountWindow() {
WindowsStoreFactory windowsStoreFactory = new InMemoryWindowsStoreFactory();
FixedLimitBatchSpout spout = new FixedLimitBatchSpout(SPOUT_LIMIT, new Fields("sentence"), SPOUT_BATCH_SIZE, new Values("the cow jumped over the moon"), new Values("the man went to the store and bought some candy"), new Values("four score and seven years ago"), new Values("how many apples can you eat"), new Values("to be or not to be the person"));
TridentTopology tridentTopology = new TridentTopology();
Stream stream = tridentTopology.newStream("spout1", spout).parallelismHint(16).each(new Fields("sentence"), new Split(), new Fields("word")).window(windowConfig, windowsStoreFactory, new Fields("word"), new CountAsAggregator(), new Fields("count")).peek(new ValidateConsumer());
Map config = new HashMap();
config.put(Config.TOPOLOGY_NAME, "TridentTumblingCountWindowTest");
JStormUnitTestRunner.submitTopology(tridentTopology.build(), null, 120, null);
}
Aggregations