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 StreamCount {
public static void main(String[] args) throws Exception {
//数据流
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
String path="D:\\onlinetime\\java-d-operate\\feature-utils-hub\\feature-flink-realtime-java\\src\\main\\resources\\hello.txt";
DataStream<String> inputDataString = env.readTextFile(path);
DataStream<Tuple2<String,Integer>> reults = inputDataString.flatMap(new MyFlatMapper())
.keyBy(0)
.sum(1);
reults.print();
env.execute();
}
public static class MyFlatMapper implements FlatMapFunction<String, Tuple2<String,Integer>>{ @Override public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception { String[] words= value.split(" "); for( String word : words){ out.collect(new Tuple2<>(word,1)); } } }
}
hello.txt
hello world
hello you
let is meet you
I am fine
thank you
and you