title: Summing a number from a random number source
body: <p>I'm just starting to learn flink and trying to build a very basic toy example which sums an integer over time and periodically prints the total sum so far</p>
  
  <p>I've created a random number generator source class like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>// RandomNumberSource.java
  public class RandomNumberSource implements SourceFunction&lt;Integer&gt; {
      public volatile boolean isRunning = true;
      private Random rand;
  
      public RandomNumberSource() {
          this.rand = new Random();
      }
  
      @Override
      public void run(SourceContext&lt;Integer&gt; ctx) throws Exception {
          while (isRunning) {
              ctx.collect(rand.nextInt(200));
              Thread.sleep(1000L);
          }
      }
  
      @Override
      public void cancel() {
          this.isRunning = false;
      }
  }
  
  </code></pre>
  
  <p>As you can see, it generates a random number every 1 second</p>
  
  <p>Now how would I go about summing the number that's being generated?</p>
  
  <pre class="lang-java prettyprint-override"><code>// StreamJob.java
  public class StreamingJob {
      public static void main(String[] args) throws Exception {
          // set up the streaming execution environment
          final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
          DataStream&lt;Integer&gt; randomNumber = env.addSource(new RandomNumberSource());
  
          // pseudo code:
          // randomNumber
          //    .window(Time.seconds(5))
          //    .reduce(0, (acc, i) =&gt; acc+i) // (initial value, reducer)
          //    .sum()
  
  
          // execute program
          env.execute("Flink Streaming Random Number Sum Aggregation");
      }
  }
  </code></pre>
  
  <p>I've added pseudo code to explain what I'm trying to do. i.e every 5 seconds, perform a sum of all the numbers and print it out.</p>
  
  <p>I feel like I'm missing something in my approach and might need some guidance on how to do this.</p>