本文将介绍Map,FlatMap,Filter,KeyBy,Reduce和Aggregations的基本用法。
DataStream Transformations基础
Map
定义
Transformation | Description |
---|---|
DataStream → DataStream | Applies a Map transformation on a DataStream . The transformation calls a MapFunction for each element of the DataStream. Each MapFunction call returns exactly one element. The user can also extend RichMapFunction to gain access to other features provided by the RichFunction interface. |
说明
利用map
方法对每个数据进行一个转换,输入数据和输出数据为1对1的关系
样例
代码
public class MapDemo {
public static void main(String[] args) throws Exception {
final StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironment();
env.setParallelism(1);
DataStream<Integer> myInts = env.fromElements(1, 2, 3, 4, 5)
.map(x -> x + 1);
myInts.print("map");
env.execute("Map Demo");
}
}
输出结果
map> 2
map> 3
map> 4
map> 5
map> 6
说明
通过map
将每个元素加1后输出
FlatMap
定义
Transformation | Description |
---|---|
DataStream → DataStream | Applies a FlatMap transformation on a DataStream . The transformation calls a FlatMapFunction for each element of the DataStream. Each FlatMapFunction call can return any number of elements including none. The user can also extend RichFlatMapFunction to gain access to other features provided by the RichFunction interface. |
说明
利用flatMap
方法对每个数据进行一个转换,输入数据和输出数据为1对n的关系,n大于等于0。此方法可以将一行数据拆成多行。
样例
代码
public class FlatMapDemo {
public static void main(String[] args) throws Exception {
final StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironment();
env.setParallelism(1);
DataStream<String> myStr = env.fromElements("1, 2, 3, 4, 5")
.flatMap(new FlatMapFunction<String, String>() {
@Override
public void flatMap(String value, Collector<String> out) throws Exception {
for (String word : value.split(",")) {
out.collect(word.trim());
}
}
});
myStr.print("flatMap");
env.execute("FlatMap Demo");
}
}
输出结果
flatMap> 1
flatMap> 2
flatMap> 3
flatMap> 4
flatMap> 5
说明
通过flatMap
将一行数据按照分隔符分割成多行输出
扫描下方二维码进入语雀查看完整文章

网友评论