Handle Write Failures¶
A ParquetFileWriter publishes its file on close(). When writing fails part way, the file should not be published.
A Write Throws¶
No extra code is needed. When ColumnWriter.writeBatch, RowWriter.writeRow or ParquetFileWriter.endRowGroup throws, the writer rejects further writes, and close() discards the output. This holds for every exception a write call throws, including one thrown by the filler.
try (ParquetFileWriter writer = ParquetFileWriter.create(out, schema)) {
ColumnWriter columns = writer.columnWriter();
for (int[] chunk : chunks) {
columns.writeBatch(batch -> batch.ints("id", chunk)); // throws: nothing is published
}
}
A writer whose write call has thrown cannot be written to again. To skip a rejected record and carry on with the next, write it with RowWriter.tryWriteRow; see Skipping a Rejected Record. ColumnWriter.writeBatch has no skip: a rejected batch fails the writer, and close() discards the whole file.
The Data Source Throws¶
The writer does not see an exception raised by the code producing the data outside a filler, so close() would publish the rows written so far. Call abort() before the exception leaves the block:
try (ParquetFileWriter writer = ParquetFileWriter.create(out, schema)) {
try {
ColumnWriter columns = writer.columnWriter();
while (source.hasNext()) {
int[] chunk = source.next(); // may throw
columns.writeBatch(batch -> batch.ints("id", chunk));
}
}
catch (Exception e) {
try {
writer.abort();
}
catch (IOException discardFailure) {
e.addSuppressed(discardFailure);
}
throw e;
}
}
abort() discards the output and closes the writer, so the close() at the end of the block does nothing. Call abort() the same way to abandon a file for any other reason.
The Output Cannot Be Discarded¶
When the output cannot be released, close() or abort() throws the IOException. Adding it to the original exception with addSuppressed, as above, keeps both. On a local file, the temporary sibling of the target path (<name>.hardwood-tmp) is left behind only when it cannot be deleted.
The complete rules are in the Writer Reference.