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
Post a Comment