Class Stream<T>

  • Type Parameters:
    T - the type of the value
    Direct Known Subclasses:
    PairStream

    @Unstable
    public class Stream<T>
    extends Object
    Represents a stream of values.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <A,​R>
      Stream<R>
      aggregate​(CombinerAggregator<? super T,​A,​? extends R> aggregator)
      Aggregates the values in this stream using the aggregator.
      <R> Stream<R> aggregate​(R initialValue, BiFunction<? super R,​? super T,​? extends R> accumulator, BiFunction<? super R,​? super R,​? extends R> combiner)
      Aggregates the values in this stream using the given initial value, accumulator and combiner.
      Stream<T>[] branch​(Predicate<? super T>... predicates)
      Returns an array of streams by splitting the given stream into multiple branches based on the given predicates.
      Stream<Long> count()
      Counts the number of values in this stream.
      Stream<T> filter​(Predicate<? super T> predicate)
      Returns a stream consisting of the elements of this stream that matches the given filter.
      <R> Stream<R> flatMap​(FlatMapFunction<? super T,​? extends R> function)
      Returns a stream consisting of the results of replacing each value of this stream with the contents produced by applying the provided mapping function to each value.
      <K,​V>
      PairStream<K,​V>
      flatMapToPair​(PairFlatMapFunction<? super T,​? extends K,​? extends V> function)
      Returns a stream consisting of the results of replacing each value of this stream with the key-value pairs produced by applying the provided mapping function to each value.
      void forEach​(Consumer<? super T> action)
      Performs an action for each element of this stream.
      <R> Stream<R> map​(Function<? super T,​? extends R> function)
      Returns a stream consisting of the result of applying the given mapping function to the values of this stream.
      <K,​V>
      PairStream<K,​V>
      mapToPair​(PairFunction<? super T,​? extends K,​? extends V> function)
      Returns a stream of key-value pairs by applying a PairFunction on each value of this stream.
      protected Stream<T> partitionBy​(Fields fields, int parallelism)  
      Stream<T> peek​(Consumer<? super T> action)
      Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as they are consumed from the resulting stream.
      void print()
      Print the values in this stream.
      Stream<T> reduce​(Reducer<T> reducer)
      Performs a reduction on the elements of this stream, by repeatedly applying the reducer.
      Stream<T> repartition​(int parallelism)
      Returns a new stream with the given value of parallelism.
      <V> PairStream<T,​V> stateQuery​(StreamState<T,​V> streamState)
      Queries the given stream state with the values in this stream as the keys.
      void to​(IBasicBolt bolt)
      Sends the elements of this stream to a bolt.
      void to​(IBasicBolt bolt, int parallelism)
      Sends the elements of this stream to a bolt.
      void to​(IRichBolt bolt)
      Sends the elements of this stream to a bolt.
      void to​(IRichBolt bolt, int parallelism)
      Sends the elements of this stream to a bolt.
      Stream<T> window​(Window<?,​?> window)
      Returns a new stream consisting of the elements that fall within the window as specified by the window parameter.
    • Field Detail

      • KEY

        protected static final Fields KEY
      • VALUE

        protected static final Fields VALUE
      • KEY_VALUE

        protected static final Fields KEY_VALUE
      • node

        protected final org.apache.storm.streams.Node node
      • stream

        protected final String stream
    • Method Detail

      • filter

        public Stream<T> filter​(Predicate<? super T> predicate)
        Returns a stream consisting of the elements of this stream that matches the given filter.
        Parameters:
        predicate - the predicate to apply to each element to determine if it should be included
        Returns:
        the new stream
      • map

        public <R> Stream<R> map​(Function<? super T,​? extends R> function)
        Returns a stream consisting of the result of applying the given mapping function to the values of this stream.
        Parameters:
        function - a mapping function to be applied to each value in this stream.
        Returns:
        the new stream
      • mapToPair

        public <K,​V> PairStream<K,​V> mapToPair​(PairFunction<? super T,​? extends K,​? extends V> function)
        Returns a stream of key-value pairs by applying a PairFunction on each value of this stream.
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        function - the mapping function to be applied to each value in this stream
        Returns:
        the new stream of key-value pairs
      • flatMap

        public <R> Stream<R> flatMap​(FlatMapFunction<? super T,​? extends R> function)
        Returns a stream consisting of the results of replacing each value of this stream with the contents produced by applying the provided mapping function to each value. This has the effect of applying a one-to-many transformation to the values of the stream, and then flattening the resulting elements into a new stream.
        Parameters:
        function - a mapping function to be applied to each value in this stream which produces new values.
        Returns:
        the new stream
      • flatMapToPair

        public <K,​V> PairStream<K,​V> flatMapToPair​(PairFlatMapFunction<? super T,​? extends K,​? extends V> function)
        Returns a stream consisting of the results of replacing each value of this stream with the key-value pairs produced by applying the provided mapping function to each value.
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        function - the mapping function to be applied to each value in this stream which produces new key-value pairs.
        Returns:
        the new stream of key-value pairs
        See Also:
        flatMap(FlatMapFunction), mapToPair(PairFunction)
      • window

        public Stream<T> window​(Window<?,​?> window)
        Returns a new stream consisting of the elements that fall within the window as specified by the window parameter. The Window specification could be used to specify sliding or tumbling windows based on time duration or event count. For example,
         // time duration based sliding window
         stream.window(SlidingWindows.of(Duration.minutes(10), Duration.minutes(1));
        
         // count based sliding window
         stream.window(SlidingWindows.of(Count.(10), Count.of(2)));
        
         // time duration based tumbling window
         stream.window(TumblingWindows.of(Duration.seconds(10));
         
        Parameters:
        window - the window configuration
        Returns:
        the new stream
        See Also:
        SlidingWindows, TumblingWindows
      • forEach

        public void forEach​(Consumer<? super T> action)
        Performs an action for each element of this stream.
        Parameters:
        action - an action to perform on the elements
      • peek

        public Stream<T> peek​(Consumer<? super T> action)
        Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as they are consumed from the resulting stream.
        Parameters:
        action - the action to perform on the element as they are consumed from the stream
        Returns:
        the new stream
      • aggregate

        public <A,​R> Stream<R> aggregate​(CombinerAggregator<? super T,​A,​? extends R> aggregator)
        Aggregates the values in this stream using the aggregator. This does a global aggregation of values across all partitions.

        If the stream is windowed, the aggregate result is emitted after each window activation and represents the aggregate of elements that fall within that window. If the stream is not windowed, the aggregate result is emitted as each new element in the stream is processed.

        Type Parameters:
        A - the accumulator type
        R - the result type
        Parameters:
        aggregator - the aggregator
        Returns:
        the new stream
      • aggregate

        public <R> Stream<R> aggregate​(R initialValue,
                                       BiFunction<? super R,​? super T,​? extends R> accumulator,
                                       BiFunction<? super R,​? super R,​? extends R> combiner)
        Aggregates the values in this stream using the given initial value, accumulator and combiner. This does a global aggregation of values across all partitions.

        If the stream is windowed, the aggregate result is emitted after each window activation and represents the aggregate of elements that fall within that window. If the stream is not windowed, the aggregate result is emitted as each new element in the stream is processed.

        Type Parameters:
        R - the result type
        Parameters:
        initialValue - the initial value of the result
        accumulator - the accumulator
        combiner - the combiner
        Returns:
        the new stream
      • count

        public Stream<Long> count()
        Counts the number of values in this stream. This does a global count of values across all partitions.

        If the stream is windowed, the counts are emitted after each window activation and represents the count of elements that fall within that window. If the stream is not windowed, the count is emitted as each new element in the stream is processed.

        Returns:
        the new stream
      • reduce

        public Stream<T> reduce​(Reducer<T> reducer)
        Performs a reduction on the elements of this stream, by repeatedly applying the reducer. This does a global reduction of values across all partitions.

        If the stream is windowed, the result is emitted after each window activation and represents the reduction of elements that fall within that window. If the stream is not windowed, the result is emitted as each new element in the stream is processed.

        Parameters:
        reducer - the reducer
        Returns:
        the new stream
      • repartition

        public Stream<T> repartition​(int parallelism)
        Returns a new stream with the given value of parallelism. Further operations on this stream would execute at this level of parallelism.
        Parameters:
        parallelism - the parallelism value
        Returns:
        the new stream
      • branch

        public Stream<T>[] branch​(Predicate<? super T>... predicates)
        Returns an array of streams by splitting the given stream into multiple branches based on the given predicates. The predicates are applied in the given order to the values of this stream and the result is forwarded to the corresponding (index based) result stream based on the (index of) predicate that matches.

        Note: If none of the predicates match a value, that value is dropped.

        Parameters:
        predicates - the predicates
        Returns:
        an array of result streams (branches) corresponding to the given predicates
      • print

        public void print()
        Print the values in this stream.
      • to

        public void to​(IRichBolt bolt)
        Sends the elements of this stream to a bolt. This could be used to plug in existing bolts as sinks in the stream, for e.g. a RedisStoreBolt. The bolt would have a parallelism of 1.

        Note: This would provide guarantees only based on what the bolt provides.

        Parameters:
        bolt - the bolt
      • to

        public void to​(IRichBolt bolt,
                       int parallelism)
        Sends the elements of this stream to a bolt. This could be used to plug in existing bolts as sinks in the stream, for e.g. a RedisStoreBolt.

        Note: This would provide guarantees only based on what the bolt provides.

        Parameters:
        bolt - the bolt
        parallelism - the parallelism of the bolt
      • to

        public void to​(IBasicBolt bolt)
        Sends the elements of this stream to a bolt. This could be used to plug in existing bolts as sinks in the stream, for e.g. a RedisStoreBolt. The bolt would have a parallelism of 1.

        Note: This would provide guarantees only based on what the bolt provides.

        Parameters:
        bolt - the bolt
      • to

        public void to​(IBasicBolt bolt,
                       int parallelism)
        Sends the elements of this stream to a bolt. This could be used to plug in existing bolts as sinks in the stream, for e.g. a RedisStoreBolt.

        Note: This would provide guarantees only based on what the bolt provides.

        Parameters:
        bolt - the bolt
        parallelism - the parallelism of the bolt
      • stateQuery

        public <V> PairStream<T,​V> stateQuery​(StreamState<T,​V> streamState)
        Queries the given stream state with the values in this stream as the keys.
        Type Parameters:
        V - the value type
        Parameters:
        streamState - the stream state
        Returns:
        the result stream
      • partitionBy

        protected Stream<T> partitionBy​(Fields fields,
                                        int parallelism)