龙空技术网

flink基础入门 彻底搞清楚flatmap算子并应用

94版三国演义 62

前言:

此时大家对“flat function”大致比较着重,兄弟们都需要学习一些“flat function”的相关知识。那么小编也在网摘上搜集了一些有关“flat function””的相关知识,希望各位老铁们能喜欢,我们快快来学习一下吧!

flatmap与map算子的区别是什么?

比如一颗芹菜,map的操作就是修改枝叶,改变造型,但是最后还是一颗芹菜

flatmap的操作,可能就是芹菜叶和芹菜杆分离,甚至还有的根据叶子大小分,一颗芹菜得到很多的东西;

从数量上来说,map就是一比一的输出;

flatmap就是一比多的输出;

来个demo把

package test;import org.apache.flink.api.common.functions.FlatMapFunction;import org.apache.flink.streaming.api.datastream.DataStreamSource;import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;import org.apache.flink.util.Collector;public class TransformflatMapTest {    /**     * flatMap算子的引用     * @param args     */    public static void main(String[] args) throws Exception{        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();        env.setParallelism(1);        DataStreamSource<Event> dataStreamSource = env.fromElements(                new Event("nacy", "/home", 1000),                new Event("bob", "/home", 2000),                new Event("marry", "/chart", 3000),                new Event("tom", "/index", 4000)        );        //2.过滤掉ts<-=2000的数据        dataStreamSource.flatMap(new FlatMapFunction<Event, Object>() {            @Override            public void flatMap(Event event, Collector<Object> collector) throws Exception {                collector.collect(event.name);                collector.collect(event.url);            }        }).print("flatMap-->");        //3、lamber表达式,与2效果一样//        dataStreamSource.flatMap((Event event, Collector<String> out)->{//            out.collect(event.name);//            out.collect(event.url);//        }).returns(new TypeHint<String>() {}).print();        env.execute();    }}

POJO类

package test;public class Event {    public String name;    public String url;    public long ts;    public Event(){}    @Override    public String toString() {        return "Event{" +                "name='" + name + '\'' +                ", url='" + url + '\'' +                ", ts=" + ts +                '}';    }    public Event(String name, String url, long ts) {        this.name = name;        this.url = url;        this.ts = ts;    }}
输出结果

标签: #flat function