public static void main(String[] args) throws Exception {
//创建执行环境
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//设置并行度
env.setParallelism(1);
//读取数据源
DataStreamSource<Event> dataStream = env.fromElements(
new Event("Bob", "./cart", 1000L),
new Event("Bob", "./cart", 1000L),
new Event("Bob", "./cart/asdf", 1000L));
SingleOutputStreamOperator<Integer> map = dataStream.map(new RichMapFunction<Event, Integer>() {
@Override
public void open(Configuration parameters) throws Exception {
super.open(parameters);
System.out.println("open生命周期被调用: " + getRuntimeContext().getIndexOfThisSubtask() + "号任务启动");
}
@Override
public Integer map(Event event) throws Exception {
return event.url.length();
}
@Override
public void close() throws Exception {
super.close();
System.out.println("close生命周期被调用: " + getRuntimeContext().getIndexOfThisSubtask() + "号任务结束");
}
});
//打印
map.print();
//开始
env.execute();
}
【Flink】富函数
最新推荐文章于 2022-09-08 10:24:12 发布