title: Spark Streaming: How to change the value of external variables in foreachRDD function?
body: <p>the code for testing:</p>
  
  <pre><code>object MaxValue extends Serializable{
      var max = 0
  }
  object Test {
      def main(args: Array[String]): Unit = {
          val sc = new SparkContext
          val ssc = new StreamingContext(sc, Seconds(5))
          val seq = Seq("testData")
          val rdd = ssc.sparkContext.parallelize(seq)
          val inputDStream = new ConstantInputDStream(ssc, rdd)
          inputDStream.foreachRDD(rdd =&gt; { MaxValue.max = 10 })    //I change MaxValue.max value to 10.
          val map = inputDStream.map(a =&gt; MaxValue.max)
          map.print    //Why the result is 0? Why not 10?
          ssc.start
          ssc.awaitTermination
      }
  }
  </code></pre>
  
  <p>In this case, how to change the value of <code>MaxValue.max</code> in <em>foreachRDD()</em>? The result of <code>map.print</code> is 0, why not 10. I want to use <strong><em>RDD.max()</em></strong> in <em>foreachRDD()</em>, so I need change <code>MaxValue.max</code> value in <em>foreachRDD()</em>.</p>
  
  <p>Could you help me? Thank you!</p>