Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class WindowWordCount {
- public static void main(String[] args) throws Exception {
- StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
- DataStream<Tuple2<String, Integer>> dataStream = env
- .socketTextStream("localhost", 9999)
- .flatMap(new Splitter())
- .keyBy(0)
- .timeWindow(Time.seconds(5))
- .sum(1);
- dataStream.print();
- env.execute("Window WordCount");
- }
- public static class Splitter implements FlatMapFunction<String, Tuple2<String, Integer>> {
- @Override
- public void flatMap(String sentence, Collector<Tuple2<String, Integer>> out) throws Exception {
- for (String word: sentence.split(" ")) {
- out.collect(new Tuple2<String, Integer>(word, 1));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement