Search in sources :

Example 1 with Consumer

use of org.apache.storm.streams.operations.Consumer in project IndyCar by DSC-SPIDAL.

the class StormTest method main.

public static void main(String[] args) throws Exception {
    StreamBuilder streamBuilder = new StreamBuilder();
    // start with source
    Stream<Tuple> sourceStream = streamBuilder.newStream(new BaseRichSpout() {

        private SpoutOutputCollector collector;

        private Random random;

        public void open(Map<String, Object> map, TopologyContext topologyContext, SpoutOutputCollector spoutOutputCollector) {
            this.collector = spoutOutputCollector;
            this.random = new Random(System.currentTimeMillis());
        }

        public void nextTuple() {
            Object[] tuple = new Object[] { this.random.nextInt(33), random.nextFloat(), random.nextFloat(), random.nextFloat() };
            this.collector.emit(Arrays.asList(tuple));
        }

        public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
            outputFieldsDeclarer.declare(new Fields("car", "speed", "rpm", "throttle"));
        }
    }, 1);
    // split for 33 cars
    Predicate<Tuple>[] branchingPredicates = new Predicate[33];
    for (int i = 0; i < 33; i++) {
        final int index = i;
        branchingPredicates[i] = tuple -> tuple.getInteger(0) == index;
    }
    Stream<Tuple>[] carBranches = sourceStream.branch(branchingPredicates);
    for (Stream<Tuple> carBranch : carBranches) {
        PairStream<Integer, List<Float>> carBranchPaired = carBranch.mapToPair((PairFunction<Tuple, Integer, List<Float>>) tuple -> {
            List<Float> values = new ArrayList<>();
            values.add(tuple.getFloat(1));
            values.add(tuple.getFloat(2));
            values.add(tuple.getFloat(3));
            return Pair.of(tuple.getInteger(0), values);
        });
        // car branch has all the raw data
        PairStream joinedStream = carBranchPaired;
        for (int i = 0; i < 3; i++) {
            int metricIndex = i;
            PairStream<Integer, Float> anomalyScoreStream = carBranch.mapToPair(new PairFunction<Tuple, Integer, Float>() {

                // here we should initialize htm java and FIFO blocking mechanism should be created
                private Random htm = new Random(System.currentTimeMillis());

                @Override
                public Pair<Integer, Float> apply(Tuple tuple) {
                    // + 1 because 0 is the card number
                    Float rawData = tuple.getFloat(metricIndex + 1);
                    try {
                        // random sleep to simulate processing time
                        Thread.sleep(htm.nextInt(6));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    return Pair.of(tuple.getInteger(0), rawData + 10000);
                }
            });
            joinedStream = joinedStream.join(anomalyScoreStream, new ValueJoiner() {

                @Override
                public Object apply(Object o, Object o2) {
                    List<Float> combined = (List<Float>) o;
                    combined.add((Float) o2);
                    return combined;
                }
            });
        }
        joinedStream.forEach(new Consumer() {

            @Override
            public void accept(Object o) {
                // publish to websockets or MQTT
                System.out.println(o);
            }
        });
    }
    try (LocalCluster cluster = new LocalCluster()) {
        cluster.submitTopology("indycar-stream", Collections.singletonMap(Config.TOPOLOGY_MAX_TASK_PARALLELISM, 33), streamBuilder.build());
        Thread.sleep(10000000);
    }
}
Also used : Consumer(org.apache.storm.streams.operations.Consumer) OutputFieldsDeclarer(org.apache.storm.topology.OutputFieldsDeclarer) BaseRichSpout(org.apache.storm.topology.base.BaseRichSpout) java.util(java.util) Pair(org.apache.storm.streams.Pair) PairStream(org.apache.storm.streams.PairStream) StreamBuilder(org.apache.storm.streams.StreamBuilder) TopologyContext(org.apache.storm.task.TopologyContext) Fields(org.apache.storm.tuple.Fields) LocalCluster(org.apache.storm.LocalCluster) ValueJoiner(org.apache.storm.streams.operations.ValueJoiner) Tuple(org.apache.storm.tuple.Tuple) PairFunction(org.apache.storm.streams.operations.PairFunction) Predicate(org.apache.storm.streams.operations.Predicate) Stream(org.apache.storm.streams.Stream) Config(org.apache.storm.Config) SpoutOutputCollector(org.apache.storm.spout.SpoutOutputCollector) LocalCluster(org.apache.storm.LocalCluster) OutputFieldsDeclarer(org.apache.storm.topology.OutputFieldsDeclarer) StreamBuilder(org.apache.storm.streams.StreamBuilder) Predicate(org.apache.storm.streams.operations.Predicate) ValueJoiner(org.apache.storm.streams.operations.ValueJoiner) Consumer(org.apache.storm.streams.operations.Consumer) PairStream(org.apache.storm.streams.PairStream) Stream(org.apache.storm.streams.Stream) TopologyContext(org.apache.storm.task.TopologyContext) Pair(org.apache.storm.streams.Pair) PairStream(org.apache.storm.streams.PairStream) Fields(org.apache.storm.tuple.Fields) SpoutOutputCollector(org.apache.storm.spout.SpoutOutputCollector) Tuple(org.apache.storm.tuple.Tuple) BaseRichSpout(org.apache.storm.topology.base.BaseRichSpout)

Aggregations

java.util (java.util)1 Config (org.apache.storm.Config)1 LocalCluster (org.apache.storm.LocalCluster)1 SpoutOutputCollector (org.apache.storm.spout.SpoutOutputCollector)1 Pair (org.apache.storm.streams.Pair)1 PairStream (org.apache.storm.streams.PairStream)1 Stream (org.apache.storm.streams.Stream)1 StreamBuilder (org.apache.storm.streams.StreamBuilder)1 Consumer (org.apache.storm.streams.operations.Consumer)1 PairFunction (org.apache.storm.streams.operations.PairFunction)1 Predicate (org.apache.storm.streams.operations.Predicate)1 ValueJoiner (org.apache.storm.streams.operations.ValueJoiner)1 TopologyContext (org.apache.storm.task.TopologyContext)1 OutputFieldsDeclarer (org.apache.storm.topology.OutputFieldsDeclarer)1 BaseRichSpout (org.apache.storm.topology.base.BaseRichSpout)1 Fields (org.apache.storm.tuple.Fields)1 Tuple (org.apache.storm.tuple.Tuple)1