title: to save/overwrite machine learning model as single file in spark cluster
body: <p>I have machine learning model using LinearRegression. I have spark clusters of 5 vm. After training the model, I want to save the model so I can use it afterwards simply loading into memory.</p>
  
  <p>I have tried using</p>
  
  <pre><code>model.save("/tmp/model.pkl").
  </code></pre>
  
  <p>When saved like this, it creates the directory named model.pkl in all nodes of the clusters which has file as <code>data/, metadata/, _SUCCESS, ._SUCCESS.crc, _temporary, .. and some more</code></p>
  
  <p>Is there a way to save the model as single file like <code>model.pkl</code>? </p>
  
  <p>Also when I retrain the model using newly available data, I am using <code>model.write().overwrite().save("/tmp/model.pkl")</code> to overwrite the existing models, so new updated model be persisted in filesystem. </p>
  
  <p>But then I get exception as <code>FileAlreadyExistsException</code></p>
  
  <pre><code>An error occurred while calling o94.save.
  : org.apache.hadoop.mapred.FileAlreadyExistsException: Output directory file:/tmp/cat_model.pkl/metadata already exists
      at org.apache.hadoop.mapred.FileOutputFormat.checkOutputSpecs(FileOutputFormat.java:131)
      at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsHadoopDataset$1.apply$mcV$sp(PairRDDFunctions.scala:1119)
      at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsHadoopDataset$1.apply(PairRDDFunctions.scala:1096)
      at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsHadoopDataset$1.apply(PairRDDFunctions.scala:1096)
      at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
      at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
      at org.apache.spark.rdd.RDD.withScope(RDD.scala:362)
      at org.apache.spark.rdd.PairRDDFunctions.saveAsHadoopDataset(PairRDDFunctions.scala:1096)
      at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsHadoopFile$4.apply$mcV$sp(PairRDDFunctions.scala:1070)
      at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsHadoopFile$4.apply(PairRDDFunctions.scala:1035)
      at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsHadoopFile$4.apply(PairRDDFunctions.scala:1035)
      at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
      at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
      at org.apache.spark.rdd.RDD.withScope(RDD.scala:362)
      at org.apache.spark.rdd.PairRDDFunctions.saveAsHadoopFile(PairRDDFunctions.scala:1035)
      at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsHadoopFile$1.apply$mcV$sp(PairRDDFunctions.scala:961)
      at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsHadoopFile$1.apply(PairRDDFunctions.scala:961)
      at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsHadoopFile$1.apply(PairRDDFunctions.scala:961)
      at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
      at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
      at org.apache.spark.rdd.RDD.withScope(RDD.scala:362)
      at org.apache.spark.rdd.PairRDDFunctions.saveAsHadoopFile(PairRDDFunctions.scala:960)
      at org.apache.spark.rdd.RDD$$anonfun$saveAsTextFile$1.apply$mcV$sp(RDD.scala:1489)
      at org.apache.spark.rdd.RDD$$anonfun$saveAsTextFile$1.apply(RDD.scala:1468)
      at org.apache.spark.rdd.RDD$$anonfun$saveAsTextFile$1.apply(RDD.scala:1468)
      at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
      at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
      at org.apache.spark.rdd.RDD.withScope(RDD.scala:362)
      at org.apache.spark.rdd.RDD.saveAsTextFile(RDD.scala:1468)
      at org.apache.spark.ml.util.DefaultParamsWriter$.saveMetadata(ReadWrite.scala:278)
      at org.apache.spark.ml.regression.LinearRegressionModel$LinearRegressionModelWriter.saveImpl(LinearRegression.scala:540)
      at org.apache.spark.ml.util.MLWriter.save(ReadWrite.scala:114)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
      at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
      at py4j.Gateway.invoke(Gateway.java:280)
      at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
      at py4j.commands.CallCommand.execute(CallCommand.java:79)
      at py4j.GatewayConnection.run(GatewayConnection.java:214)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>How can I overwrite the existing model?</p>
  
  <p>I have write permission for directory <code>/tmp</code> on all nodes of the cluster.</p>
  
  <p>When trying to load the model using <code>model.load('/tmp/model.pkl')</code>,
  I am getting error as </p>
  
  <pre><code>An error occurred while calling o94.load.
  : java.lang.UnsupportedOperationException: empty collection
  </code></pre>
  
  <p>It seems, <code>save(path)</code> did not saved model correctly.
  How can I load the saved model correctly.
  What would be right way to save and again load the learning model in spark?</p>