Generic/Template Programming in Flink
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的更多相关文章
- Flink -- Java Generics Programming
Flink uses a lot of generics programming, which is an executor Framework with cluster of executor ha ...
- 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 ...
- Flink Program Guide (2) -- 综述 (DataStream API编程指导 -- For Java)
v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...
- Flink官网文档翻译
http://ifeve.com/flink-quick-start/ http://vinoyang.com/2016/05/02/flink-concepts/ http://wuchong.me ...
- C++ template —— 模板特化(五)
本篇讲解模板特化-------------------------------------------------------------------------------------------- ...
- Asynchronous programming with Tornado
Asynchronous programming can be tricky for beginners, therefore I think it’s useful to iron some bas ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- (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 ...
- CGAL Manual/tutorial_hello_world.html
Hello World Author CGAL Editorial Board 本教程是为知道C++和几何算法的基本知识的CGAL新手准备的.第一节展示了如何特化点和段CGAL类,以及如何应用几何谓词 ...
随机推荐
- django中多个app放入同一文件apps
新建一个apps文件夹 需要整理的app文件夹拖到同一个文件夹中,即apps.(弹出对话框,取消勾选Search for references) 在pycharm中,右键apps文件夹--选择mark ...
- PHP单表操作mysqli数据库类的封装
class DB{ private $options=array( 'database_type' => 'mysql', 'database_name' => 'test', 'serv ...
- 基础篇:3)规范化:3d制图总章
本章目的:明确3d绘图也有相应的准则,遵守者方有相应的进阶之路. 1.建模目标:拥有自己的建模思想 学习完成3d制图,最直接的评价标准就是--拥有自己的建模思想. 其表现为: 1)建模思路明确,能独立 ...
- 利用Python实现倒序任意整数
这是很早以前学习C时候做过的一个练习题,题目的要求大概是把用户输入的三位数倒序输出,比如说用户输入123,然后程序应该输出的结果是321.如果遇到用户输入100,那么程序应该输出1.然后我给扩展一下, ...
- SQLite3开发接口函数详解
SQLite3是SQLite一个全新的版本,它虽然是在SQLite 2.8.13的代码基础之上开发的,但是使用了和之前的版本不兼容的数据库格式和API. SQLite3是为了满足以下的需求而开发的: ...
- greenplum表的distributed key值查看
greenplum属于分布式的数据库,MPP+Share nothing的体系,查询的效率很快.不过,这是建立在数据分散均匀的基础上的.如果DK值设置不合理的话,完全有可能出现所有数据落在单个节点上的 ...
- Unity GL画折线
新建一个脚本,这个物体得挂在有摄像机组件的物体上才能生效 OnPostRender() 这个函数才会被自动调用(类似生命周期自动调用) 然后就可以代码画线了,原理是openGL的画线 using Un ...
- Spring Service、Dao进行Junit单元测试
pring对Controller.Service.Dao进行Junit单元测试总结 所有用Junit进行单元测试,都需要下面的配置 @RunWith(SpringJUnit4ClassRunner ...
- step1: python & scrapy安装
#首先安装python,这里安装python所需依赖包yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-dev ...
- bzoj 5319: [Jsoi2018]军训列队
Description Solution 最优情况可以是所有人按位置从小到大排序之后依次占到自己 \(K+\) 排名的位置上去 因为每一个休息位置不同,那么一定递增,所以一定存在一个分界点,左边的是往 ...