Generic/Template Programming in Flink

SourceFunction<T>

@Public
public interface SourceFunction<T> extends Function, Serializable { void run(SourceContext<T> ctx) throws Exception; void cancel(); @Public // Interface might be extended in the future with additional methods.
interface SourceContext<T> {
void collect(T element); @PublicEvolving
void collectWithTimestamp(T element, long timestamp); @PublicEvolving
void emitWatermark(Watermark mark); @PublicEvolving
void markAsTemporarilyIdle();
Object getCheckpointLock();
void close();
}
}
@Override
public void run(SourceContext<Integer> ctx) throws Exception {
while ((start < counter || counter == -1) && isRunning) {
synchronized (ctx.getCheckpointLock()) {
ctx.collect(start);
++start; // loop back to 0
if (start == Integer.MAX_VALUE) {
start = 0;
}
}
Thread.sleep(10L);
}
}

AsyncFunction<IN, OUT>

@PublicEvolving
public interface AsyncFunction<IN, OUT> extends Function, Serializable {
void asyncInvoke(IN input, ResultFuture<OUT> resultFuture) throws Exception;
default void timeout(IN input, ResultFuture<OUT> resultFuture) throws Exception {
resultFuture.completeExceptionally(
new TimeoutException("Async function call has timed out."));
}
}
@Override
public void asyncInvoke(final Integer input, final ResultFuture<String> resultFuture) {
executorService.submit(() -> {
// wait for while to simulate async operation here
long sleep = (long) (ThreadLocalRandom.current().nextFloat() * sleepFactor);
try {
Thread.sleep(sleep); if (ThreadLocalRandom.current().nextFloat() < failRatio) {
resultFuture.completeExceptionally(new Exception("wahahahaha..."));
} else {
resultFuture.complete(
Collections.singletonList("key-" + (input % 10)));
}
} catch (InterruptedException e) {
resultFuture.complete(new ArrayList<>(0));
}
});
}
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);
}
FlatMapFunction<T, O>
@Public
@FunctionalInterface
public interface FlatMapFunction<T, O> extends Function, Serializable {
void flatMap(T value, Collector<O> out) throws Exception;
}
FlatMapFunction<String, Tuple2<String, Integer>>() {
private static final long serialVersionUID = -938116068682344455L; @Override
public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
out.collect(new Tuple2<>(value, 1));
}
}

Generic/Template Programming in Flink的更多相关文章

  1. Flink -- Java Generics Programming

    Flink uses a lot of generics programming, which is an executor Framework with cluster of executor ha ...

  2. Important Programming Concepts (Even on Embedded Systems) Part V: State Machines

    Earlier articles in this series: Part I: Idempotence Part II: Immutability Part III: Volatility Part ...

  3. Flink Program Guide (2) -- 综述 (DataStream API编程指导 -- For Java)

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  4. Flink官网文档翻译

    http://ifeve.com/flink-quick-start/ http://vinoyang.com/2016/05/02/flink-concepts/ http://wuchong.me ...

  5. C++ template —— 模板特化(五)

    本篇讲解模板特化-------------------------------------------------------------------------------------------- ...

  6. Asynchronous programming with Tornado

    Asynchronous programming can be tricky for beginners, therefore I think it’s useful to iron some bas ...

  7. Google C++ Style Guide

    Background C++ is one of the main development languages used by many of Google's open-source project ...

  8. (C/C++) Interview in English - Basic concepts.

    Question Key words Anwser A assignment operator abstract class It is a class that has one or more pu ...

  9. CGAL Manual/tutorial_hello_world.html

    Hello World Author CGAL Editorial Board 本教程是为知道C++和几何算法的基本知识的CGAL新手准备的.第一节展示了如何特化点和段CGAL类,以及如何应用几何谓词 ...

随机推荐

  1. kaggle kernel使用指南

    有几个要注意的地方: 1.可以选择CPU或GPU,但是机器学习模型一般的CPU就够了,最近(2019.04)使用GPU的话一小时后总是会断开连接,这时候要跑久一点的cell就凉了. 2.导入文件:支持 ...

  2. python(unittest)报告导出(一):使用HTMLTestRunner导出

    (前提:HTMLTestRunner.py放在python安装目录的Lib文件夹下) 一般将HTMLTestRunner.py文件放入需要引用的目录下,但这个太过于局限,仅对当前项目有用,所以可以将H ...

  3. Java非静态代码块和静态代码块

    类中存在两种特殊的代码块,即非静态代码块和静态代码块,前者是直接由 { } 括起来的代码,而后者是由 static{ } 括起来的代码. 非静态代码块在类初始化创建实例时,将会被提取到类的构造器中执行 ...

  4. [原创]markdown语法学习(commonmark)

    CommonMark是markdown的解析器,以下是部分学习链接: commonmark规范 https://spec.commonmark.org/ commonmark帮助 https://co ...

  5. 233 Matrix(矩阵快速幂+思维)

    In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233 ...

  6. 红米手机3S 3X简单卡刷开发版获得ROOT权限的方法

    小米的机器不同手机型号一般小米论坛都提供两个不同的系统,即分别是稳定版和开发版,稳定版没有提供root权限管理,开发版中就支持了root权限,很多情况下我们需要使用的一些功能强大的APP,都需要在ro ...

  7. 配置idea解决乱码

    在项目开发过程中,我们一般希望在修改完代码之后不重启项目即可提现出修改的结果,那么热部署项目就显得十分必要了.在idea中将项目热部署至tomcat中的方法如下: 首先打开tomcat配置界面,在se ...

  8. vue相关问题在工作中的问题及ui组件及html样式搭建相关网站下载资源

    https://youzan.github.io/vant/#/zh-CN/nav-bar http://www.builive.com/docs/api/index.html    bui框架BUI ...

  9. CommandBehavior.CloseConnection有何作用

    其用在ExecuteReader(c)中,返回对象前不能关闭数据库连接,须用CommandBehavior.CloseConnection: 这是一个关于实际知识点的问题,面试官考查的是应聘者数据库访 ...

  10. JS原型与原型链(好文看三遍)

    一. 普通对象与函数对象 JavaScript 中,万物皆对象!但对象也是有区别的.分为普通对象和函数对象,Object ,Function 是JS自带的函数对象. 下面举例说明: function ...