diff --git a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetFileWriter.java b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetFileWriter.java index db15ba406a..814b98c50f 100644 --- a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetFileWriter.java +++ b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetFileWriter.java @@ -170,6 +170,7 @@ public static enum Mode { // set when end is called private ParquetMetadata footer = null; + private boolean closed; private final CRC32 crc; private final ReusingByteBufferAllocator crcAllocator; @@ -1658,11 +1659,16 @@ public void end(Map extraMetaData) throws IOException { @Override public void close() throws IOException { + if (closed) { + return; + } try (PositionOutputStream temp = out) { temp.flush(); if (crcAllocator != null) { crcAllocator.close(); } + } finally { + closed = true; } } diff --git a/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileWriter.java b/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileWriter.java index 32e2eac0b9..c6be72ff70 100644 --- a/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileWriter.java +++ b/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileWriter.java @@ -286,6 +286,9 @@ public void testWriteRead() throws Exception { w.endColumn(); w.endBlock(); w.end(new HashMap()); + // Although writer is already closed in previous end(), + // explicitly close it again to verify double close behavior. + w.close(); ParquetMetadata readFooter = ParquetFileReader.readFooter(configuration, path); assertEquals("footer: " + readFooter, 2, readFooter.getBlocks().size());