Search in sources :

Example 1 with Stream

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);
}
Also used : Fields(backtype.storm.tuple.Fields) GroupedAggregator(storm.trident.operation.impl.GroupedAggregator) CombinerAggregator(storm.trident.operation.CombinerAggregator) Aggregator(storm.trident.operation.Aggregator) ReducerAggregator(storm.trident.operation.ReducerAggregator) GroupedAggregator(storm.trident.operation.impl.GroupedAggregator) Stream(storm.trident.Stream)

Example 2 with Stream

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);
}
Also used : Values(backtype.storm.tuple.Values) Fields(backtype.storm.tuple.Fields) TridentTopology(storm.trident.TridentTopology) Stream(storm.trident.Stream) Debug(storm.trident.operation.builtin.Debug) Test(org.junit.Test)

Example 3 with Stream

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();
}
Also used : FixedBatchSpout(storm.trident.testing.FixedBatchSpout) Fields(backtype.storm.tuple.Fields) Consumer(storm.trident.operation.Consumer) TridentTopology(storm.trident.TridentTopology) CountAsAggregator(storm.trident.testing.CountAsAggregator) Values(backtype.storm.tuple.Values) Stream(storm.trident.Stream) Split(storm.trident.testing.Split) TridentTuple(storm.trident.tuple.TridentTuple)

Example 4 with Stream

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();
}
Also used : Fields(backtype.storm.tuple.Fields) TridentTopology(storm.trident.TridentTopology) Stream(storm.trident.Stream) RandomNumberGeneratorSpout(org.apache.storm.starter.spout.RandomNumberGeneratorSpout) Debug(storm.trident.operation.builtin.Debug)

Example 5 with Stream

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);
}
Also used : Fields(backtype.storm.tuple.Fields) TridentTopology(storm.trident.TridentTopology) CountAsAggregator(storm.trident.testing.CountAsAggregator) HashMap(java.util.HashMap) Values(backtype.storm.tuple.Values) InMemoryWindowsStoreFactory(storm.trident.windowing.InMemoryWindowsStoreFactory) Stream(storm.trident.Stream) Split(storm.trident.testing.Split) HashMap(java.util.HashMap) Map(java.util.Map) WindowsStoreFactory(storm.trident.windowing.WindowsStoreFactory) InMemoryWindowsStoreFactory(storm.trident.windowing.InMemoryWindowsStoreFactory) Test(org.junit.Test)

Aggregations

Fields (backtype.storm.tuple.Fields)12 Stream (storm.trident.Stream)12 TridentTopology (storm.trident.TridentTopology)10 Values (backtype.storm.tuple.Values)7 Test (org.junit.Test)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Debug (storm.trident.operation.builtin.Debug)5 CountAsAggregator (storm.trident.testing.CountAsAggregator)5 Split (storm.trident.testing.Split)5 InMemoryWindowsStoreFactory (storm.trident.windowing.InMemoryWindowsStoreFactory)4 WindowsStoreFactory (storm.trident.windowing.WindowsStoreFactory)4 FixedBatchSpout (storm.trident.testing.FixedBatchSpout)3 Aggregator (storm.trident.operation.Aggregator)2 CombinerAggregator (storm.trident.operation.CombinerAggregator)2 ReducerAggregator (storm.trident.operation.ReducerAggregator)2 GroupedAggregator (storm.trident.operation.impl.GroupedAggregator)2 ArrayList (java.util.ArrayList)1 RandomNumberGeneratorSpout (org.apache.storm.starter.spout.RandomNumberGeneratorSpout)1 Consumer (storm.trident.operation.Consumer)1