Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. public class WindowWordCount {
  2.  
  3.     public static void main(String[] args) throws Exception {
  4.  
  5.         StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
  6.  
  7.         DataStream<Tuple2<String, Integer>> dataStream = env
  8.                 .socketTextStream("localhost", 9999)
  9.                 .flatMap(new Splitter())
  10.                 .keyBy(0)
  11.                 .timeWindow(Time.seconds(5))
  12.                 .sum(1);
  13.  
  14.         dataStream.print();
  15.  
  16.         env.execute("Window WordCount");
  17.     }
  18.  
  19.     public static class Splitter implements FlatMapFunction<String, Tuple2<String, Integer>> {
  20.         @Override
  21.         public void flatMap(String sentence, Collector<Tuple2<String, Integer>> out) throws Exception {
  22.             for (String word: sentence.split(" ")) {
  23.                 out.collect(new Tuple2<String, Integer>(word, 1));
  24.             }
  25.         }
  26.     }
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement