title: Spark 2.0.2 nested K-means in rdds/nested rdds or dataframes or datasets
body: <p>I am trying to run lots of k-means in parallel. I have a room and lots of data for it and i would like to calculate clusters for each room. So i have </p>
  
  <pre><code>roomsSignals[(room:String, signals:List[org.apache.spark.mllib.linalg.Vector]]
  
  roomsSignals.map{l=&gt;
  val data=sc.parallelize(l.signals)
   val clusterCenters=2
   val model = KMeans.train(data, clusterCenters, 5)
      model.clusterCenters.map { r =&gt;r.toJson.toString}.mkString(",")
  
  }.collect.foreach(println)
  </code></pre>
  
  <p>Which gives me the error:</p>
  
  <pre><code>org.apache.spark.SparkException: Job aborted due to stage failure: Task 33 in stage 18.0 failed 4 times, most recent failure: Lost task 33.3 in stage 18.0 (TID 1284, 192.168.181.122):     java.lang.NullPointerException
  at $anonfun$1.apply(&lt;console&gt;:77)
  at $anonfun$1.apply(&lt;console&gt;:76)
  at scala.collection.Iter    ator$$anon$11.next(Iter    ator.scala:409)
  at scala.collection.Iter    ator$class.foreach(Iter    ator.scala:893)
  at scala.collection.AbstractIter    ator.foreach(Iter    ator.scala:1336)
  at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
  at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
  at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
  at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:310)
  at scala.collection.AbstractIter    ator.to(Iter    ator.scala:1336)
  at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:302)
  at scala.collection.AbstractIter    ator.toBuffer(Iter    ator.scala:1336)
  at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:289)
  at scala.collection.AbstractIter    ator.toArray(Iter    ator.scala:1336)
  at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:912)
  at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:912)
  at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1916)
  at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1916)
  at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:70)
  at org.apache.spark.scheduler.Task.run(Task.scala:86)
  at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:274)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  at java.lang.Thread.run(Thread.java:745)
  Driver stacktrace:
  at org.apache.spark.scheduler.DAGScheduler.org$apache$spark$scheduler$DAGScheduler$$failJobAndIndependentStages(DAGScheduler.scala:1454)
  at org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1442)
  at org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1441)
  at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
  </code></pre>