# Flink中Filter算子
ilter 算子用于从输入数据流中过滤出满足特定条件的元素,而那些不满足条件的元素则会被丢弃。
使用:
用户需要提供一个实现 FilterFunction 接口的函数。
原理:
该函数会为每个元素返回一个布尔值。
- 如果为
true,则元素被保留; - 如果为
false,则元素被过滤掉;
# Filter实例代码1
import com.itlaoqi.flink.pojo.Event;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
public class FilterSample {
public static void main(String[] args) throws Exception{
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(1);
DataStreamSource<Event> stream = env.fromElements(
new Event("Mary", "./home", 1000L),
new Event("Bob", "./cart", 2000L)
);
stream.print();
stream.filter(event -> event.getUser().equals("Bob")).map(event -> event.getUser()).print();
env.execute();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19