Compression codecs make data smaller so it uses less space and moves faster. This helps when working with big data in Hadoop.
Compression codecs (Snappy, LZO, Gzip) in Hadoop
hadoop jar your-job.jar -D mapreduce.output.fileoutputformat.compress=true \
-D mapreduce.output.fileoutputformat.compress.codec=org.apache.hadoop.io.compress.SnappyCodec \
-D mapreduce.output.fileoutputformat.compress.type=BLOCK \
input_path output_pathYou set compression codecs in Hadoop job configuration using codec class names.
Common codecs are Snappy, LZO, and Gzip, each with different speed and compression levels.
mapreduce.output.fileoutputformat.compress.codec=org.apache.hadoop.io.compress.SnappyCodec
mapreduce.output.fileoutputformat.compress.codec=com.hadoop.compression.lzo.LzoCodec
mapreduce.output.fileoutputformat.compress.codec=org.apache.hadoop.io.compress.GzipCodec
This Python example writes a text file compressed with Gzip codec to Hadoop HDFS, then reads it back and prints the content.
from pydoop import hdfs # Write a text file compressed with Gzip in Hadoop HDFS file_path = '/user/test/sample.txt.gz' text = 'Hello Hadoop compression codecs!\n' * 5 with hdfs.open(file_path, 'wt', codec='gzip') as f: f.write(text) # Read back the file with hdfs.open(file_path, 'rt', codec='gzip') as f: content = f.read() print(content)
Snappy is fast but compresses less than Gzip.
LZO requires installing native libraries on all Hadoop nodes.
Gzip compresses best but uses more CPU and is slower.
Compression codecs reduce data size to save space and speed up Hadoop jobs.
Snappy, LZO, and Gzip are popular codecs with different speed and compression trade-offs.
Choose the codec based on your need for speed, compression ratio, and cluster setup.
Practice
Solution
Step 1: Understand codec speed characteristics
Snappy is designed for speed, offering fast compression and decompression.Step 2: Compare with other codecs
Gzip compresses better but slower; LZO is fast but usually slower than Snappy; Bzip2 is slowest.Final Answer:
Snappy -> Option CQuick Check:
Fastest codec = Snappy [OK]
- Confusing Gzip as fastest due to better compression
- Thinking LZO is faster than Snappy
- Assuming Bzip2 is fast
Solution
Step 1: Identify the property for compression codec
The property key is 'mapreduce.output.fileoutputformat.compress.codec' to set codec class.Step 2: Match the codec class for Snappy
Snappy codec class is 'org.apache.hadoop.io.compress.SnappyCodec'.Final Answer:
Property with SnappyCodec class -> Option BQuick Check:
Snappy codec class = org.apache.hadoop.io.compress.SnappyCodec [OK]
- Using GzipCodec or other codec classes by mistake
- Misspelling the property name
- Confusing Lz4Codec with LZO
conf.set("mapreduce.output.fileoutputformat.compress", "true");
conf.set("mapreduce.output.fileoutputformat.compress.codec", "org.apache.hadoop.io.compress.GzipCodec");
Solution
Step 1: Check if compression is enabled
Property 'mapreduce.output.fileoutputformat.compress' is set to 'true', so compression is enabled.Step 2: Identify the codec class used
Codec class is 'org.apache.hadoop.io.compress.GzipCodec', which means Gzip compression is used.Final Answer:
Gzip -> Option DQuick Check:
Compression enabled with Gzip codec [OK]
- Ignoring the compress flag and assuming no compression
- Confusing codec class with Snappy or LZO
- Assuming default codec if not checked
Solution
Step 1: Understand LZO codec requirements
LZO requires native libraries installed on all cluster nodes to work properly.Step 2: Analyze error cause
Codec not found error usually means native LZO libraries are missing or not accessible in the library path.Final Answer:
LZO native libraries missing or not in path -> Option AQuick Check:
LZO needs native libs installed [OK]
- Assuming misspelled property causes codec not found
- Confusing Snappy and LZO codec errors
- Thinking Gzip is incompatible with Hadoop
Solution
Step 1: Identify compression needs
Best compression ratio means smallest file size, speed is less important.Step 2: Compare codecs by compression ratio and speed
Gzip compresses better but slower; Snappy is fastest but less compression; LZO is in between.Step 3: Choose codec matching needs
Gzip fits best for high compression ratio with slower speed tolerance.Final Answer:
Gzip, best compression ratio with slower speed -> Option AQuick Check:
Best compression ratio = Gzip [OK]
- Choosing Snappy for best compression ratio
- Thinking LZO compresses better than Gzip
- Avoiding compression despite need to save space
Ready to prove it?
Pass the test and this pattern moves unknown
