T
- Tuple type, any instance of T
at runtime must be
serializable.public interface TStream<T> extends TopologyElement, Placeable<TStream<T>>
TStream
is a declaration of a continuous sequence of tuples. A
connected topology of streams and functional transformations is built using
Topology
. filter
, transform
or sink
this declared stream using a
function. com.ibm.streams.topology.streams
package
provide specific source streams, or transformations on streams with specific
types.
TStream
implements Placeable
to allow placement
directives against the processing that produced this stream.
For example, calling a Placeable
method on the stream
returned from filter(Predicate)
will apply to the
container that is executing the Predicate
passed into filter()
.
When multiple streams are produced by a method (e.g. split(int, ToIntFunction)
placement directives are common to all of the produced streams.
Modifier and Type | Interface and Description |
---|---|
static class |
TStream.Routing
Enumeration for routing tuples to parallel channels.
|
Modifier and Type | Method and Description |
---|---|
TStream<T> |
asType(java.lang.Class<T> tupleTypeClass)
Return a strongly typed reference to this stream.
|
TStream<T> |
autonomous()
Return a stream matching this stream whose subsequent
processing will execute in an autonomous region.
|
com.ibm.streamsx.topology.builder.BInputPort |
connectTo(com.ibm.streamsx.topology.builder.BOperatorInvocation receivingBop,
boolean functional,
com.ibm.streamsx.topology.builder.BInputPort input)
Internal method.
|
TStream<T> |
endLowLatency()
Return a TStream that is no longer guaranteed to run in the same process
as the calling stream.
|
TStream<T> |
endParallel()
Ends a parallel region by merging the channels into a single stream.
|
TStream<T> |
filter(Predicate<T> filter)
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 . |
TSink |
forEach(Consumer<T> action)
Sink (terminate) this stream.
|
java.lang.Class<T> |
getTupleClass()
Class of the tuples on this stream, if known.
|
java.lang.reflect.Type |
getTupleType()
Type of the tuples on this stream.
|
TStream<T> |
isolate()
Return a stream whose immediate subsequent processing will execute
in a separate operating system process from this stream's processing.
|
<J,U,K> TStream<J> |
join(Function<T,K> keyer,
TWindow<U,K> window,
BiFunction<T,java.util.List<U>,J> joiner)
Join this stream with a partitioned window of type
U with key type K . |
<J,U> TStream<J> |
join(TWindow<U,?> window,
BiFunction<T,java.util.List<U>,J> joiner)
Join this stream with window of type
U . |
<J,U,K> TStream<J> |
joinLast(Function<? super T,? extends K> keyer,
TStream<U> lastStream,
Function<? super U,? extends K> lastStreamKeyer,
BiFunction<T,U,J> joiner)
Join this stream with the last tuple seen on a stream of type
U
with partitioning. |
<J,U> TStream<J> |
joinLast(TStream<U> lastStream,
BiFunction<T,U,J> joiner)
Join this stream with the last tuple seen on a stream of type
U . |
TWindow<T,java.lang.Object> |
last()
Declare a
TWindow that continually represents the last tuple on this stream. |
TWindow<T,java.lang.Object> |
last(int count)
Declare a
TWindow that continually represents the last count tuples
seen on this stream. |
TWindow<T,java.lang.Object> |
last(long time,
java.util.concurrent.TimeUnit unit)
Declare a
TWindow that continually represents the last time seconds
of tuples (in the given time unit ) on this stream. |
TWindow<T,java.lang.Object> |
last(Supplier<java.lang.Integer> count)
Declare a
TWindow that continually represents the last count tuples
seen on this stream. |
TWindow<T,java.lang.Object> |
lastSeconds(Supplier<java.lang.Integer> time)
Declare a
TWindow that continually represents the last time seconds
of tuples on this stream. |
TStream<T> |
lowLatency()
Return a stream that is guaranteed to run in the same process as the
calling TStream.
|
<U> TStream<U> |
map(Function<T,U> mapper)
Declare a new stream that maps 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 . |
<U> TStream<U> |
multiTransform(Function<T,java.lang.Iterable<U>> transformer)
Declare a new stream that maps tuples from this stream into one or
more (or zero) tuples of a different type
U . |
com.ibm.streamsx.topology.builder.BOutput |
output()
Internal method.
|
TStream<T> |
parallel(int width)
Parallelizes the stream into a a fixed
number of parallel channels using round-robin distribution.
|
TStream<T> |
parallel(Supplier<java.lang.Integer> width,
Function<T,?> keyer)
Parallelizes the stream into a number of parallel channels
using key based distribution.
|
TStream<T> |
parallel(Supplier<java.lang.Integer> width,
TStream.Routing routing)
Parallelizes the stream into
width parallel channels. |
TStream<T> |
parallel(Supplier<java.lang.Integer> width)
Parallelizes the stream into
width parallel channels. |
TSink |
print()
Print each tuple on
System.out . |
void |
publish(java.lang.String topic,
boolean allowFilter)
Publish tuples from this stream for consumption by other IBM Streams applications.
|
void |
publish(java.lang.String topic)
Publish tuples from this stream for consumption by other IBM Streams applications.
|
void |
publish(Supplier<java.lang.String> topic,
boolean allowFilter)
Publish tuples from this stream for consumption by other IBM Streams applications.
|
void |
publish(Supplier<java.lang.String> topic)
Publish tuples from this stream for consumption by other IBM Streams applications.
|
TStream<T> |
sample(double fraction)
Return a stream that is a random sample of this stream.
|
TStream<T> |
setConsistent(ConsistentRegionConfig config)
Set the operator that is the source of this stream to be the start of a
consistent region to support at least once and exactly once
processing.
|
TStream<T> |
setParallel(Supplier<java.lang.Integer> width)
Sets the current stream as the start of a parallel region.
|
TSink |
sink(Consumer<T> sinker)
Terminate this stream.
|
java.util.List<TStream<T>> |
split(int n,
ToIntFunction<T> splitter)
Distribute a stream's tuples among
n streams
as specified by a splitter . |
TStream<T> |
throttle(long delay,
java.util.concurrent.TimeUnit unit)
Throttle a stream by ensuring any tuple is submitted with least
delay from the previous tuple. |
<U> TStream<U> |
transform(Function<T,U> transformer)
Declare a new stream that transforms each tuple from this stream into one
(or zero) tuple of a different type
U . |
TStream<T> |
union(java.util.Set<TStream<T>> others)
Create a stream that is a union of this stream and
others streams
of the same type T . |
TStream<T> |
union(TStream<T> other)
Create a stream that is a union of this stream and
other stream
of the same type T . |
TWindow<T,java.lang.Object> |
window(TWindow<?,?> configWindow)
Declare a
TWindow on this stream that has the same configuration
as another window. |
addResourceTags, colocate, getInvocationName, getResourceTags, invocationName, isPlaceable, operator
builder, topology
TStream<T> filter(Predicate<T> filter)
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
// Java 8 - Using lambda expression
TStream<String> s = ...
TStream<String> filtered = s.filter(t -> !t.isEmpty());
// Java 7 - Using anonymous class
TStream<String> s = ...
TStream<String> filtered = s.filter(new Predicate<String>() {
@Override
public boolean test(String t) {
return !t.isEmpty();
}} );
filter
- Filtering logic to be executed against each tuple.split(int, ToIntFunction)
java.util.List<TStream<T>> split(int n, ToIntFunction<T> splitter)
n
streams
as specified by a 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, mySplitter()); TStream<String> stream0 = stream.filter(myPredicate("ch0")); TStream<String> stream1 = stream.filter(myPredicate("ch1"));
parallel(Supplier, Routing)
also distributes a stream's
tuples among N-channels but it presents a different usage model.
The user specifies a single logical processing pipeline and
the logical pipeline is transparently replicated for each of the channels.
The API does not provide access to the individual channels in
the logical stream.
endParallel()
declares the end of the parallel pipeline and combines
all of the channels into a single resulting stream.
Example of splitting a stream of tuples by their severity attribute:
interface MyType { String severity; ... };
TStream<MyType> s = ...
List<<TStream<MyType>> splits = s.split(3, new ToIntFunction<MyType>() {
@Override
public int applyAsInt(MyType tuple) {
if(tuple.severity.equals("high"))
return 0;
else if(tuple.severity.equals("low"))
return 1;
else
return 2;
}} );
splits.get(0). ... // high severity processing pipeline
splits.get(1). ... // low severity processing pipeline
splits.get(2). ... // "other" severity processing pipeline
n
- the number of output streamssplitter
- the splitter functionn
streamsjava.lang.IllegalArgumentException
- if n <= 0
parallel(Supplier, Routing)
<U> TStream<U> transform(Function<T,U> transformer)
U
. For each tuple t
on this stream, the returned stream will contain a tuple that is the
result of transformer.apply(t)
when the return is not null
.
If transformer.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.
// Java 8 - Using lambda expression
TStream<String> strings = ...
TStream<Double> doubles = strings.transform(v -> Double.valueOf(v));
// Java 8 - Using method reference
TStream<String> strings = ...
TStream<Double> doubles = strings.transform(Double::valueOf);
// Java 7 - Using anonymous class
TStream<String> strings = ...
TStream<Double> doubles = strings.transform(new Function() {
@Override
public Double apply(String v) {
return Double.valueOf(v);
}});
This function is equivalent to map(Function)
.
transformer
- Transformation logic to be executed against each tuple.U
transformed from this
stream's tuples.<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 mapping a stream containing numeric values as
String
objects into a stream of Double
values.
// Java 8 - Using lambda expression
TStream<String> strings = ...
TStream<Double> doubles = strings.map(v -> Double.valueOf(v));
// Java 8 - Using method reference
TStream<String> strings = ...
TStream<Double> doubles = strings.map(Double::valueOf);
// Java 7 - Using anonymous class
TStream<String> strings = ...
TStream<Double> doubles = strings.map(new Function() {
@Override
public Double apply(String v) {
return Double.valueOf(v);
}});
This function is equivalent to transform(Function)
.
The typical term in most apis is map
.
mapper
- Mapping logic to be executed against each tuple.U
mapped from this
stream's tuples.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(new UnaryOperator() {
@Override
public String apply(String tuple) {
return tuple.concat("extra");
}});
This method is equivalent to
transform(Function<T,T> modifier
).
modifier
- Modifier logic to be executed against each tuple.T
modified 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 transforming 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.
// Java 8 - Using lambda expression
TStream<String> lines = ...
TStream<String> words = lines.multiTransform(
line -> Arrays.asList(line.split(" ")));
// Java 7 - Using anonymous class
TStream<String> lines = ...
TStream<String> words = lines.multiTransform(new Function>() {
@Override
public Iterable apply(String line) {
return Arrays.asList(line.split(" "));
}});
mapper
- Mapper logic to be executed against each tuple.U
mapped from this
stream's tuples.<U> TStream<U> multiTransform(Function<T,java.lang.Iterable<U>> transformer)
U
.
This function is equivalent to flatMap(Function)
.
transformer
- Mapper logic to be executed against each tuple.U
mapped from this
stream's tuples.TSink forEach(Consumer<T> action)
t
on this stream
action.accept(t)
will be called. This is
typically used to send information to external systems, such as databases
or dashboards.
Example of terminating a stream of String
tuples by printing them
to System.out
.
TStream<String> values = ...
values.forEach(new Consumer() {
@Override
public void accept(String tuple) {
System.out.println(tuple);
}
});
action
- Action to be executed against each tuple on this stream.TSink sink(Consumer<T> sinker)
This function is equivalent to forEach(Consumer)
.
sinker
- Action to be executed against each tuple on this stream.TStream<T> union(TStream<T> other)
other
stream
of the same type T
. Any tuple on this stream or other
will appear on the returned stream. other
is defined,
thus the return stream is unordered.
other
is this stream or keyed version of this stream
then this
is returned as a stream cannot be unioned with itself.other
- Stream to union with this stream.other
.TStream<T> union(java.util.Set<TStream<T>> others)
others
streams
of the same type T
. Any tuple on this stream or any of
others
will appear on the returned stream. others
is defined,
thus the return stream is unordered. this
is returned.
others
or this stream will be reduced to a single copy of itself.
this
is returned (for example, others
is empty or only contains the same logical stream as this
.others
- Streams to union with this stream.others
.TSink print()
System.out
. For each tuple t
on this
stream System.out.println(t.toString())
will be called.java.lang.Class<T> getTupleClass()
getTupleType()
if it is a Class
object.null
if getTupleType()
is not a Class
object.java.lang.reflect.Type getTupleType()
null
if no type knowledge could be determined<J,U> TStream<J> join(TWindow<U,?> window, BiFunction<T,java.util.List<U>,J> joiner)
U
. For each tuple on this
stream, it is joined with the contents of window
. Each tuple is
passed into joiner
and the return value is submitted to the
returned stream. If call returns null then no tuple is submitted.joiner
- Join function.window
.<J,U,K> TStream<J> join(Function<T,K> keyer, TWindow<U,K> window, BiFunction<T,java.util.List<U>,J> joiner)
U
with key type K
.
For each tuple on this stream, it is joined with the contents of window
for the key keyer.apply(tuple)
. Each tuple is
passed into joiner
and the return value is submitted to the
returned stream. If call returns null then no tuple is submitted.keyer
- Key function for this stream to match the window's key.window
- Keyed window to join this stream with.joiner
- Join function.window
.<J,U,K> TStream<J> joinLast(Function<? super T,? extends K> keyer, TStream<U> lastStream, Function<? super U,? extends K> lastStreamKeyer, BiFunction<T,U,J> joiner)
U
with partitioning.
For each tuple on this
stream, it is joined with the last tuple seen on lastStream
with a matching key (of type K
).
t
on this stream will match the last tuple
u
on lastStream
if
keyer.apply(t).equals(lastStreamKeyer.apply(u))
is true.
equals
and
hashCode()
.
Each tuple is
passed into joiner
and the return value is submitted to the
returned stream. If call returns null then no tuple is submitted.
keyer
- Key function for this streamlastStream
- Stream to join with.lastStreamKeyer
- Key function for lastStream
joiner
- Join function.lastStream
.<J,U> TStream<J> joinLast(TStream<U> lastStream, BiFunction<T,U,J> joiner)
U
.
For each tuple on this
stream, it is joined with the last tuple seen on lastStream
. Each tuple is
passed into joiner
and the return value is submitted to the
returned stream. If call returns null then no tuple is submitted.
join(TWindow, BiFunction)
where instead the window contents are passed as a single tuple of type U
rather than a list containing one tuple. If no tuple has been seen on lastStream
then null
will be passed as the second argument to joiner
.lastStream
- Stream to join with.joiner
- Join function.lastStream
.TWindow<T,java.lang.Object> last(long time, java.util.concurrent.TimeUnit unit)
TWindow
that continually represents the last time
seconds
of tuples (in the given time unit
) on this stream.
If no tuples have been seen on the stream in the last time
seconds
then the window will be empty.
time
seconds of tuples seen on this stream
TWindow.key(Function)
or TWindow.key()
.
When the window is partitioned each partition independently maintains the last time
seconds of tuples for each key seen on this stream.time
- Time size of the windowunit
- Unit for time
time
seconds.TWindow<T,java.lang.Object> lastSeconds(Supplier<java.lang.Integer> time)
TWindow
that continually represents the last time
seconds
of tuples on this stream.
Same as last(long,TimeUnit)
except the time
is
specified with a Supplier<Integer>
such as one created
by Topology.createSubmissionParameter(String, Class)
.time
- Time size of the window in secondstime
seconds.TWindow<T,java.lang.Object> last(Supplier<java.lang.Integer> count)
TWindow
that continually represents the last count
tuples
seen on this stream.
Same as last(int)
except the count
is
specified with a Supplier<Integer>
such as one created
by Topology.createSubmissionParameter(String, Class)
.count
- Tuple size of the windowcount
tuples.TWindow<T,java.lang.Object> last(int count)
TWindow
that continually represents the last count
tuples
seen on this stream. If the stream has not yet seen count
tuples then it will contain all of the tuples seen on the stream,
which will be less than count
. If no tuples have been
seen on the stream then the window will be empty.
count
tuples seen on this stream.
TWindow.key(Function)
or TWindow.key()
.
When the window is partitioned each partition independently maintains the
last count
tuples for each key seen on this stream.count
- Tuple size of the windowcount
tuples.TWindow<T,java.lang.Object> last()
TWindow
that continually represents the last tuple on this stream.
If no tuples have been seen on the stream then the window will be empty.
TWindow.key(Function)
or TWindow.key()
.
When the window is partitioned each partition independently maintains the
last tuple for each key seen on this stream.TWindow<T,java.lang.Object> window(TWindow<?,?> configWindow)
TWindow
on this stream that has the same configuration
as another window.
TWindow.key(Function)
or TWindow.key()
.configWindow
- Window to copy the configuration from.configWindow
.void publish(java.lang.String topic)
Topology.subscribe(String, Class)
for Java Streams applications.com.ibm.streamsx.topology.topic::Subscribe
operator for SPL
Streams applications.com.ibm.streamsx.topology.topic::FilteredSubscribe
operator for SPL
Streams applications subscribing to a subset of the published tuples.TStream<JSONObject>
) the subscription is to
a JSON stream.
TStream<T>
) the declared Java type (T
) of the stream is an exact match.SPL streams
the SPL
schema
is an exact match.publish(topic, false)
.
A topic name: