T
- Tuple type.public interface TStream<T> extends TopologyElement
TStream
is a declaration of a continuous sequence of tuples. A
connected topology of streams and functional transformations is built using
Topology
. filter
, map (or transform)
or sink
this declared stream using a
function.
TStream
is not a runtime representation of a stream,
it is a declaration used in building a topology.
The actual runtime stream is created once the topology
is submitted
to a runtime.
Modifier and Type | Method and Description |
---|---|
TStream<java.lang.String> |
asString()
Convert this stream to a stream of
String tuples by calling
toString() on each tuple. |
TStream<T> |
filter(Predicate<T> predicate)
Declare a new stream that filters tuples from this stream.
|
<U> TStream<U> |
flatMap(Function<T,java.lang.Iterable<U>> mapper)
Declare a new stream that maps tuples from this stream into one or
more (or zero) tuples of a different type
U . |
java.util.Set<java.lang.String> |
getTags()
Returns the set of tags associated with this stream.
|
<K> TWindow<T,K> |
last(int count,
Function<T,K> keyFunction)
Declare a partitioned window that continually represents the last
count
tuples on this stream for each partition. |
<K> TWindow<T,K> |
last(long time,
java.util.concurrent.TimeUnit unit,
Function<T,K> keyFunction)
Declare a partitioned window that continually represents the last
time seconds of
tuples on this stream for each partition. |
<U> TStream<U> |
map(Function<T,U> mapper)
Declare a new stream that maps (or transforms) each tuple from this stream into one
(or zero) tuple of a different type
U . |
TStream<T> |
modify(UnaryOperator<T> modifier)
Declare a new stream that modifies each tuple from this stream into one
(or zero) tuple of the same type
T . |
TStream<T> |
peek(Consumer<T> peeker)
Declare a stream that contains the same contents as this stream while
peeking at each element using
peeker . |
<U> TStream<U> |
pipe(Pipe<T,U> pipe)
Declare a stream that contains the output of the specified
Pipe
oplet applied to this stream. |
TSink<T> |
print()
Utility method to print the contents of this stream
to
System.out at runtime. |
TSink<T> |
sink(Consumer<T> sinker)
Sink (terminate) this stream using a function.
|
TSink<T> |
sink(Sink<T> oplet)
Sink (terminate) this stream using a oplet.
|
java.util.List<TStream<T>> |
split(int n,
ToIntFunction<T> splitter)
Split a stream's tuples among
n streams as specified by
splitter . |
TStream<T> |
tag(java.lang.String... values)
Adds the specified tags to the stream.
|
TStream<T> |
union(java.util.Set<TStream<T>> others)
Declare a stream that will contain all tuples from this stream and all the
streams in
others . |
TStream<T> |
union(TStream<T> other)
Declare a stream that will contain all tuples from this stream and
other . |
topology
TStream<T> filter(Predicate<T> predicate)
t
on this stream will appear in the returned stream if
filter.test(t)
returns true
. If
filter.test(t)
returns false
then then t
will not
appear in the returned stream.
Examples of filtering out all empty strings from stream s
of type
String
TStream<String> s = ...
TStream<String> filtered = s.filter(t -> !t.isEmpty());
predicate
- Filtering logic to be executed against each tuple.<U> TStream<U> map(Function<T,U> mapper)
U
. For each tuple t
on this stream, the returned stream will contain a tuple that is the
result of mapper.apply(t)
when the return is not null
.
If mapper.apply(t)
returns null
then no tuple
is submitted to the returned stream for t
.
Examples of transforming a stream containing numeric values as
String
objects into a stream of Double
values.
// Using lambda expression
TStream<String> strings = ...
TStream<Double> doubles = strings.map(v -> Double.valueOf(v));
// Using method reference
TStream<String> strings = ...
TStream<Double> doubles = strings.map(Double::valueOf);
mapper
- Mapping logic to be executed against each tuple.U
mapped from this
stream's tuples.<U> TStream<U> flatMap(Function<T,java.lang.Iterable<U>> mapper)
U
. For each tuple
t
on this stream, the returned stream will contain all non-null tuples in
the Iterator<U>
that is the result of mapper.apply(t)
.
Tuples will be added to the returned stream in the order the iterator
returns them.
t
.
Examples of mapping a stream containing lines of text into a stream of words split out from each line. The order of the words in the stream will match the order of the words in the lines.
TStream<String> lines = ...
TStream<String> words = lines.flatMap(
line -> Arrays.asList(line.split(" ")));
U
- Type of mapped input tuples.mapper
- Mapper logic to be executed against each tuple.U
mapped and flattened from this
stream's tuples.java.util.List<TStream<T>> split(int n, ToIntFunction<T> splitter)
n
streams as specified by
splitter
.
For each tuple on the stream, splitter.applyAsInt(tuple)
is
called. The return value r
determines the destination stream:
if r < 0 the tuple is discarded else it is sent to the stream at position (r % n) in the returned array.
Each split TStream
is exposed by the API. The user has full
control over the each stream's processing pipeline. Each stream's
pipeline must be declared explicitly. Each stream can have different
processing pipelines.
An N-way split()
is logically equivalent to a collection of N
filter()
invocations, each with a predicate to select the tuples
for its stream. split()
is more efficient. Each tuple is analyzed
only once by a single splitter
instance to identify the
destination stream. For example, these are logically equivalent:
List<TStream<String>> streams = stream.split(2, tuple -> tuple.length()); TStream<String> stream0 = stream.filter(tuple -> (tuple.length() % 2) == 0); TStream<String> stream1 = stream.filter(tuple -> (tuple.length() % 2) == 1);
Example of splitting a stream of log records by their level attribute:
TStream<LogRecord> lrs = ...
List<<TStream<LogRecord>> splits = lrr.split(3, lr -> {
if (SEVERE.equals(lr.getLevel()))
return 0;
else if (WARNING.equals(lr.getLevel()))
return 1;
else
return 2;
});
splits.get(0). ... // SEVERE log record processing pipeline
splits.get(1). ... // WARNING log record processing pipeline
splits.get(2). ... // "other" log record processing pipeline
n
- the number of output streamssplitter
- the splitter functionn
streamsjava.lang.IllegalArgumentException
- if n <= 0
TStream<T> peek(Consumer<T> peeker)
peeker
. t
on this stream, peeker.accept(t)
will be
called.peeker
- Function to be called for each tuple.this
TSink<T> sink(Consumer<T> sinker)
t
on this stream
sinker.accept(t)
will be called. This is
typically used to send information to external systems, such as databases
or dashboards.
If sinker
implements AutoCloseable
, its close()
method will be called when the topology's execution is terminated.
Example of terminating a stream of String
tuples by printing them
to System.out
.
TStream<String> values = ...
values.sink(t -> System.out.println(tuple));
sinker
- Logic to be executed against each tuple on this stream.TSink<T> sink(Sink<T> oplet)
sink(Consumer)
with a full life-cycle of
the oplet as well as easy access to
runtime services
.oplet
- Oplet processes each tuple without producing output.<U> TStream<U> pipe(Pipe<T,U> pipe)
Pipe
oplet applied to this stream.U
- Tuple type of the returned stream.pipe
- The Pipe
oplet.TStream<T> modify(UnaryOperator<T> modifier)
T
. For each tuple t
on this stream, the returned stream will contain a tuple that is the
result of modifier.apply(t)
when the return is not null
.
The function may return the same reference as its input t
or
a different object of the same type.
If modifier.apply(t)
returns null
then no tuple
is submitted to the returned stream for t
.
Example of modifying a stream String
values by adding the suffix 'extra
'.
TStream<String> strings = ...
TStream<String> modifiedStrings = strings.modify(t -> t.concat("extra"));
This method is equivalent to
map(Function<T,T> modifier
).
modifier
- Modifier logic to be executed against each tuple.T
modified from this
stream's tuples.TStream<java.lang.String> asString()
String
tuples by calling
toString()
on each tuple. This is equivalent to
map(Object::toString)
.TSink<T> print()
System.out
at runtime. Each tuple is printed
using System.out.println(tuple)
.TSink
for the sink processing.<K> TWindow<T,K> last(int count, Function<T,K> keyFunction)
count
tuples on this stream for each partition. Each partition independently maintains the last
count
tuples for each key seen on this stream.
If no tuples have been seen on the stream for a key then the corresponding partition will be empty.
keyFunction
.
For each tuple on the stream keyFunction.apply(tuple)
is called
and the returned value is the tuple's key. For any two tuples ta,tb
in a partition
keyFunction.apply(ta).equals(keyFunction.apply(tb))
is true.
equals()
and hashCode()
correctly.
To create a window partitioned using the tuple as the key use identity()
as the key function.
To create an unpartitioned window use a key function that returns a constant,
by convention unpartitioned()
is recommended.
K
- Key type.count
- Number of tuples to maintain in each partition.keyFunction
- Function that defines the key for each tuple.count
tuples for each partition.<K> TWindow<T,K> last(long time, java.util.concurrent.TimeUnit unit, Function<T,K> keyFunction)
time
seconds of
tuples on this stream for each partition. If no tuples have been
seen on the stream for a key in the last time
seconds then the partition will be empty.
Each partition independently maintains the last
count
tuples for each key seen on this stream.
keyFunction
.
For each tuple on the stream keyFunction.apply(tuple)
is called
and the returned value is the tuple's key. For any two tuples ta,tb
in a partition
keyFunction.apply(ta).equals(keyFunction.apply(tb))
is true.
equals()
and hashCode()
correctly.
To create a window partitioned using the tuple as the key use identity()
as the key function.
To create an unpartitioned window use a key function that returns a constant,
by convention unpartitioned()
is recommended.
K
- Key type.time
- Time to retain a tuple in a partition.unit
- Unit for time
.keyFunction
- Function that defines the key for each tuple.count
tuple.TStream<T> union(TStream<T> other)
other
. A stream cannot be unioned with itself, in this case
this
will be returned.other
- this
and other
.TStream<T> union(java.util.Set<TStream<T>> others)
others
. A stream cannot be unioned with itself, in
this case the union will only contain tuples from this stream once. If
others
is empty or only contains this
then this
is returned.others
- Stream to union with this stream.this
and others
.TStream<T> tag(java.lang.String... values)
values
- Tag values.java.util.Set<java.lang.String> getTags()
Copyright IBM 2015,2016 - 2f6ad0e-20160307-0902