Data and AI Platforms

Hadoop Snappy compression example with Hive

By
Snappy is a fast compression codec widely used in Hadoop ecosystems. This updated guide shows how to generate data, upload it to HDFS, process it with Hive using Snappy compression, verify the output files, and load Snappy-compressed data back into Hive using modern Hadoop commands.

To use Snappy compression in Hadoop with Hive, turn on compressed output and set the codec to SnappyCodec before you write the table:

SET hive.exec.compress.output=true;
SET mapreduce.output.fileoutputformat.compress.codec=org.apache.hadoop.io.compress.SnappyCodec;
SET mapreduce.output.fileoutputformat.compress.type=BLOCK;

The rest of this post is a full working example. It creates test data, puts it into HDFS, writes a Snappy compressed Hive table, checks the .snappy file and loads it back into Hive.

Snappy is a compression library from Google. It is built for speed rather than the smallest files, which is why it is a common choice in Hadoop and Hive pipelines. It works with Hadoop 2.x and 3.x, and with Hive on MapReduce, Tez or Spark.

1. Create sample input data

Generate a small test file:

$ seq 1 1000 | awk '{OFS="\001";print $1, $1 % 10}' > test_input.hive
$ head -5 test_input.hive
1^A1
2^A2
3^A3
4^A4
5^A5

2. Upload the data into HDFS

Updated command: Hadoop now uses hdfs dfs instead of hadoop dfs.

$ hdfs dfs -mkdir -p /tmp/hivetest
$ hdfs dfs -put test_input.hive /tmp/hivetest

$ hdfs dfs -ls /tmp/hivetest
Found 1 items
-rw-r--r--   3 hdfs supergroup   5893 ... /tmp/hivetest/test_input.hive

3. Create an external Hive table

External tables in Hive reference a directory:

hive> CREATE EXTERNAL TABLE hivetest1 (a INT, b INT)
      ROW FORMAT DELIMITED FIELDS TERMINATED BY '\001'
      LOCATION '/tmp/hivetest';

hive> SELECT * FROM hivetest1 LIMIT 1;
1     1

4. Enable Snappy in Hive

Before writing compressed output, enable Snappy via session settings:

hive> SET hive.exec.compress.output=true;
hive> SET mapreduce.output.fileoutputformat.compress.codec=org.apache.hadoop.io.compress.SnappyCodec;
hive> SET mapreduce.output.fileoutputformat.compress.type=BLOCK;

Note: In Hadoop 2.x and 3.x, the modern property names start with mapreduce.output. instead of mapred.output.

Now create a managed table for compressed output:

hive> CREATE TABLE hivetest2 (a INT, b INT);

5. Insert data into the Snappy-compressed table

Hive will launch a MapReduce (or Tez) job that produces Snappy-compressed output blocks:

hive> INSERT OVERWRITE TABLE hivetest2
      SELECT * FROM hivetest1;

Total jobs = 1
... job output omitted ...
1000 Rows loaded to hivetest2

6. Validate the compressed data file

Verify that Hive created a Snappy file:

$ hdfs dfs -ls /user/hive/warehouse/hivetest2
Found 1 items
-rw-r--r--   3 hdfs supergroup  4021 ... /user/hive/warehouse/hivetest2/000000_0.snappy

Read the Snappy file with -text, which picks the codec from the file extension and decompresses it (-cat would only show compressed binary):

$ hdfs dfs -text /user/hive/warehouse/hivetest2/000000_0.snappy | head -5
1^A1
2^A2
3^A3
4^A4
5^A5

7. Load Snappy data into another Hive table

You can also load existing Snappy files using LOAD DATA:

hive> CREATE TABLE hivetest3 (a INT, b INT);

hive> LOAD DATA INPATH '/user/hive/warehouse/hivetest2/000000_0.snappy'
      INTO TABLE hivetest3;

hive> SELECT * FROM hivetest3 LIMIT 5;
1     1
2     2
3     3
4     4
5     5

Notes for Modern Hadoop Versions

  • Snappy is available in Hadoop 2.x and later, but it is not the default output codec. You have to enable it as shown above.
  • On Hive-on-Tez or Spark, compression is still controlled via SET statements or table properties.
  • Modern Hive uses ORC or Parquet, which compress internally. ORC defaults to ZLIB (set orc.compress=SNAPPY to change it), and Hive writes Parquet uncompressed unless you set parquet.compression=SNAPPY.
  • This example remains valid for teaching raw file-based compression workflows.

Snappy remains a preferred codec due to its balance between speed and reasonable compression ratio. Before Hadoop 3.3.1, the SnappyCodec needed the native Hadoop library with Snappy support, and jobs failed with a "native snappy library not available" error without it (check with hadoop checknative -a). Since Hadoop 3.3.1 the codec uses snappy-java, which bundles the native code, so no separate install is needed.

Is Snappy splittable in Hadoop

Not on its own. SnappyCodec does not implement Hadoop's SplittableCompressionCodec, so a Snappy compressed text file like the 000000_0.snappy above is read whole by a single task. For small files that is fine. For a 10 GB file it means one slow task instead of dozens of parallel ones.

Splitting works when Snappy compresses blocks inside a container format. SequenceFile with block compression, Avro, ORC and Parquet all do this, which is also why compress.type=BLOCK only matters for SequenceFile output. For real Hive tables, store the data as ORC or Parquet and let the format use Snappy inside:

hive> CREATE TABLE hivetest_orc (a INT, b INT)
      STORED AS ORC TBLPROPERTIES ("orc.compress"="SNAPPY");
hive> INSERT OVERWRITE TABLE hivetest_orc SELECT * FROM hivetest1;

hive> CREATE TABLE hivetest_parquet (a INT, b INT)
      STORED AS PARQUET TBLPROPERTIES ("parquet.compression"="SNAPPY");
hive> INSERT OVERWRITE TABLE hivetest_parquet SELECT * FROM hivetest1;

What happens with a broken Snappy file

From my own work, when one task sat on a whole Snappy file for a long time, the file was often broken. Those are two different things, though.

  • One task per file is normal. A plain .snappy file always goes to one task, healthy or not. That is the splitting point above.
  • A cut-off file fails. Hadoop's SnappyCodec writes data in blocks, each with its length in front. If the file ends early, the reader fails with java.io.EOFException: Unexpected end of block in input stream.
  • Corrupt bytes fail too. Older Hadoop with native Snappy throws java.lang.InternalError: Could not decompress data. Input is invalid. Newer Hadoop uses snappy-java instead (HADOOP-17125) and throws an IOException from it.
  • The task does not skip ahead. It fails, and YARN tries it again, up to 4 times by default (mapreduce.map.maxattempts). Every try reads the same file from the start and fails at the same spot, then the job fails. On a big file this looks like one task stuck for ages.
  • Skipping bad records does not help. SkipBadRecords is for records that crash your map code. A Snappy stream has no sync markers, so there is no clean place to start again after a bad block. Spark can skip the whole file with spark.sql.files.ignoreCorruptFiles, see the Spark docs.
  • Wrong Snappy flavor. A file made by another Snappy tool in the Snappy framing format is not Hadoop's block format, and fails the same way.
  • Fix. Take the file name from the failed task log, test it with hdfs dfs -text <file> > /dev/null, then replace or move the file.

Snappy vs gzip, bzip2 and zstd in Hadoop

  • Snappy is built for speed. Files are bigger than with gzip, but compressing and reading is fast, which is why it became the usual choice for intermediate data and ORC or Parquet tables.
  • Gzip makes smaller files but costs more CPU. Like Snappy, a gzip text file cannot be split.
  • Bzip2 is slow, but BZip2Codec is the codec that ships with Hadoop that can split plain text files.
  • Zstd has been in Hadoop since 2.9.0 as ZStandardCodec. It gets close to gzip on size at much better speed, so it is worth testing for data you keep for a long time.

Related posts: fixing a hanging Hive DROP TABLE on the metastore, Hive on Tez vs Hive on Spark, and an older example of analyzing IIS logs with Hive where compressed partitions pay off.

Need help with distributed systems, backend engineering or data platforms? See my consulting services.

Alexander Alten

Alexander Alten

Founder of Scalytics, co-creator and PMC member of Apache Wayang, and author of KafScale. I write about Kafka, streaming, data platforms, IoT and AI agent infrastructure in production.