Spark append mode for partitioned text file fails with SaveMode.Append – IOException File already Exists

Code-

dataDF.write.partitionBy("year", "month", "date").mode(SaveMode.Append).text("s3://data/test2/events/")

Error-

16/07/06 02:15:05 ERROR datasources.DynamicPartitionWriterContainer: Aborting task.
java.io.IOException: File already exists:s3://path/1839dd1ed38a.gz
 at com.amazon.ws.emr.hadoop.fs.s3n.S3NativeFileSystem.create(S3NativeFileSystem.java:614)
 at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:913)
 at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:894)
 at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:791)
 at com.amazon.ws.emr.hadoop.fs.EmrFileSystem.create(EmrFileSystem.java:177)
 at org.apache.hadoop.mapreduce.lib.output.TextOutputFormat.getRecordWriter(TextOutputFormat.java:135)
 at org.apache.spark.sql.execution.datasources.text.TextOutputWriter.<init>(DefaultSource.scala:156)
 at org.apache.spark.sql.execution.datasources.text.TextRelation$$anon$1.newInstance(DefaultSource.scala:125)
 at org.apache.spark.sql.execution.datasources.BaseWriterContainer.newOutputWriter(WriterContainer.scala:129)
 at org.apache.spark.sql.execution.datasources.DynamicPartitionWriterContainer.newOutputWriter$1(WriterContainer.scala:424)
 at org.apache.spark.sql.execution.datasources.DynamicPartitionWriterContainer.writeRows(WriterContainer.scala:356)
 at org.apache.spark.sql.execution.datasources.InsertIntoHadoopFsRelation$$anonfun$run$1$$anonfun$apply$mcV$sp$3.apply(InsertIntoHadoopFsRelation.scala:150)
 at org.apache.spark.sql.execution.datasources.InsertIntoHadoopFsRelation$$anonfun$run$1$$anonfun$apply$mcV$sp$3.apply(InsertIntoHadoopFsRelation.scala:150)
 at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:66)
 at org.apache.spark.scheduler.Task.run(Task.scala:89)
 at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:214)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
 at java.lang.Thread.run(Thread.java:745)
16/07/06 02:15:05 INFO output.DirectFileOutputCommitter: Nothing to clean up on abort since there are no temporary files written
16/07/06 02:15:05 ERROR datasources.DynamicPartitionWriterContainer: Task attempt attempt_201607060215_0004_m_001709_3 aborted.
16/07/06 02:15:05 ERROR executor.Executor: Exception in task 1709.3 in stage 4.0 (TID 12093)
org.apache.spark.SparkException: Task failed while writing rows.
 at org.apache.spark.sql.execution.datasources.DynamicPartitionWriterContainer.writeRows(WriterContainer.scala:414)
 at org.apache.spark.sql.execution.datasources.InsertIntoHadoopFsRelation$$anonfun$run$1$$anonfun$apply$mcV$sp$3.apply(InsertIntoHadoopFsRelation.scala:150)
 at org.apache.spark.sql.execution.datasources.InsertIntoHadoopFsRelation$$anonfun$run$1$$anonfun$apply$mcV$sp$3.apply(InsertIntoHadoopFsRelation.scala:150)
 at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:66)
 at org.apache.spark.scheduler.Task.run(Task.scala:89)
 at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:214)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
 at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.IOException: File already exists:s3://path/a984-1839dd1ed38a.gz
 at com.amazon.ws.emr.hadoop.fs.s3n.S3NativeFileSystem.create(S3NativeFileSystem.java:614)
 at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:913)
 at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:894)
 at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:791)
 at com.amazon.ws.emr.hadoop.fs.EmrFileSystem.create(EmrFileSystem.java:177)
 at org.apache.hadoop.mapreduce.lib.output.TextOutputFormat.getRecordWriter(TextOutputFormat.java:135)
 at org.apache.spark.sql.execution.datasources.text.TextOutputWriter.<init>(DefaultSource.scala:156)
 at org.apache.spark.sql.execution.datasources.text.TextRelation$$anon$1.newInstance(DefaultSource.scala:125)
 at org.apache.spark.sql.execution.datasources.BaseWriterContainer.newOutputWriter(WriterContainer.scala:129)
 at org.apache.spark.sql.execution.datasources.DynamicPartitionWriterContainer.newOutputWriter$1(WriterContainer.scala:424)
 at org.apache.spark.sql.execution.datasources.DynamicPartitionWriterContainer.writeRows(WriterContainer.scala:356)
 ... 8 more

Possible causes and fixes.

  1. Generally there is some other exception hidden like ArithmeticException/UnknownHostException along with this error. I know this is silly but trust me – there should be some small error hidden in the logs. Check Yarn logs.

    yarn logs -applicationId <application_id>
    http://stackoverflow.com/questions/36034928/spark-exception-task-failed-while-writing-rows

  2. Using older version of Parquet Writer. This is only for parquet writer not text writer.

    https://issues.apache.org/jira/browse/SPARK-8413

  3. Wrong exception type thrown from S3 or GS. Spark expects a FileNotFoundException but a more generic exception IOException is thrown from backend system.

    https://groups.google.com/forum/#!topic/cloud-dataproc-discuss/jNP7fkJdD5A

  4. S3 sync problem- Somehow S3 is not able to sync on files and randomly throws this error.

    https://forums.databricks.com/questions/1489/why-do-i-get-javaioioexception-file-already-exists.html

  5. Finally, Spark speculative Execution. For some reason the spark speculation mode writing to S3 is broken. Multiple executors try writing same files and die.

    https://forums.databricks.com/questions/1764/ioexception-writing-parquet-file-with-savemodeappe.html
    Set, spark.speculation to false,
    conf=new SparkConf().set(“spark.speculation“,”false”)

 

Hope these are helpful. If you have any other findings, put them down in comments.

 

Cheers.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *