Skip to main content

Use snappy codec with Hive

Listen:

[1] Snappy is a compression and decompression library, initially developed from Google and now integrated into Hadoop. Snappy acts about 10% faster than LZO, the biggest differences are the packaging and that snappy only provides a codec and does not have a container spec, whereas LZO has a file-format container and a compression codec. Snappy is shipped with CDH3u2 (for Clouderas Distribution) included in the hadoop-0.20 package or in [2] Apache hadoop Version 0.21.0 up.

The example I explain was initially created from Esteban, an Cloudera Customer Operations Engineer.

Create a sequenced file
$ seq 1 1000 | awk '{OFS="\001";print $1, $1 % 10}' > test_input.hive
$ cat test_input.hive |head -5
11
22
33
44

Upload into hdfs
$ hadoop dfs -mkdir /tmp/hivetest
$ hadoop dfs -put /home/hdfs/test_input.hive /tmp/hivetest

$ hadoop dfs -ls /tmp/hivetest
Found 1 items
-rw-r--r--   3 hdfs supergroup       5893 2012-01-19 09:58 /tmp/hivetest/test_input.hive

Process the plain file in hive with snappy
Now we create an external table in hive with the content of the uploaded file. An external table reference in hive has to be a directory.

hive> create external table hivetest1 (a int, b int) location '/tmp/hivetest';
OK
Time taken: 0.053 seconds
hive> select * from hivetest1 limit 1;
OK
1       1
Time taken: 0.155 seconds

To work with another compression codec as configured in hive-site.xml we have to enable and define the codec usually done with internal SET statements from the hive command line. For batch jobs the definitions should be set with "hive -e SET ...."

hive> SET hive.exec.compress.output=true;
hive> SET mapred.output.compression.codec=org.apache.hadoop.io.compress.SnappyCodec;
hive> SET mapred.output.compression.type=BLOCK;
hive> create table hivetest2 (a int, b int);
OK
Time taken: 0.15 seconds

After we successfully created the table "hivetest2" we try to import the data from our generated sequence file into the second table and use the exported compression codec. Here, hive starts a mapreduce job and after finishing moves the data into the new table and overwrites existing data.
 
hive> insert overwrite table hivetest2 select * from hivetest1;
Total MapReduce jobs = 2
Launching Job 1 out of 2
[.. removed ..]
Ended Job = 1068060947, job is filtered out (removed at runtime).
Moving data to: hdfs://hadoop1.internal:9000/tmp/hive-hdfs/hive_2012-01-19_10-20-11_796_1729199454214158343/-ext-10000
Loading data to table default.hivetest2
Deleted hdfs://hadoop1.internal:9000/user/hive/warehouse/hivetest2
Table default.hivetest2 stats: [num_partitions: 0, num_files: 1, num_rows: 0, total_size: 4021]
1000 Rows loaded to hivetest2
OK
Time taken: 16.843 seconds

Now we want to see if we can read the data and if we used the compression codec we wanted. The mapreduce job created one file, as we expected.

hive> select * from hivetest2 limit 1;
OK
1       1
Time taken: 0.171 seconds
$ hadoop dfs -ls /user/hive/warehouse/hivetest2
Found 1 items
-rw-r--r--   3 hdfs supergroup       4021 2012-01-19 10:20 /user/hive/warehouse/hivetest2/000000_0.snappy

Finally read the file
$ hadoop fs -cat /user/hive/warehouse/hivetest2/000000_0.snappy | head -5
.¸11
22
33
44
55

We processed the file in hive with snappy, created an snappy output and can work with it. Remember, if you want to use snappy you have to set the codec in your mapreduce jobs.

We can also use LOAD DATA statements to process the data we created and compressed with snappy

hive> create table hivetest3 (a int, b int);
OK
hive> load data inpath '/user/hive/warehouse/hivetest2/000000_0.snappy' into table hivetest3;
Loading data to table default.hivetest3
OK
Time taken: 0.327 seconds
hive> select * from hivetest3 limit 5;
OK
1       1
2       2
3       3
4       4
5       5
Time taken: 0.178 seconds

[1] http://code.google.com/p/hadoop-snappy/
[2] https://issues.apache.org/jira/browse/HADOOP-7206

Comments

Popular posts from this blog

Deal with corrupted messages in Apache Kafka

Under some strange circumstances, it can happen that a message in a Kafka topic is corrupted. This often happens when using 3rd party frameworks with Kafka. In addition, Kafka < 0.9 does not have a lock on Log.read() at the consumer read level, but does have a lock on Log.write(). This can lead to a rare race condition as described in KAKFA-2477 [1]. A likely log entry looks like this: ERROR Error processing message, stopping consumer: (kafka.tools.ConsoleConsumer$) kafka.message.InvalidMessageException: Message is corrupt (stored crc = xxxxxxxxxx, computed crc = yyyyyyyyyy Kafka-Tools Kafka stores the offset of each consumer in Zookeeper. To read the offsets, Kafka provides handy tools [2]. But you can also use zkCli.sh, at least to display the consumer and the stored offsets. First we need to find the consumer for a topic (> Kafka 0.9): bin/kafka-consumer-groups.sh --zookeeper management01:2181 --describe --group test Prior to Kafka 0.9, the only way to get this in...

Beyond Ctrl+F - Use LLM's For PDF Analysis

PDFs are everywhere, seemingly indestructible, and present in our daily lives at all thinkable and unthinkable positions. We've all got mountains of them, and even companies shouting about "digital transformation" haven't managed to escape their clutches. Now, I'm a product guy, not a document management guru. But I started thinking: if PDFs are omnipresent in our existence, why not throw some cutting-edge AI at the problem? Maybe Large Language Models (LLMs) and Retrieval Augmented Generation (RAG) could be the answer. Don't get me wrong, PDF search indexes like Solr exist, but they're basically glorified Ctrl+F. They point you to the right file, but don't actually help you understand what's in it. And sure, Microsoft Fabric's got some fancy PDF Q&A stuff, but it's a complex beast with a hefty price tag. That's why I decided to experiment with LLMs and RAG. My idea? An intelligent knowledge base built on top of our existing P...

AI's False Reality: Understanding Hallucination

Artificial Intelligence (AI) has leapfrogged to the poster child of technological innovation, on track to transform industries in a scale similar to the Industrial Revolution of the 1800s. But in this case, as cutting-edge technology, AI presents its own unique challenge, exploiting our human behavior of "love to trust", we as humans face a challenge: AI hallucinations. This phenomenon, where AI models generate outputs that are factually incorrect, misleading, or entirely fabricated, raises complex questions about the reliability and trust of AI models and larger systems. The tendency for AI to hallucinate comes from several interrelated factors. Overfitting – a condition where models become overly specialized to their training data – can lead to confident but wildly inaccurate responses when presented with novel scenarios (Guo et al., 2017). Moreover, biases embedded within datasets shape the models' understanding of the world; if these datasets are flawed or unreprese...