文章23 | 阅读 14968 | 点赞0
package com.gosuncn;
import org.apache.flink.api.common.typeinfo.Types;
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 WordCountStreamingJob {
public static void main(String[] args) throws Exception {
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStream<String> lines = env.socketTextStream("47.52.74.183", 8888);
// DataStream<Tuple2> wordAndOne = lines.flatMap((FlatMapFunction<String, Tuple2>) (line, out) -> {
// for (String word : line.split(" ")) {
// out.collect(Tuple2.of(word, 1));
// }
// }).returns(Types.TUPLE(Types.STRING, Types.INT));
DataStream<Tuple2> wordAndOne = lines.flatMap((String line, Collector<Tuple2> out) -> {
for (String word : line.split(" ")) {
out.collect(Tuple2.of(word, 1));
}
}).returns(Types.TUPLE(Types.STRING, Types.INT));
DataStream<Tuple2> summed = wordAndOne.keyBy(0).sum(1);
summed.print();
env.execute("WordCountStreamingJob");
}
}
内容来源于网络,如有侵权,请联系作者删除!