热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

java7flatmap_flink学习之七map、fliter、flatmap

看完了Flink的datasource、sink,也就把一头一尾给看完了,从数据流入到数据流出,缺少了中间的处理环节。而flink的大头恰

看完了Flink的datasource、sink,也就把一头一尾给看完了,从数据流入到数据流出,缺少了中间的处理环节。

而flink的大头恰恰是只在这个中间环节,如下图:

}

可以看到:

1、返回的是SingleOutputStreamOperator泛型,这是个基础的类型,好多DataStream的方法都返回它,比如map、flapmap、filter、process等

2、最终是调用transform方法来实现的,看下transfrom的实现:

@PublicEvolving

public SingleOutputStreamOperator transform(String operatorName, TypeInformation outTypeInfo, OneInputStreamOperator operator) {

this.transformation.getOutputType();

OneInputTransformation resultTransform = new OneInputTransformation(this.transformation, operatorName, operator, outTypeInfo, this.environment.getParallelism());

SingleOutputStreamOperator returnStream = new SingleOutputStreamOperator(this.environment, resultTransform);

this.getExecutionEnvironment().addOperator(resultTransform);

return returnStream;

}

额,好像还不如不看,直接看怎么用吧!

@Slf4j

public class KafkaUrlSinkJob {

public static void main(String[] args) throws Exception {

final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

Properties properties = new Properties();

properties.put("bootstrap.servers", "localhost:9092");

properties.put("zookeeper.connect", "localhost:2181");

properties.put("group.id", "metric-group");

properties.put("auto.offset.reset", "latest");

properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

properties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

SingleOutputStreamOperator dataStreamSource = env.addSource(

new FlinkKafkaConsumer010(

"testjin",// topic

new SimpleStringSchema(),

properties

)

).setParallelism(1)

// map操作,转换,从一个数据流转换成另一个数据流,这里是从string-->UrlInfo

.map(string -> JSON.parseObject(string, UrlInfo.class))

}

可以看到,kafka中传递的是String类型,在这里通过map转换后,变SingleOutputStreamOperator 类型,否则就是SingleOutputStreamOperator 。

map方法不允许缺少数据,也就是原来多少条数据,处理后依然是多少条数据,只是用来做转换。

flatmap

flatmap,也就是将嵌套集合转换并平铺成非嵌套集合。看个例子,还是用上面的kafka datasource:

// 构造一个嵌套的数据

SingleOutputStreamOperator> listDataStreaamSource = dataStreamSource

.map(urlInfo -> {

List list = Lists.newArrayList();

list.add(urlInfo);

UrlInfo urlInfo1 = new UrlInfo();

urlInfo1.setUrl(urlInfo.getUrl() + "-copy");

urlInfo1.setHash(DigestUtils.md5Hex(urlInfo1.getUrl()));

list.add(urlInfo1);

return list;

}).returns(new ListTypeInfo(UrlInfo.class));

listDataStreaamSource.addSink(new PrintSinkFunction<>());

说明&#xff1a;

1、注意这里的returns方法&#xff0c;如果不指定&#xff0c;会在运行时报错

/*I think the short description of the error message is quite good, but let me expand it a bit.

In order to execute a program, Flink needs to know the type of the values that are processed because it needs to serialize and deserialize them. Flink&#39;s type system is based on TypeInformation which describes a data type. When you specify a function, Flink tries to infer the return type of that function. In case of the FlatMapFunction of your example the type of the objects that are passed to the Collector.

Unfortunately, some Lambda functions lose this information due to type erasure such that Flink cannot automatically infer the type. Therefore, you have to explicitly declare the return type.

如果直接上面这样转换&#xff0c;因为lambda表达式会丢失部分信息&#xff0c;会报如下异常&#xff1a;

org.apache.flink.api.common.functions.InvalidTypesException: The generic type parameters of &#39;Collector&#39; are missing. In many cases lambda methods don&#39;t provide enough information for automatic type extraction when Java generics are involved. An easy workaround is to use an (anonymous) class instead that implements the &#39;org.apache.flink.api.common.functions.FlatMapFunction&#39; interface. Otherwise the type has to be specified explicitly using type information.

*/

不过由于返回的是一个List,不可能直接用 List.class&#xff0c;没这种写法。而flink则

提供了更多选项&#xff0c;这里使用的是

public SingleOutputStreamOperator returns(TypeInformation typeInfo){}

这个构造函数&#xff0c;而ListTypeInfo则是继承TypeInfomation抽象类的一个List实现。

和上文的KafkaSender一起运行&#xff0c;会有如下结果&#xff1a;

kafkaSender:

2019-01-15 20:21:46.650 [main] INFO org.apache.kafka.common.utils.AppInfoParser - Kafka commitId : e89bffd6b2eff799

2019-01-15 20:21:46.653 [main] INFO myflink.KafkaSender - send msg:{"domain":"so.com","id":0,"url":"http://so.com/1547554906650"}

KafkaUrlSinkJob

[UrlInfo(id&#61;0, url&#61;http://so.com/1547554906650, hash&#61;null), UrlInfo(id&#61;0, url&#61;http://so.com/1547554906650-copy, hash&#61;efb0862d481297743b08126b2cda602e)]

也就是一个UrlInfo 扩展成了 一个List

下面看看怎么使用flatmap

...

SingleOutputStreamOperator flatSource &#61; listDataStreaamSource.flatMap(new FlatMapFunction, UrlInfo>() {

&#64;Override

public void flatMap(List urlInfos, Collector collector) throws Exception {

urlInfos.parallelStream().forEach(urlInfo -> collector.collect(urlInfo));

}

});

flatSource.addSink(new PrintSinkFunction<>());

...

当然可以写成lambda表达式&#xff1a;(注意lambda表达式需要显式指定return type)

SingleOutputStreamOperator flatSource &#61; listDataStreaamSource.flatMap(

(FlatMapFunction, UrlInfo>) (urlInfos, collector) ->

urlInfos.parallelStream().forEach(urlInfo -> collector.collect(urlInfo))).returns(UrlInfo.class);

看看打印出来的结果&#xff1a;

2> [UrlInfo(id&#61;0, url&#61;http://so.com/1547554906650, hash&#61;null), UrlInfo(id&#61;0, url&#61;http://so.com/1547554906650-copy, hash&#61;efb0862d481297743b08126b2cda602e)]

1> [UrlInfo(id&#61;0, url&#61;http://so.com/1547554903640, hash&#61;null), UrlInfo(id&#61;0, url&#61;http://so.com/1547554903640-copy, hash&#61;138f79ecc92744a65b03132959da2f73)]

1> UrlInfo(id&#61;0, url&#61;http://so.com/1547554903640-copy, hash&#61;138f79ecc92744a65b03132959da2f73)

1> UrlInfo(id&#61;0, url&#61;http://so.com/1547554903640, hash&#61;null)

2> UrlInfo(id&#61;0, url&#61;http://so.com/1547554906650, hash&#61;null)

2> UrlInfo(id&#61;0, url&#61;http://so.com/1547554906650-copy, hash&#61;efb0862d481297743b08126b2cda602e)

也就是说&#xff0c;flatmap方法最终返回的是一个collector&#xff0c;而这个collector只有一层&#xff0c;当输入数据有嵌套的情况下&#xff0c;可以将数据平铺处理。

当然&#xff0c;不只是针对嵌套集合&#xff0c;由于flatmap返回的数据条数并不会做限制&#xff0c;也就可以做一些扩展数据处理的情况&#xff0c;如下&#xff1a;

dataStream.flatMap((FlatMapFunction) (value, out) -> {

for (String word : value.split(" ")) {

out.collect(word);

}

});

这里就是将string使用空格切割后&#xff0c;组成一个新的dataStream.

filter

顾名思义&#xff0c;filter用于过滤数据&#xff0c;继续在上面代码的基础上写测试。为了避免干扰&#xff0c;将上面两个dataSourceStream.addSink注释掉&#xff0c;添加以下代码&#xff1a;

// 根据domain字段&#xff0c;过滤数据&#xff0c;只保留BAIDU的domain

SingleOutputStreamOperator filterSource &#61; flatSource.filter(urlInfo -> {

if(StringUtils.equals(UrlInfo.BAIDU,urlInfo.getDomain())){

return true;

}

return false;

});

filterSource.addSink(new PrintSinkFunction<>());

这里排除别的domain数据&#xff0c;只保留BAIDU的数据&#xff0c;运行结果就不贴出来了&#xff0c;验证了filter的效果。



推荐阅读
author-avatar
爱你宝贝胡
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有