public final class SpoutWrapper<OUT> extends RichParallelSourceFunction<OUT> implements StoppableFunction
SpoutWrapper
wraps an IRichSpout
in order to execute it within a Flink Streaming program. It
takes the spout's output tuples and transforms them into Flink tuples of type OUT
(see
SpoutCollector
for supported types).SpoutWrapper
calls the wrapped spout's nextTuple()
method in
an infinite loop.SpoutWrapper
can call nextTuple()
for a finite number of
times and terminate automatically afterwards (for finite input streams). The number of nextTuple()
calls can
be specified as a certain number of invocations or can be undefined. In the undefined case, SpoutWrapper
terminates if no record was emitted to the output collector for the first time during a call to
nextTuple()
.FiniteSpout
interface and numberOfInvocations
is not provided or
is null
, SpoutWrapper
calls nextTuple()
method until
FiniteSpout.reachedEnd()
returns true.SourceFunction.SourceContext<T>
Constructor and Description |
---|
SpoutWrapper(backtype.storm.topology.IRichSpout spout)
Instantiates a new
SpoutWrapper that calls the nextTuple() method of
the given spout in an infinite loop. |
SpoutWrapper(backtype.storm.topology.IRichSpout spout,
Collection<String> rawOutputs)
Instantiates a new
SpoutWrapper that calls the nextTuple() method of
the given spout in an infinite loop. |
SpoutWrapper(backtype.storm.topology.IRichSpout spout,
Collection<String> rawOutputs,
Integer numberOfInvocations)
Instantiates a new
SpoutWrapper that calls the nextTuple() method of
the given spout a finite number of times. |
SpoutWrapper(backtype.storm.topology.IRichSpout spout,
Integer numberOfInvocations)
Instantiates a new
SpoutWrapper that calls the nextTuple() method of
the given spout a finite number of times. |
SpoutWrapper(backtype.storm.topology.IRichSpout spout,
String[] rawOutputs)
Instantiates a new
SpoutWrapper that calls the nextTuple() method of
the given spout in an infinite loop. |
SpoutWrapper(backtype.storm.topology.IRichSpout spout,
String[] rawOutputs,
Integer numberOfInvocations)
Instantiates a new
SpoutWrapper that calls the nextTuple() method of
the given spout a finite number of times. |
SpoutWrapper(backtype.storm.topology.IRichSpout spout,
String name,
Collection<String> rawOutputs,
Integer numberOfInvocations)
Instantiates a new
SpoutWrapper that calls the nextTuple() method of
the given spout a finite number of times. |
Modifier and Type | Method and Description |
---|---|
void |
cancel()
Cancels the source.
|
void |
close()
Tear-down method for the user code.
|
void |
run(SourceFunction.SourceContext<OUT> ctx)
Starts the source.
|
void |
setStormTopology(backtype.storm.generated.StormTopology stormTopology)
Sets the original Storm topology.
|
void |
stop()
Stops the source.
|
getIterationRuntimeContext, getRuntimeContext, open, setRuntimeContext
public SpoutWrapper(backtype.storm.topology.IRichSpout spout) throws IllegalArgumentException
SpoutWrapper
that calls the nextTuple()
method of
the given spout
in an infinite loop. The output type will be one of Tuple0
to
Tuple25
depending on the spout's declared number of attributes.spout
- The spout
to be used.IllegalArgumentException
- If the number of declared output attributes is not with range [0;25].public SpoutWrapper(backtype.storm.topology.IRichSpout spout, Integer numberOfInvocations) throws IllegalArgumentException
SpoutWrapper
that calls the nextTuple()
method of
the given spout
a finite number of times. The output type will be one of Tuple0
to
Tuple25
depending on the spout's declared number of attributes.spout
- The spout
to be used.numberOfInvocations
- The number of calls to ISpout.nextTuple()
. If value is negative, SpoutWrapper
terminates if no tuple was emitted for the first time. If value is null
, finite invocation is
disabled.IllegalArgumentException
- If the number of declared output attributes is not with range [0;25].public SpoutWrapper(backtype.storm.topology.IRichSpout spout, String[] rawOutputs) throws IllegalArgumentException
SpoutWrapper
that calls the nextTuple()
method of
the given spout
in an infinite loop. The output type can be any type if parameter
rawOutput
is true
and the spout's number of declared output tuples is 1. If rawOutput
is
false
the output type will be one of Tuple0
to Tuple25
depending on the spout's declared
number of attributes.spout
- The spout
to be used.rawOutputs
- Contains stream names if a single attribute output stream, should not be of type Tuple1
but be
of a raw type. (Can be null
.)IllegalArgumentException
- If rawOuput
is true
and the number of declared output attributes is not 1 or if
rawOuput
is false
and the number of declared output attributes is not with range
[0;25].public SpoutWrapper(backtype.storm.topology.IRichSpout spout, String[] rawOutputs, Integer numberOfInvocations) throws IllegalArgumentException
SpoutWrapper
that calls the nextTuple()
method of
the given spout
a finite number of times. The output type can be any type if parameter
rawOutput
is true
and the spout's number of declared output tuples is 1. If rawOutput
is
false
the output type will be one of Tuple0
to Tuple25
depending on the spout's declared
number of attributes.spout
- The spout
to be used.rawOutputs
- Contains stream names if a single attribute output stream, should not be of type Tuple1
but be
of a raw type. (Can be null
.)numberOfInvocations
- The number of calls to ISpout.nextTuple()
. If value is negative, SpoutWrapper
terminates if no tuple was emitted for the first time. If value is null
, finite invocation is
disabled.IllegalArgumentException
- If rawOuput
is true
and the number of declared output attributes is not 1 or if
rawOuput
is false
and the number of declared output attributes is not with range
[0;25].public SpoutWrapper(backtype.storm.topology.IRichSpout spout, Collection<String> rawOutputs) throws IllegalArgumentException
SpoutWrapper
that calls the nextTuple()
method of
the given spout
in an infinite loop. The output type can be any type if parameter
rawOutput
is true
and the spout's number of declared output tuples is 1. If rawOutput
is
false
the output type will be one of Tuple0
to Tuple25
depending on the spout's declared
number of attributes.spout
- The spout
to be used.rawOutputs
- Contains stream names if a single attribute output stream, should not be of type Tuple1
but be
of a raw type. (Can be null
.)IllegalArgumentException
- If rawOuput
is true
and the number of declared output attributes is not 1 or if
rawOuput
is false
and the number of declared output attributes is not with range
[0;25].public SpoutWrapper(backtype.storm.topology.IRichSpout spout, Collection<String> rawOutputs, Integer numberOfInvocations) throws IllegalArgumentException
SpoutWrapper
that calls the nextTuple()
method of
the given spout
a finite number of times. The output type can be any type if parameter
rawOutput
is true
and the spout's number of declared output tuples is 1. If rawOutput
is
false
the output type will be one of Tuple0
to Tuple25
depending on the spout's declared
number of attributes.spout
- The spout
to be used.rawOutputs
- Contains stream names if a single attribute output stream, should not be of type Tuple1
but be
of a raw type. (Can be null
.)numberOfInvocations
- The number of calls to ISpout.nextTuple()
. If value is negative, SpoutWrapper
terminates if no tuple was emitted for the first time. If value is null
, finite invocation is
disabled.IllegalArgumentException
- If rawOuput
is true
and the number of declared output attributes is not 1 or if
rawOuput
is false
and the number of declared output attributes is not with range
[0;25].public SpoutWrapper(backtype.storm.topology.IRichSpout spout, String name, Collection<String> rawOutputs, Integer numberOfInvocations) throws IllegalArgumentException
SpoutWrapper
that calls the nextTuple()
method of
the given spout
a finite number of times. The output type can be any type if parameter
rawOutput
is true
and the spout's number of declared output tuples is 1. If rawOutput
is
false
the output type will be one of Tuple0
to Tuple25
depending on the spout's declared
number of attributes.spout
- The spout
to be used.name
- The name of the spout.rawOutputs
- Contains stream names if a single attribute output stream, should not be of type Tuple1
but be
of a raw type. (Can be null
.)numberOfInvocations
- The number of calls to ISpout.nextTuple()
. If value is negative, SpoutWrapper
terminates if no tuple was emitted for the first time. If value is null
, finite invocation is
disabled.IllegalArgumentException
- If rawOuput
is true
and the number of declared output attributes is not 1 or if
rawOuput
is false
and the number of declared output attributes is not with range
[0;25].public void setStormTopology(backtype.storm.generated.StormTopology stormTopology)
stormTopology
- The original Storm topology.public final void run(SourceFunction.SourceContext<OUT> ctx) throws Exception
SourceFunction
SourceFunction.SourceContext
emit
elements.
Sources that implement Checkpointed
must lock on the checkpoint lock (using a synchronized block) before updating internal
state and emitting elements, to make both an atomic operation:
public class ExampleSource<T> implements SourceFunction<T>, Checkpointed<Long> {
private long count = 0L;
private volatile boolean isRunning = true;
public void run(SourceContext<T> ctx) {
while (isRunning && count < 1000) {
synchronized (ctx.getCheckpointLock()) {
ctx.collect(count);
count++;
}
}
}
public void cancel() {
isRunning = false;
}
public Long snapshotState(long checkpointId, long checkpointTimestamp) { return count; }
public void restoreState(Long state) { this.count = state; }
}
run
in interface SourceFunction<OUT>
ctx
- The context to emit elements to and for accessing locks.Exception
public void cancel()
SourceFunction.run(SourceContext)
method. The implementation needs to ensure that the
source will break out of that loop after this method is called.
A typical pattern is to have an "volatile boolean isRunning"
flag that is set to
false
in this method. That flag is checked in the loop condition.
When a source is canceled, the executing thread will also be interrupted
(via Thread.interrupt()
). The interruption happens strictly after this
method has been called, so any interruption handler can rely on the fact that
this method has completed. It is good practice to make any flags altered by
this method "volatile", in order to guarantee the visibility of the effects of
this method to any interruption handler.
Sets the isRunning
flag to false
.
cancel
in interface SourceFunction<OUT>
public void stop()
cancel()
this is a request to the source function to shut down
gracefully. Pending data can still be emitted and it is not required to stop immediately -- however, in the near
future. The job will keep running until all emitted data is processed completely.
Most streaming sources will have a while loop inside the run()
method. You need to ensure that the source
will break out of this loop. This can be achieved by having a volatile field "isRunning" that is checked in the
loop and that is set to false in this method.
The call to stop()
should not block and not throw any exception.
Sets the isRunning
flag to false
.
stop
in interface StoppableFunction
public void close() throws Exception
RichFunction
This method can be used for clean up work.
close
in interface RichFunction
close
in class AbstractRichFunction
Exception
- Implementations may forward exceptions, which are caught by the runtime. When the
runtime catches an exception, it aborts the task and lets the fail-over logic
decide whether to retry the task execution.Copyright © 2014–2017 The Apache Software Foundation. All rights reserved.