聊聊flink的Async I/O

本文深入解析Flink的异步I/O机制,包括AsyncFunction接口的使用,RichAsyncFunction类的功能,以及AsyncDataStream如何将异步操作应用于数据流。探讨了不同输出模式的影响,如有序和无序模式,并介绍了关键参数如超时时间和并发请求容量的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  // This example implements the asynchronous request and callback with Futures that have the
  
  // interface of Java 8's futures (which is the same one followed by Flink's Future)
  
  /**
  
  * An implementation of the 'AsyncFunction' that sends requests and sets the callback.
  
  */
  
  class AsyncDatabaseRequest extends RichAsyncFunction<String, Tuple2<String, String>> {
  
  /** The database specific client that can issue concurrent requests with callbacks */
  
  private transient DatabaseClient client;
  
  @Override
  
  public void open(Configuration parameters) throws Exception {
  
  client = new DatabaseClient(host, post, credentials);
  
  }
  
  @Override
  
  public void close() throws Exception {
  
  client.close();
  
  }
  
  @Override
  
  public void asyncInvoke(String key, final ResultFuture<Tuple2<String, String>> resultFuture) throws Exception {
  
  // issue the asynchronous request, receive a future for result
  
  final Future<String> result = client.query(key);
  
  // set the callback to be executed once the request by the client is complete
  
  // the callback simply forwards the result to the result future
  
  CompletableFuture.supplyAsync(new Supplier<String>() {
  
  @Override
  
  public String get() {
  
  try {
  
  return result.get();
  
  } catch (InterruptedException | ExecutionException e) {
  
  // Normally handled explicitly.
  
  return null;
  
  }
  
  }
  
  }).thenAccept( (String dbResult) -> {
  
  resultFuture.complete(Collections.singleton(new Tuple2<>(key, dbResult)));
  
  });
  
  }
  
  }
  
  // create the original stream
  
  DataStream<String> stream = ...;
  
  // apply the async I/O transformation
  
  DataStream<Tuple2<String, String>> resultStream =
  
  AsyncDataStream.unorderedWait(stream, new AsyncDatabaseRequest(), 1000, TimeUnit.MILLISECONDS, 100);
  
  本实例展示了flink Async I/O的基本用法,首先是实现AsyncFunction接口,用于编写异步请求逻辑及将结果或异常设置到resultFuture,然后就是使用AsyncDataStream的unorderedWait或orderedWait方法将AsyncFunction作用到DataStream作为transformation;AsyncDataStream的unorderedWait或orderedWait有两个关于async operation的参数,一个是timeout参数用于设置async的超时时间,一个是capacity参数用于指定同一时刻最大允许多少个(并发)async request在执行
  
  AsyncFunction
  
  flink-streaming-java_2.11-1.7.0-sources.jar!/org/apache/flink/streaming/api/functions/async/AsyncFunction.java
  
  /**
  
  * A function to trigger Async I/O operation.
  
  *
  
  * <p>For each #asyncInvoke, an async io operation can be triggered, and once it has been done,
  
  * the result can be collected by calling {@link ResultFuture#complete}. 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.
  
  *
  
  * <p>{@link ResultFuture} can be passed into callbacks or futures to collect the result data.
  
  * An error can also be propagate to the async IO operator by
  
  * {@link ResultFuture#completeExceptionally(Throwable)}.
  
  *
  
  * <p>Callback www.michenggw.com example usage:
  
  *
  
  * <pre>{@code
  
  * public class HBaseAsyncFunc implements AsyncFunction<String, String> {
  
  *
  
  * public void asyncInvoke(String row, ResultFuture<String> result) throws Exception {
  
  * HBaseCallback cb = new HBaseCallback(result);
  
  * Get get = new Get(Bytes.toBytes(row));
  
  * hbase.asyncGet(get, cb);
  
  * }
  
  * }
  
  * }</pre>
  
  *
  
  * <p>Future example www.mcyllpt.com usage:
  
  *
  
  * <pre>{@code
  
  * public class HBaseAsyncFunc implements AsyncFunction<String, String> {
  
  *
  
  * public void asyncInvoke(String row, final ResultFuture<String> result) throws Exception {
  
  * Get get = new Get(Bytes.toBytes(row));
  
  * ListenableFuture<Result>www.gcyl158.com future = hbase.asyncGet(get);
  
  * Futures.addCallback(future, new FutureCallback<Result>() {
  
  * public void onSuccess(Result result) {
  
  * List<String> ret = process(result);
  
  * result.complete(ret);
  
  * }
  
  * public void onFailure(Throwable thrown) {
  
  * result.completeExceptionally(thrown);
  
  * }
  
  * });
  
  * }
  
  * }
  
  * }</pre>
  
  *
  
  * @param <IN> The type of the www.haitianguo.cn input elements.
  
  * @param <OUT> The type of the returned elements.
  
  */
  
  @PublicEvolving
  
  public interface AsyncFunction<IN, OUT> extends Function, Serializable {
  
  /**
  
   * Trigger async operation for each stream input.
  
   *
  
   * @param input element coming from an upstream task
  
   * @param resultFuture to be completed with the result data
  
   * @exception Exception in case of a user code error. An exception will make the task fail and
  
   * trigger fail-over process.
  
   */
  
  void asyncInvoke(IN input, ResultFuture<www.fenghuang1999.com> resultFuture) throws Exception;
  
  /**
  
   * {@link AsyncFunction#asyncInvoke} timeout occurred.
  
   * By default, the result future is exceptionally completed with a timeout exception.
  
   *
  
   * @param input element coming from an upstream task
  
   * @param resultFuture to be completed with the result data
  
   */
  
  default void timeout(IN input, ResultFuture<OUT> resultFuture) throws Exception {
  
  resultFuture.completeExceptionally(
  
  new TimeoutException("Async function call has timed out."));
  
  }
  
  }
  
  AsyncFunction接口继承了Function,它定义了asyncInvoke方法以及一个default的timeout方法;asyncInvoke方法执行异步逻辑,然后通过ResultFuture.complete将结果设置到ResultFuture,如果异常则通过ResultFuture.completeExceptionally(Throwable)来传递到ResultFuture
  
  RichAsyncFunction
  
  flink-streaming-java_2.11-1.7.0-sources.jar!/org/apache/flink/streaming/api/functions/async/RichAsyncFunction.java
  
  @PublicEvolving
  
  public abstract class RichAsyncFunction<IN, OUT> extends AbstractRichFunction implements AsyncFunction<IN, OUT> {
  
  private static final long serialVersionUID = 3858030061138121840L;
  
  @Override
  
  public void setRuntimeContext(RuntimeContext runtimeContext) {
  
  Preconditions.checkNotNull(runtimeContext);
  
  if (runtimeContext instanceof IterationRuntimeContext) {
  
  super.setRuntimeContext(
  
  new RichAsyncFunctionIterationRuntimeContext(
  
  (IterationRuntimeContext) runtimeContext));
  
  } else {
  
  super.setRuntimeContext(new RichAsyncFunctionRuntimeContext(runtimeContext));
  
  }
  
  }
  
  @Override
  
  public abstract void asyncInvoke(IN input, ResultFuture<OUT> resultFuture) throws Exception;
  
  //......
  
  }
  
  RichAsyncFunction继承了AbstractRichFunction,同时声明实现AsyncFunction接口,它不没有实现asyncInvoke,交由子类实现;它覆盖了setRuntimeContext方法,这里使用RichAsyncFunctionRuntimeContext或者RichAsyncFunctionIterationRuntimeContext进行包装
  
  RichAsyncFunctionRuntimeContext
  
  flink-streaming-java_2.11-1.7.0-sources.jar!/org/apache/flink/streaming/api/functions/async/RichAsyncFunction.java
  
  /**
  
   * A wrapper class for async function's {@link RuntimeContext}. The async function runtime
  
   * context only supports basic operations which are thread safe. Consequently, state access,
  
   * accumulators, broadcast variables and the distributed cache are disabled.
  
   */
  
  private static class RichAsyncFunctionRuntimeContext implements RuntimeContext {
  
  private final RuntimeContext runtimeContext;
  
  RichAsyncFunctionRuntimeContext(RuntimeContext context) {
  
  runtimeContext = Preconditions.checkNotNull(context);
  
  }
  
  @Override
  
  public String getTaskName() {
  
  return runtimeContext.getTaskName();
  
  }
  
  @Override
  
  public MetricGroup getMetricGroup() {
  
  return runtimeContext.getMetricGroup();
  
  }
  
  @Override
  
  public int getNumberOfParallelSubtasks() {
  
  return runtimeContext.getNumberOfParallelSubtasks();
  
  }
  
  @Override
  
  public int getMaxNumberOfParallelSubtasks() {
  
  return runtimeContext.getMaxNumberOfParallelSubtasks();
  
  }
  
  @Override
  
  public int getIndexOfThisSubtask() {
  
  return runtimeContext.getIndexOfThisSubtask();
  
  }
  
  @Override
  
  public int getAttemptNumber() {
  
  return runtimeContext.getAttemptNumber();
  
  }
  
  @Override
  
  public String getTaskNameWithSubtasks() {
  
  return runtimeContext.getTaskNameWithSubtasks();
  
  }
  
  @Override
  
  public ExecutionConfig getExecutionConfig() {
  
  return runtimeContext.getExecutionConfig();
  
  }
  
  @Override
  
  public ClassLoader getUserCodeClassLoader() {
  
  return runtimeContext.getUserCodeClassLoader();
  
  }
  
  // -----------------------------------------------------------------------------------
  
  // Unsupported operations
  
  // -----------------------------------------------------------------------------------
  
  @Override
  
  public DistributedCache getDistributedCache() {
  
  throw new UnsupportedOperationException("Distributed cache is not supported in rich async functions.");
  
  }
  
  @Override
  
  public <T> ValueState<T> getState(ValueStateDescriptor<T> stateProperties) {
  
  throw new UnsupportedOperationException("State is not supported in rich async functions.");
  
  }
  
  @Override
  
  public <T> ListState<T> getListState(ListStateDescriptor<T> stateProperties) {
  
  throw new UnsupportedOperationException("State is not supported in rich async functions.");
  
  }
  
  @Override
  
  public <T> ReducingState<T> getReducingState(ReducingStateDescriptor<T> stateProperties) {
  
  throw new UnsupportedOperationException("State is not supported in rich async functions.");
  
  }
  
  @Override
  
  public <IN, ACC, OUT> AggregatingState<IN, OUT> getAggregatingState(AggregatingStateDescriptor<IN, ACC, OUT> stateProperties) {
  
  throw new UnsupportedOperationException("State is not supported in rich async functions.");
  
  }
  
  @Override
  
  public <T, ACC> FoldingState<T, ACC> getFoldingState(FoldingStateDescriptor<T, ACC> stateProperties) {
  
  throw new UnsupportedOperationException("State is not supported in rich async functions.");
  
  }
  
  @Override
  
  public <UK, UV> MapState<UK, UV> getMapState(MapStateDescriptor<UK, UV> stateProperties) {
  
  throw new UnsupportedOperationException("State is not supported in rich async functions.");
  
  }
  
  @Override
  
  public <V, A extends Serializable> void addAccumulator(String name, Accumulator<V, A> accumulator) {
  
  throw new UnsupportedOperationException("Accumulators are not supported in rich async functions.");
  
  }
  
  @Override
  
  public <V, A extends Serializable> Accumulator<V, A> getAccumulator(String name) {
  
  throw new UnsupportedOperationException("Accumulators are not supported in rich async functions.");
  
  }
  
  @Override
  
  public Map<String, Accumulator<?, ?>> getAllAccumulators() {
  
  throw new UnsupportedOperationException("Accumulators are not supported in rich async functions.");
  
  }
  
  @Override
  
  public IntCounter getIntCounter(String name) {
  
  throw new UnsupportedOperationException("Int counters are not supported in rich async functions.");
  
  }
  
  @Override
  
  public LongCounter getLongCounter(String name) {
  
  throw new UnsupportedOperationException("Long counters are not supported in rich async functions.");
  
  }
  
  @Override
  
  public DoubleCounter getDoubleCounter(String name) {
  
  throw new UnsupportedOperationException("Long counters are not supported in rich async functions.");
  
  }
  
  @Override
  
  public Histogram getHistogram(String name) {
  
  throw new UnsupportedOperationException("Histograms are not supported in rich async functions.");
  
  }
  
  @Override
  
  public boolean hasBroadcastVariable(String name) {
  
  throw new UnsupportedOperationException("Broadcast variables are not supported in rich async functions.");
  
  }
  
  @Override
  
  public <RT> List<RT> getBroadcastVariable(String name) {
  
  throw new UnsupportedOperationException("Broadcast variables are not supported in rich async functions.");
  
  }
  
  @Override
  
  public <T, C> C getBroadcastVariableWithInitializer(String name, BroadcastVariableInitializer<T, C> initializer) {
  
  throw new UnsupportedOperationException("Broadcast variables are not supported in rich async functions.");
  
  }
  
  }
  
  RichAsyncFunctionRuntimeContext实现了RuntimeContext接口,它将一些方法代理给RuntimeContext,其余的Unsupported的方法都覆盖抛出UnsupportedOperationException
  
  RichAsyncFunctionIterationRuntimeContext
  
  flink-streaming-java_2.11-1.7.0-sources.jar!/org/apache/flink/streaming/api/functions/async/RichAsyncFunction.java
  
  private static class RichAsyncFunctionIterationRuntimeContext extends RichAsyncFunctionRuntimeContext implements IterationRuntimeContext {
  
  private final IterationRuntimeContext iterationRuntimeContext;
  
  RichAsyncFunctionIterationRuntimeContext(IterationRuntimeContext iterationRuntimeContext) {
  
  super(iterationRuntimeContext);
  
  this.iterationRuntimeContext = Preconditions.checkNotNull(iterationRuntimeContext);
  
  }
  
  @Override
  
  public int getSuperstepNumber() {
  
  return iterationRuntimeContext.getSuperstepNumber();
  
  }
  
  // -----------------------------------------------------------------------------------
  
  // Unsupported operations
  
  // -----------------------------------------------------------------------------------
  
  @Override
  
  public <T extends Aggregator<?>> T getIterationAggregator(String name) {
  
  throw new UnsupportedOperationException("Iteration aggregators are not supported in rich async functions.");
  
  }
  
  @Override
  
  public <T extends Value> T getPreviousIterationAggregate(String name) {
  
  throw new UnsupportedOperationException("Iteration aggregators are not supported in rich async functions.");
  
  }
  
  }
  
  RichAsyncFunctionIterationRuntimeContext继承了RichAsyncFunctionRuntimeContext,实现了IterationRuntimeContext接口,它将getSuperstepNumber方法交由IterationRuntimeContext处理,然后覆盖getIterationAggregator、getPreviousIterationAggregate方法抛出UnsupportedOperationException
  
  AsyncDataStream
  
  flink-streaming-java_2.11-1.7.0-sources.jar!/org/apache/flink/streaming/api/datastream/AsyncDataStream.java
  
  @PublicEvolving
  
  public class AsyncDataStream {
  
  /**
  
   * Output mode for asynchronous operations.
  
   */
  
  public enum OutputMode { ORDERED, UNORDERED }
  
  private static final int DEFAULT_QUEUE_CAPACITY = 100;
  
  private static <IN, OUT> SingleOutputStreamOperator<OUT> addOperator(
  
  DataStream<IN> in,
  
  AsyncFunction<IN, OUT> func,
  
  long timeout,
  
  int bufSize,
  
  OutputMode mode) {
  
  TypeInformation<OUT> outTypeInfo = TypeExtractor.getUnaryOperatorReturnType(
  
  func,
  
  AsyncFunction.class,
  
  0,
  
  1,
  
  new int[]{1, 0},
  
  in.getType(),
  
  Utils.getCallLocationName(),
  
  true);
  
  // create transform
  
  AsyncWaitOperator<IN, OUT> operator = new AsyncWaitOperator<>(
  
  in.getExecutionEnvironment().clean(func),
  
  timeout,
  
  bufSize,
  
  mode);
  
  return in.transform("async wait operator", outTypeInfo, operator);
  
  }
  
  public static <IN, OUT> SingleOutputStreamOperator<OUT> unorderedWait(
  
  DataStream<IN> in,
  
  AsyncFunction<IN, OUT> func,
  
  long timeout,
  
  TimeUnit timeUnit,
  
  int capacity) {
  
  return addOperator(in, func, timeUnit.toMillis(timeout), capacity, OutputMode.UNORDERED);
  
  }
  
  public static <IN, OUT> SingleOutputStreamOperator<OUT> unorderedWait(
  
  DataStream<IN> in,
  
  AsyncFunction<IN, OUT> func,
  
  long timeout,
  
  TimeUnit timeUnit) {
  
  return addOperator(
  
  in,
  
  func,
  
  timeUnit.toMillis(timeout),
  
  DEFAULT_QUEUE_CAPACITY,
  
  OutputMode.UNORDERED);
  
  }
  
  public static <IN, OUT> SingleOutputStreamOperator<OUT> orderedWait(
  
  DataStream<IN> in,
  
  AsyncFunction<IN, OUT> func,
  
  long timeout,
  
  TimeUnit timeUnit,
  
  int capacity) {
  
  return addOperator(in, func, timeUnit.toMillis(timeout), capacity, OutputMode.ORDERED);
  
  }
  
  public static <IN, OUT> SingleOutputStreamOperator<OUT> orderedWait(
  
  DataStream<IN> in,
  
  AsyncFunction<IN, OUT> func,
  
  long timeout,
  
  TimeUnit timeUnit) {
  
  return addOperator(
  
  in,
  
  func,
  
  timeUnit.toMillis(timeout),
  
  DEFAULT_QUEUE_CAPACITY,
  
  OutputMode.ORDERED);
  
  }
  
  }
  
  AsyncDataStream提供了unorderedWait、orderedWait两类方法来将AsyncFunction作用于DataStream
  
  unorderedWait、orderedWait方法有带capacity参数的也有不带capacity参数的,不带capacity参数即默认使用DEFAULT_QUEUE_CAPACITY,即100;这些方法最后都是调用addOperator私有方法来实现,它使用的是AsyncWaitOperator;unorderedWait、orderedWait方法都带了timeout参数,用于指定等待async操作完成的超时时间
  
  AsyncDataStream提供了两种OutputMode,其中UNORDERED是无序的,即一旦async操作完成就emit结果,当使用TimeCharacteristic.ProcessingTime的时候这种模式延迟最低、负载最低;ORDERED是有序的,即按element的输入顺序emit结果,为了保证有序operator需要缓冲数据,因而会造成一定的延迟及负载
  
  小结
  
  flink给外部数据访问提供了Asynchronous I/O的API,用于提升streaming的吞吐量,其基本使用就是定义一个实现AsyncFunction接口的function,然后使用AsyncDataStream的unorderedWait或orderedWait方法将AsyncFunction作用到DataStream作为transformation
  
  AsyncFunction接口继承了Function,它定义了asyncInvoke方法以及一个default的timeout方法;asyncInvoke方法执行异步逻辑,然后通过ResultFuture.complete将结果或异常设置到ResultFuture,如果异常则通过ResultFuture.completeExceptionally(Throwable)来传递到ResultFuture;RichAsyncFunction继承了AbstractRichFunction,同时声明实现AsyncFunction接口,它不没有实现asyncInvoke,交由子类实现;它覆盖了setRuntimeContext方法,这里使用RichAsyncFunctionRuntimeContext或者RichAsyncFunctionIterationRuntimeContext进行包装
  
  AsyncDataStream的unorderedWait或orderedWait有两个关于async operation的参数,一个是timeout参数用于设置async的超时时间,一个是capacity参数用于指定同一时刻最大允许多少个(并发)async request在执行;AsyncDataStream提供了两种OutputMode,其中UNORDERED是无序的,即一旦async操作完成就emit结果,当使用TimeCharacteristic.ProcessingTime的时候这种模式延迟最低、负载最低;ORDERED是有序的,即按element的输入顺序emit结果,为了保证有序operator需要缓冲数据,因而会造成一定的延迟及负载

转载于:https://www.cnblogs.com/qwangxiao/p/10291342.html

要运行 Flink 示例程序 `WordCount.jar` 并测试其流处理功能,需使用 Flink 的流处理 API(DataStream API),并确保程序运行在支持流处理的环境中。以下是实现步骤和注意事项: ### 环境准备 1. **启动 Flink 集群** 如果尚未启动 Flink 集群,可以使用以下命令启动本地集群: ```bash ./bin/start-cluster.sh ``` 这将启动一个 JobManager 和一个 TaskManager,适合本地测试使用。 2. **准备输入数据源** 流处理通常依赖于实时数据源,例如通过 `nc`(NetCat)发送的数据。启动一个监听端口的 NetCat 服务: ```bash nc -l 9009 ``` 这将等待用户输入数据,并将其作为流式输入发送到 Flink 程序。 ### 运行流处理 WordCount 示例 Flink 提供了 `SocketWindowWordCount` 示例程序,用于演示如何通过流处理 API 统计单词出现的次数。运行该程序的命令如下: ```bash ./bin/flink run -c com.flink.example.SocketWindowWordCount ./examples/streaming/SocketWindowWordCount.jar --port 9009 ``` 其中 `-c` 指定主类名,`--port` 表示连接的端口号。程序会从指定端口读取数据流,并进行窗口统计。 ### 示例程序代码 以下是一个简化版的 `SocketWindowWordCount` 示例代码,展示了如何通过 DataStream API 实现流式 WordCount: ```java public class SocketWindowWordCount { public static void main(String[] args) throws Exception { StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); DataStream<String> text = env.socketTextStream("localhost", 9009); DataStream<WordWithCount> windowCounts = text .flatMap((String value, Collector<WordWithCount> out) -> { for (String word : value.split("\\s")) { out.collect(new WordWithCount(word, 1)); } }) .keyBy("word") .timeWindow(Time.seconds(5)) .sum("count"); windowCounts.print().setParallelism(1); env.execute("Socket Window WordCount"); } public static class WordWithCount { public String word; public long count; public WordWithCount() {} public WordWithCount(String word, long count) { this.word = word; this.count = count; } @Override public String toString() { return word + " : " + count; } } } ``` 该代码通过 `socketTextStream` 方法从指定端口读取数据流,然后使用 `flatMap` 将文本拆分为单词流,再通过 `keyBy` 和 `timeWindow` 进行窗口统计,并最终输出单词计数结果[^4]。 ### 提交任务到 Flink 集群 在 Flink 集群启动后,可以通过 Web UI 或命令行提交任务。命令行方式如下: ```bash ./bin/flink run -m localhost:8081 -c com.flink.example.SocketWindowWordCount ./examples/streaming/SocketWindowWordCount.jar --port 9009 ``` 其中 `-m` 指定 Flink 集群的 JobManager 地址,`-c` 指定主类名,`--port` 为数据源端口号。 ### 测试流处理功能 1. **发送测试数据** 在启动 NetCat 后,手动输入一些文本,例如: ``` hello world flink stream processing hello flink ``` 程序将根据设定的窗口大小(如 5 秒)输出单词计数结果。 2. **查看执行结果** Flink 程序的输出将显示在控制台,例如: ``` hello : 2 world : 1 flink : 2 stream : 1 processing : 1 ``` 这表明程序成功统计了窗口内的单词出现次数。 ### 注意事项 - **端口冲突**:确保使用的端口(如 9009)未被其他程序占用。 - **Flink 集群状态**:确保 Flink 集群正常运行,可以通过 Web UI(默认地址为 `http://localhost:8081`)查看任务状态。 - **依赖库**:如果程序依赖外部库(如 Kafka 连接器),需确保这些库已正确添加到 Flink 的 `lib` 目录或通过命令行指定。 通过以上步骤,可以成功运行 Flink 的流处理示例程序,并验证其功能。如果遇到连接问题或任务失败,可检查日志文件(位于 Flink 的 `log` 目录)以获取详细信息[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值