flink-java实现Wordcount(实时)
public class WordCount { public static void main(String[] args) throws Exception { //1.创建执行环境 StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); //2.创建DStream DataStreamSource<String> lines = env.socketTextStream("linux01", 8888); //3.调用Transformation方法 SingleOutputStreamOperator<String> words = lines.flatMap(new FlatMapFunction<String, String>() { @Override public void flatMap(String line, Collector<String> collector) throws Exception { String[] words = line.split(" "); for (String word:words){ collector.collect(word); } } }); SingleOutputStreamOperator<Tuple2<String, Integer>> wordAndOne = words.map(new MapFunction<String, Tuple2<String, Integer>>() { @Override public Tuple2<String, Integer> map(String word) throws Exception { return Tuple2.of(word, 1); } }); KeyedStream<Tuple2<String, Integer>, String> keyed = wordAndOne.keyBy(new KeySelector<Tuple2<String, Integer>, String>() { @Override public String getKey(Tuple2<String, Integer> stringIntegerTuple2) throws Exception { return stringIntegerTuple2.f0; } }); SingleOutputStreamOperator<Tuple2<String, Integer>> sum = keyed.sum(1); //4调用Sink sum.print(); //启动 env.execute(); } }
flink-java实现Wordcount(离线)
public class BatchWordCount { public static void main(String[] args) throws Exception { //创建离线的ExecutionEnvironment final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); //指定Source DataSet<String> lines = env.readTextFile("src/data/"); //对数据切分压平 DataSet<Tuple2<String, Integer>> result = lines .flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() { @Override public void flatMap(String line, Collector<Tuple2<String, Integer>> collector) throws Exception { //使用分隔符切分 String[] words = line.split(" "); //循环遍历切分后的数组 for (String word : words) { //将单词使用collector收集 collector.collect(Tuple2.of(word, 1)); } } }) .groupBy(0) //分组 .sum(1); //聚合 //保存结果 result.writeAsText("./out"); //执行 env.execute(); } }