Fix- Unexpected end of input stream

Have you ever encountered the “ Unexpected end of input stream” error, while running a hive query? Don’t worry, In this article, we’ll Learn why we are getting this error and how to resolve it.

The “ Unexpected end of input stream” is an error encountered when dealing with empty (zero length) compressed files (with the .gz extension) in the hive table directory. Remove or fix the corrupted data should resolve this issue

What is Unexpected End of Input Stream?

In Hive, Whenever you are trying to query a table and you have empty (zero length) compressed files (extension .gz) in your hive table’s directory, you get this error(Unexpected end of the input stream).

You will be seeing the below error message in the console or HS2 server logs

Error: Unexpected end of input stream 
at at
at org.apache.hadoop.util.LineReader.fillBuffer( at org.apache.hadoop.util.LineReader.readCustomLine(
at org.apache.hadoop.util.LineReader.readLine(
at org.apache.hadoop.mapreduce.lib.input.LineRecordReader.nextKeyValue(
at org.apache.pig.builtin.PigStorage.getNext(
at org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.PigRecordReader.nextKeyValue(
at org.apache.hadoop.mapred.MapTask$NewTrackingRecordReader.nextKeyValue(
at org.apache.hadoop.mapreduce.task.MapContextImpl.nextKeyValue(
at at org.apache.hadoop.mapred.MapTask.runNewMapper(
at at org.apache.hadoop.mapred.YarnChild$
at Method) at

How to replicate this issue

We can use the below simple steps to replicate this issue in Hive

Step 1: Create a sample file and compress it

echo "sample file" > sample.txt 

gzip sample.txt

Step 2: Create a table path and add the zero-byte compressed file

hadoop fs -mkdir /tmp/test/ 

hadoop fs -put sample.txt.gz /tmp/test/ 

hadoop fs -chmod 777 /tmp/test

Step 3: Create an external table pointing to the newly created table location

hive> create external table example_table (a string) location '/tmp/test';

Step 4: Create zero byte zip file and upload it to the table path below

touch sample2.txt.gz 

hadoop fs -put sample2.txt.gz /tmp/test

Step5: If you try to run a query on top of the table “example_table” you will see the exact same error message

hive> select * from example_table; 


Failed with exception Unexpected end of input stream Time taken: 1.56 seconds

Other potential causes

  1. Data Corruption: The data part of the external table might be corrupted or incomplete. This could happen due to data ingestion, storage, or transfer issues. Check if the data files are available and not truncated or corrupted. Can be done by using an external tool to validate the files. For example Python- Parq tool
  2. Data Format: Ensure that the data format specified in the table definition matches the actual format of the data files. If the data format is incorrect, it can lead to misinterpretation of the data and result in an EOFException.
  3. SerDe Compatibility: If you’re using a custom SerDe (Serializer/Deserializer) for reading the data, ensure that it is compatible with the data format and version you are working with.
  4. File Integrity: If the table is partitioned, ensure that all partitions have complete and consistent data. An issue with a specific partition could lead to an EOFException when querying that partition.
  5. Data Skew: Data skew, where some partitions or files have significantly more data than others, can lead to unexpected behavior. This might cause premature termination of reading due to uneven data distribution.
  6. HDFS or Network Issues: Connectivity problems or disruptions between Hive and HDFS or any external storage system could result in an EOFException. Make sure your Hadoop cluster and storage systems are functioning properly.
  7. Resource Limitations: In some cases, resource limitations such as insufficient memory or disk space could lead to incomplete reads and subsequently trigger an EOFException.


The resolution for this issue is simple, You just have to follow the below steps 🙂

1. Please check If zero-length file exists.

2. If exists then kindly remove the zero-length files.

3. Run the query

hadoop fs -rm -r  /tmp/test/sample2.txt.gz

hive> select * from example_table;  


sample file

Sharing some general troubleshooting/resolution steps, Which can help us if it is not exactly due to the zero-length file

General troubleshooting steps and resolution to Other potential causes

  1. Data Corruption: The input data might be corrupted. Check if the data source has any inconsistencies or missing parts.
  2. HDFS/S3 Issues: If you are reading data from HDFS or S3, ensure that the files are present and accessible. Also, check if there are any network or connectivity issues with these storage systems.
  3. Serialization/Deserialization: The data being read may have been serialized using a specific format, and deserialization might be failing. Ensure that the serialization and deserialization formats are compatible and correct.
  4. Query Configuration: Sometimes, this error can occur due to misconfiguration in the Hive query or job settings. Check if the query is correctly set up and try tuning the configuration parameters as required.
  5. Data Size: If you are reading very large files, it’s possible that some parts of the file are being truncated during the read operation. Try splitting the data into smaller chunks or increasing the buffer size to handle larger data.
  6. File Permissions: Ensure that the user running the Hive query has the necessary permissions to access and read the data files.
  7. Hive Version Compatibility: In some cases, there might be compatibility issues between different versions of Hive and the data format. Make sure you are using compatible versions.
  8. Check Logs: Examine the Hive server logs and the logs of the storage


Facing the “ Unexpected end of input stream” error in Hive can be daunting, but armed with a deeper understanding of its causes and effective troubleshooting strategies, developers can overcome this hurdle. By ensuring data integrity, handling compressed files with care, and addressing network and configuration issues, you can steer your Hive operations toward success.


Q1: What is a

A is a type of exception in Java programming that occurs when the end of a file or stream is reached unexpectedly, indicating that no more data can be read from the input source.

Q2: What causes a to be thrown?

A is thrown when an attempt is made to read data from an input stream, such as a file or a network connection, but there is no more data left to be read, resulting in the end-of-file condition.

Q3: How can I handle a in my Java program?

To handle a, you should use try-catch blocks to catch the exception and take appropriate actions, such as closing the input stream and handling the situation gracefully to avoid program crashes.

Q4: What are some common scenarios where might occur? can occur when reading files, network streams, or data from sources where the expected amount of data isn’t available. For example, reading data until the end of a file or reading from a socket stream that has been prematurely closed.

Q5: Is there a way to prevent from happening?

Yes, you can prevent by performing proper checks before reading from an input stream. Always verify that there is data available to be read using methods like available() or by checking the return value of read operations. Additionally, using consistent and clear data structures can help prevent unexpected end-of-file situations.

Jerry Richard
Follow me

Was this post helpful?

Thanks for your feedback!

Leave a Comment