Interface TStream<T>
- Type Parameters:
T- Tuple type, any instance ofTat runtime must be serializable.
- All Superinterfaces:
Placeable<TStream<T>>,TopologyElement
- All Known Subinterfaces:
SPLStream
TStream is a declaration of a continuous sequence of tuples. A
connected topology of streams and functional transformations is built using
Topology. Generic methods on this interface provide the ability to
filter, transform or sink this declared stream using a
function. Utility methods in the
com.teracloud.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.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumEnumeration for routing tuples to parallel channels. -
Method Summary
Modifier and TypeMethodDescriptionReturn a strongly typed reference to this stream.Return a stream matching this stream whose subsequent processing will execute in an autonomous region.com.teracloud.streams.topology.builder.BInputPortconnectTo(com.teracloud.streams.topology.builder.BOperatorInvocation receivingBop, boolean functional, com.teracloud.streams.topology.builder.BInputPort input) Internal method.Return a TStream that is no longer guaranteed to run in the same process as the calling stream.Ends a parallel region by merging the channels into a single stream.Declare a new stream that filters tuples from this stream.<U> TStream<U>Declare a new stream that maps tuples from this stream into one or more (or zero) tuples of a different typeU.Sink (terminate) this stream.Class of the tuples on this stream, if known.Type of the tuples on this stream.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 this stream with a partitioned window of typeUwith key typeK.<J,U> TStream<J> join(TWindow<U, ?> window, BiFunction<T, List<U>, J> joiner) Join this stream with window of typeU.<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 typeUwith 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 typeU.last()Declare aTWindowthat continually represents the last tuple on this stream.last(int count) Declare aTWindowthat continually represents the lastcounttuples seen on this stream.Declare aTWindowthat continually represents the lasttimeseconds of tuples (in the given timeunit) on this stream.Declare aTWindowthat continually represents the lastcounttuples seen on this stream.lastSeconds(Supplier<Integer> time) Declare aTWindowthat continually represents the lasttimeseconds of tuples on this stream.Return a stream that is guaranteed to run in the same process as the calling TStream.<U> TStream<U>Declare a new stream that maps each tuple from this stream into one (or zero) tuple of a different typeU.modify(UnaryOperator<T> modifier) Declare a new stream that modifies each tuple from this stream into one (or zero) tuple of the same typeT.<U> TStream<U>multiTransform(Function<T, Iterable<U>> transformer) Declare a new stream that maps tuples from this stream into one or more (or zero) tuples of a different typeU.com.teracloud.streams.topology.builder.BOutputoutput()Internal method.parallel(int width) Parallelizes the stream into a a fixed number of parallel channels using round-robin distribution.Parallelizes the stream intowidthparallel channels.Parallelizes the stream into a number of parallel channels using key based distribution.parallel(Supplier<Integer> width, TStream.Routing routing) Parallelizes the stream intowidthparallel channels.print()Print each tuple onSystem.out.voidPublish tuples from this stream for consumption by other Streams applications.voidPublish tuples from this stream for consumption by other Streams applications.voidPublish tuples from this stream for consumption by other Streams applications.voidPublish tuples from this stream for consumption by other Streams applications.sample(double fraction) Return a stream that is a random sample of this stream.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.setParallel(Supplier<Integer> width) Sets the current stream as the start of a parallel region.Terminate this stream.split(int n, ToIntFunction<T> splitter) Distribute a stream's tuples amongnstreams as specified by asplitter.Throttle a stream by ensuring any tuple is submitted with leastdelayfrom the previous tuple.<U> TStream<U>Declare a new stream that transforms each tuple from this stream into one (or zero) tuple of a different typeU.Create a stream that is a union of this stream andotherstream of the same typeT.Create a stream that is a union of this stream andothersstreams of the same typeT.Declare aTWindowon this stream that has the same configuration as another window.Methods inherited from interface com.teracloud.streams.topology.context.Placeable
addResourceTags, colocate, getInvocationName, getResourceTags, invocationName, isPlaceable, operatorMethods inherited from interface com.teracloud.streams.topology.TopologyElement
builder, topology
-
Method Details
-
filter
Declare a new stream that filters tuples from this stream. Each tupleton this stream will appear in the returned stream iffilter.test(t)returnstrue. Iffilter.test(t)returnsfalsethen thentwill not appear in the returned stream.Examples of filtering out all empty strings from stream
sof typeString// 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(); }} );- Parameters:
filter- Filtering logic to be executed against each tuple.- Returns:
- Filtered stream
- See Also:
-
split
Distribute a stream's tuples amongnstreams as specified by asplitter.For each tuple on the stream,
splitter.applyAsInt(tuple)is called. The return valuerdetermines 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
TStreamis 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 Nfilter()invocations, each with apredicateto select the tuples for its stream.split()is more efficient. Each tuple is analyzed only once by a singlesplitterinstance 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- Parameters:
n- the number of output streamssplitter- the splitter function- Returns:
- List of
nstreams - Throws:
IllegalArgumentException- ifn <= 0- See Also:
-
transform
Declare a new stream that transforms each tuple from this stream into one (or zero) tuple of a different typeU. For each tupleton this stream, the returned stream will contain a tuple that is the result oftransformer.apply(t)when the return is notnull. Iftransformer.apply(t)returnsnullthen no tuple is submitted to the returned stream fort.Examples of transforming a stream containing numeric values as
Stringobjects into a stream ofDoublevalues.// 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<String, Double>() { @Override public Double apply(String v) { return Double.valueOf(v); }});This function is equivalent to
map(Function).- Parameters:
transformer- Transformation logic to be executed against each tuple.- Returns:
- Stream that will contain tuples of type
Utransformed from this stream's tuples.
-
map
Declare a new stream that maps each tuple from this stream into one (or zero) tuple of a different typeU. For each tupleton this stream, the returned stream will contain a tuple that is the result ofmapper.apply(t)when the return is notnull. Ifmapper.apply(t)returnsnullthen no tuple is submitted to the returned stream fort.Examples of mapping a stream containing numeric values as
Stringobjects into a stream ofDoublevalues.// 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<String, Double>() { @Override public Double apply(String v) { return Double.valueOf(v); }});This function is equivalent to
transform(Function). The typical term in most apis ismap.- Parameters:
mapper- Mapping logic to be executed against each tuple.- Returns:
- Stream that will contain tuples of type
Umapped from this stream's tuples. - Since:
- 1.7
-
modify
Declare a new stream that modifies each tuple from this stream into one (or zero) tuple of the same typeT. For each tupleton this stream, the returned stream will contain a tuple that is the result ofmodifier.apply(t)when the return is notnull. The function may return the same reference as its inputtor a different object of the same type. Ifmodifier.apply(t)returnsnullthen no tuple is submitted to the returned stream fort.Example of modifying a stream
Stringvalues 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).- Parameters:
modifier- Modifier logic to be executed against each tuple.- Returns:
- Stream that will contain tuples of type
Tmodified from this stream's tuples.
-
flatMap
Declare a new stream that maps tuples from this stream into one or more (or zero) tuples of a different typeU. For each tupleton this stream, the returned stream will contain all non-null tuples in theIterator<U>that is the result ofmapper.apply(t). Tuples will be added to the returned stream in the order the iterator returns them.
If the return is null or an empty iterator then no tuples are added to the returned stream for input tuplet.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<String, Iterable>() { @Override public Iterable apply(String line) { return Arrays.asList(line.split(" ")); }}); - Parameters:
mapper- Mapper logic to be executed against each tuple.- Returns:
- Stream that will contain tuples of type
Umapped from this stream's tuples. - Since:
- 1.7
-
multiTransform
Declare a new stream that maps tuples from this stream into one or more (or zero) tuples of a different typeU.This function is equivalent to
flatMap(Function).- Parameters:
transformer- Mapper logic to be executed against each tuple.- Returns:
- Stream that will contain tuples of type
Umapped from this stream's tuples.
-
forEach
Sink (terminate) this stream. For each tupleton this streamaction.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
Stringtuples by printing them toSystem.out.TStream<String> values = ... values.forEach(new Consumer() { @Override public void accept(String tuple) { System.out.println(tuple); } }); - Parameters:
action- Action to be executed against each tuple on this stream.- Returns:
- the sink element
- Since:
- 1.7
-
sink
Terminate this stream.This function is equivalent to
forEach(Consumer).- Parameters:
sinker- Action to be executed against each tuple on this stream.- Returns:
- the sink element
-
union
Create a stream that is a union of this stream andotherstream of the same typeT. Any tuple on this stream orotherwill appear on the returned stream.
No ordering of tuples across this stream andotheris defined, thus the return stream is unordered.
Ifotheris this stream or keyed version of this stream thenthisis returned as a stream cannot be unioned with itself.- Parameters:
other- Stream to union with this stream.- Returns:
- Stream that will contain tuples from this stream and
other.
-
union
Create a stream that is a union of this stream andothersstreams of the same typeT. Any tuple on this stream or any ofotherswill appear on the returned stream.
No ordering of tuples across this stream andothersis defined, thus the return stream is unordered.
If others does not contain any streams thenthisis returned.
A stream or a keyed version of a stream cannot be unioned with itself, so any stream that is represented multiple times inothersor this stream will be reduced to a single copy of itself.
In the case that no stream is to be unioned with this stream thenthisis returned (for example,othersis empty or only contains the same logical stream asthis.- Parameters:
others- Streams to union with this stream.- Returns:
- Stream containing tuples from this stream and
others.
-
print
TSink print()Print each tuple onSystem.out. For each tupleton this streamSystem.out.println(t.toString())will be called. -
getTupleClass
Class of the tuples on this stream, if known. Will be the same asgetTupleType()if it is aClassobject.- Returns:
- Class of the tuple on this stream,
nullifgetTupleType()is not aClassobject.
-
getTupleType
Type getTupleType()Type of the tuples on this stream. Can be null if no type knowledge can be determined.- Returns:
- Type of the tuples on this stream,
nullif no type knowledge could be determined
-
join
Join this stream with window of typeU. For each tuple on this stream, it is joined with the contents ofwindow. Each tuple is passed intojoinerand the return value is submitted to the returned stream. If call returns null then no tuple is submitted.- Parameters:
joiner- Join function.- Returns:
- A stream that is the results of joining this stream with
window.
-
join
Join this stream with a partitioned window of typeUwith key typeK. For each tuple on this stream, it is joined with the contents ofwindowfor the keykeyer.apply(tuple). Each tuple is passed intojoinerand the return value is submitted to the returned stream. If call returns null then no tuple is submitted.- Parameters:
keyer- Key function for this stream to match the window's key.window- Keyed window to join this stream with.joiner- Join function.- Returns:
- A stream that is the results of joining this stream with
window.
-
joinLast
<J,U, TStream<J> joinLastK> (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 typeUwith partitioning. For each tuple on this stream, it is joined with the last tuple seen onlastStreamwith a matching key (of typeK).
Each tupleton this stream will match the last tupleuonlastStreamifkeyer.apply(t).equals(lastStreamKeyer.apply(u))is true.
The assumption is made that the key classes correctly implement the contract forequalsandhashCode().Each tuple is passed into
joinerand the return value is submitted to the returned stream. If call returns null then no tuple is submitted.- Parameters:
keyer- Key function for this streamlastStream- Stream to join with.lastStreamKeyer- Key function forlastStreamjoiner- Join function.- Returns:
- A stream that is the results of joining this stream with
lastStream.
-
joinLast
Join this stream with the last tuple seen on a stream of typeU. For each tuple on this stream, it is joined with the last tuple seen onlastStream. Each tuple is passed intojoinerand the return value is submitted to the returned stream. If call returns null then no tuple is submitted.
This is a simplified version ofjoin(TWindow, BiFunction)where instead the window contents are passed as a single tuple of typeUrather than a list containing one tuple. If no tuple has been seen onlastStreamthennullwill be passed as the second argument tojoiner.- Parameters:
lastStream- Stream to join with.joiner- Join function.- Returns:
- A stream that is the results of joining this stream with
lastStream.
-
last
Declare aTWindowthat continually represents the lasttimeseconds of tuples (in the given timeunit) on this stream. If no tuples have been seen on the stream in the lasttimeseconds then the window will be empty.
The window has a single partition that always contains the lasttimeseconds of tuples seen on this stream
A key based partitioned window can be created from the returned window usingTWindow.key(Function)orTWindow.key(). When the window is partitioned each partition independently maintains the lasttimeseconds of tuples for each key seen on this stream.- Parameters:
time- Time size of the windowunit- Unit fortime- Returns:
- Window on this stream representing the last
timeseconds.
-
lastSeconds
Declare aTWindowthat continually represents the lasttimeseconds of tuples on this stream. Same aslast(long,TimeUnit)except thetimeis specified with aSupplier<Integer>such as one created byTopology.createSubmissionParameter(String, Class).- Parameters:
time- Time size of the window in seconds- Returns:
- Window on this stream representing the last
timeseconds.
-
last
Declare aTWindowthat continually represents the lastcounttuples seen on this stream. Same aslast(int)except thecountis specified with aSupplier<Integer>such as one created byTopology.createSubmissionParameter(String, Class).- Parameters:
count- Tuple size of the window- Returns:
- Window on this stream representing the last
counttuples.
-
last
Declare aTWindowthat continually represents the lastcounttuples seen on this stream. If the stream has not yet seencounttuples then it will contain all of the tuples seen on the stream, which will be less thancount. If no tuples have been seen on the stream then the window will be empty.
The window has a single partition that always contains the lastcounttuples seen on this stream.
The window has a single partition that always contains the last tuple seen on this stream.
A key based partitioned window can be created from the returned window usingTWindow.key(Function)orTWindow.key(). When the window is partitioned each partition independently maintains the lastcounttuples for each key seen on this stream.- Parameters:
count- Tuple size of the window- Returns:
- Window on this stream representing the last
counttuples.
-
last
Declare aTWindowthat continually represents the last tuple on this stream. If no tuples have been seen on the stream then the window will be empty.
The window has a single partition that always contains the last tuple seen on this stream.
A key based partitioned window can be created from the returned window usingTWindow.key(Function)orTWindow.key(). When the window is partitioned each partition independently maintains the last tuple for each key seen on this stream.- Returns:
- Window on this stream representing the last tuple.
-
window
Declare aTWindowon this stream that has the same configuration as another window.
The window has a single partition.
A key based partitioned window can be created from the returned window usingTWindow.key(Function)orTWindow.key().- Parameters:
configWindow- Window to copy the configuration from.- Returns:
- Window on this stream with the same configuration as
configWindow.
-
publish
Publish tuples from this stream for consumption by other Streams applications. Applications consume published streams using:-
Topology.subscribe(String, Class)for Java Streams applications. -
com.teracloud.streams.topology.topic::Subscribeoperator for SPL Streams applications. -
com.teracloud.streams.topology.topic::FilteredSubscribeoperator for SPL Streams applications subscribing to a subset of the published tuples.
A subscriber matches to a publisher if:- The topic name is an exact match, and:
-
For JSON streams (
TStream<JSONObject>) the subscription is to a JSON stream. -
For Java streams (
TStream<T>) the declared Java type (T) of the stream is an exact match. -
For
SPL streamstheSPL schemais an exact match.
This method is identical topublish(topic, false).A topic name:
- must not be zero length
- must not contain the nul character (
-
-