[flink]#14_算子Operator

DataStream Operator

Map

获取一个元素并生成一个元素

//新的一年给每个员工的工资加 5000。
SingleOutputStreamOperator<Employee> map = employeeStream.map(new MapFunction<Employee, Employee>() {

	@Override 
	public Employee map(Employee employee) throws Exception {

		employee.salary = employee.salary + 5000;

		return employee; 
	}
	}); 
map.print();

FlatMap

获取一个元素并生成零个、一个或多个元素

//将工资大于 40000 的找出来。
SingleOutputStreamOperator<Employee> flatMap = employeeStream.flatMap(new FlatMapFunction<Employee, Employee>() {

	@Override
	public void flatMap(Employee employee, Collector<Employee> out) throws Exception {

		if (employee.salary >= 40000) { out.collect(employee);}
	} 
}); 
flatMap.print();

Filter

对每个元素都进行判断,返回为 true 的元素

//将工资大于 40000 的找出来。
SingleOutputStreamOperator<Employee> filter = employeeStream.filter(new FilterFunction<Employee>() {

	@Override 
	public boolean filter(Employee employee) throws Exception { 
		if (employee.salary >= 40000) { return true; } 
		return false; 
	}

}); 
filter.print();

KeyBy

KeyBy 在逻辑上是基于 key 对流进行分区,相同的 Key 会被分到一个分区(这里分区指的就是下游算 子多个并行节点的其中一个)。在内部,它使用 hash 函数对流进行分区。它返回 KeyedDataStream 数据流。

//根据商品的店铺 id 来进行分区。
KeyedStream<ProductEvent, Integer> keyBy = productStream.keyBy(new KeySelector<ProductEvent, Integer>() {

	@Override
	public Integer getKey(ProductEvent product) throws Exception {

		return product.shopId;
	} 
}); 
keyBy.print();

Reduce

Reduce 返回单个的结果值,并且 reduce 操作每处理一个元素总是创建一个新值。常用的方法有 average、sum、min、max、count,使用 Reduce 方法都可实现。

//上面先将数据流进行 keyby 操作,因为执行 Reduce 操作只能是 KeyedStream,然后将员工的工资做 了一个求平均值的操作。
SingleOutputStreamOperator<Employee> reduce = employeeStream
.keyBy(new KeySelector<Employee, Integer>() {
 	@Override 
 	public Integer getKey(Employee employee) throws Exception { 	
 		return employee.shopId; 
 	} 
 })
.reduce(new ReduceFunction<Employee>() { 
	@Override 
	public Employee reduce(Employee employee1, Employee employee2) throws Exception { 
		employee1.salary = (employee1.salary + employee2.salary) / 2; 
		return employee1; 
	} 
}); 
reduce.print();

Aggregations

KeyedStream.sum(0) 
KeyedStream.sum("key") 
KeyedStream.min(0) 
KeyedStream.min("key") 
KeyedStream.max(0) 
KeyedStream.max("key") 
KeyedStream.minBy(0) 
KeyedStream.minBy("key") 
KeyedStream.maxBy(0) 
KeyedStream.maxBy("key")

Window

允许按时间或其他条件对现有 KeyedStream 进行分组

inputStream.keyBy(0).window(Time.seconds(10));//以 10 秒的时间窗口聚合

WindowAll

将元素按照某种特性聚集在一起,该函数不支持并行操作,默认的并行度就是 1

inputStream.keyBy(0).windowAll(TumblingProcessingTimeWindows.of(Time.seconds(10) ));

Union

将两个或多个数据流结合在一起

inputStream.union(inputStream1, inputStream2, ...);

Window Join

通过一些 key 将同一个 window 的两个数据流 join 起来

//在 5 秒的窗口中连接两个流,其中第一个流的第一个属性的连接条件等于另一个流的第二个 属性
inputStream.join(inputStream1).where(0).equalTo(1) .window(Time.seconds(5)) .apply (new JoinFunction () {...});

Split

将流拆分为两个或多个流

SplitStream<Integer> split = inputStream.split(new OutputSelector<Integer>() { 
	@Override 
	public Iterable<String> select(Integer value) { 
		List<String> output = new ArrayList<String>(); 
		if (value % 2 == 0) { output.add("even"); } 
		else { output.add("odd"); } 
		return output;
	}
});

Select

从拆分流中选择特定流

扫描二维码关注公众号,回复: 8752691 查看本文章
SplitStream<Integer> split;
DataStream<Integer> even = split.select("even");
DataStream<Integer> odd = split.select("odd"); 
DataStream<Integer> all = split.select("even","odd");

DataSet Operator

Map、FlatMap、Filter 等…

First-n

DataSet<Tuple2<String, Integer>> in = 
// 返回 DataSet 中前 5 的元素 
DataSet<Tuple2<String, Integer>> out1 = in.first(5);

// 返回分组后每个组的前 2 元素 
DataSet<Tuple2<String, Integer>> out2 = in.groupBy(0) .first(2);

// 返回分组后每个组的前 3 元素(按照上升排序) 
DataSet<Tuple2<String, Integer>> out3 = in.groupBy(0).sortGroup(1, Order.ASCENDING) .first(3);
发布了78 篇原创文章 · 获赞 0 · 访问量 1412

猜你喜欢

转载自blog.csdn.net/qq_30782921/article/details/103533625