我是Apache flink的新手,我试图过滤以字母 "N "开头的单词,我得到了输出,但我如何能得到不以单词 "N "开头的单词,下面是我使用的代码。
package DataStream;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;
public class WordStream {
public static void main(String[] args) throws Exception {
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStream<String> inputData = env.socketTextStream("localhost", 9999);
DataStream<String> filterData = inputData.filter(new FilterFunction<String>() {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public boolean filter(String value) throws Exception {
return value.startsWith("N");
}
});
DataStream<Tuple2<String, Integer>> tokenize = filterData
.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
@Override
public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
out.collect(new Tuple2<String, Integer>(value, Integer.valueOf(1)));
}
});
DataStream<Tuple2<String, Integer>> counts = tokenize.keyBy(0).sum(1);
counts.print();
env.execute("WordStream");
}
}
你能建议如何捕获不匹配的单词到另一个流。
我想你可以使用 侧产出 来实现。只需要在实际采集器中发出匹配的元素,用ProcessFunction发出带有侧输出标签的未匹配元素,然后从主流中获取侧输出元素。
举个例子,你的代码可以改成这样。
package datastream;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;
public class WordStream {
public static void main(String[] args) throws Exception {
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStream<String> inputData = env.socketTextStream("localhost", 9999);
// Initialize side-output tag to collect the un-matched elements
OutputTag<Tuple2<String, Integer>> unMatchedSideOutput = new OutputTag<Tuple2<String, Integer>>("unmatched-side-output") {};
SingleOutputStreamOperator<Tuple2<String, Integer>> tokenize = inputData
.process(new ProcessFunction<String, Tuple2<String, Integer>>() {
@Override
public void processElement(String value, Context ctx, Collector<Tuple2<String, Integer>> out) {
if (value.startsWith("N")) {
// Emit the data to actual collector
out.collect(new Tuple2<>("Matched=" + value, Integer.valueOf(1)));
} else {
// Emit the un-matched data to side output
ctx.output(unMatchedSideOutput, new Tuple2<>("UnMatched=" + value, Integer.valueOf(1)));
}
}
});
DataStream<Tuple2<String, Integer>> count = tokenize.keyBy(0).sum(1);
// Fetch the un-matched element using side-output tag and process it
DataStream<Tuple2<String, Integer>> unMatchedCount = tokenize.getSideOutput(unMatchedSideOutput).keyBy(0).sum(1);
count.print();
unMatchedCount.print();
env.execute("WordStream");
}
}
注 我稍微改变了发射值的前缀。Matched=
和 UnMatched=
以便在输出中得到清晰的理解。
对于下面的输入。
Hello
Nevermind
Hello
我得到以下输出。
3> (UnMatched=Hello,1)
4> (Matched=Nevermind,1)
3> (UnMatched=Hello,2)
一个更简单的解决方案。
DataStream<String> nwords = input.filter(s -> startsWith("N"));
DataStream<String> others = input.filter(s -> !startsWith("N"));
我相信这比使用边输出的解决方案效率稍低,但它仍将在一个任务中运行,使用操作者链,所以它也不需要serde开销,或网络。
不要误会我的意思--一般来说,侧输出是分割流的最佳方式。