IN
- The type of the input elements.OUT
- The type of the returned elements.@PublicEvolving public interface AsyncFunction<IN,OUT> extends Function, Serializable
For each #asyncInvoke, an async io operation can be triggered, and once it has been done,
the result can be collected by calling AsyncCollector.collect(java.util.Collection<OUT>)
. For each async
operation, its context is stored in the operator immediately after invoking
#asyncInvoke, avoiding blocking for each stream input as long as the internal buffer is not full.
AsyncCollector
can be passed into callbacks or futures to collect the result data.
An error can also be propagate to the async IO operator by
AsyncCollector.collect(Throwable)
.
Callback example usage:
public class HBaseAsyncFunc implements AsyncFunction<String, String> {
public void asyncInvoke(String row, AsyncCollector<String> collector) throws Exception {
HBaseCallback cb = new HBaseCallback(collector);
Get get = new Get(Bytes.toBytes(row));
hbase.asyncGet(get, cb);
}
}
Future example usage:
public class HBaseAsyncFunc implements AsyncFunction<String, String> {
public void asyncInvoke(String row, final AsyncCollector<String> collector) throws Exception {
Get get = new Get(Bytes.toBytes(row));
ListenableFuture<Result> future = hbase.asyncGet(get);
Futures.addCallback(future, new FutureCallback<Result>() {
public void onSuccess(Result result) {
List<String> ret = process(result);
collector.collect(ret);
}
public void onFailure(Throwable thrown) {
collector.collect(thrown);
}
});
}
}
Modifier and Type | Method and Description |
---|---|
void |
asyncInvoke(IN input,
AsyncCollector<OUT> collector)
Trigger async operation for each stream input.
|
void asyncInvoke(IN input, AsyncCollector<OUT> collector) throws Exception
input
- element coming from an upstream taskcollector
- to collect the result dataException
- in case of a user code error. An exception will make the task fail and
trigger fail-over process.Copyright © 2014–2018 The Apache Software Foundation. All rights reserved.