欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hadoop mapreduce开发实践之输出数据压缩

程序员文章站 2022-07-15 22:27:31
...

1、hadoop 输出数据压缩

1.1、为什么要压缩?

  • 输出数据较大时,使用hadoop提供的压缩机制对数据进行压缩,可以指定压缩的方式。减少网络传输带宽和存储的消耗;
  • 可以对map的输出进行压缩(map输出到reduce输入的过程,可以shuffle过程中网络传输的数据量)
  • 可以对reduce的输出结果进行压缩(最终保存到hdfs上的数据,主要是减少占用HDFS存储)

mapper和reduce程序都不需要更改,只需要在streaming程序运行中指定参数即可;

-jobconf  "mapred.compress.map.output=true" \
-jobconf  "mapred.map.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec" \
-jobconf  "mapred.output.compress=true" \
-jobconf  "mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec" \

1.2、 run_streaming程序

#!/bin/bash

HADOOP_CMD="/home/hadoop/app/hadoop/hadoop-2.6.0-cdh5.13.0/bin/hadoop"
STREAM_JAR_PATH="/home/hadoop/app/hadoop/hadoop-2.6.0-cdh5.13.0/share/hadoop/tools/lib/hadoop-streaming-2.6.0-cdh5.13.0.jar"

INPUT_FILE_PATH="/input/The_Man_of_Property"
OUTPUT_FILE_PATH="/output/wordcount/CacheArchiveCompressFile"

$HADOOP_CMD fs -rmr -skipTrash $OUTPUT_FILE_PATH

$HADOOP_CMD jar $STREAM_JAR_PATH \
                -input $INPUT_FILE_PATH \
                -output $OUTPUT_FILE_PATH \
                -jobconf "mapred.job.name=wordcount_wordwhite_cacheArchivefile_demo" \
                -jobconf  "mapred.compress.map.output=true" \
                -jobconf  "mapred.map.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec" \
                -jobconf  "mapred.output.compress=true" \
                -jobconf  "mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec" \
                -mapper "python mapper.py WHF.gz" \
                -reducer "python reducer.py" \
                -cacheArchive "hdfs://localhost:9000/input/cachefile/wordwhite.tar.gz#WHF.gz" \
                -file "./mapper.py" \
                -file "./reducer.py"

1.3、 执行程序

$ chmod +x run_streaming_compress.sh
$ ./run_streaming_compress.sh
... 中间输出省略 ...
18/02/02 10:51:50 INFO streaming.StreamJob: Output directory: /output/wordcount/CacheArchiveCompressFile

1.4、 查看结果

$ hadoop fs -ls /output/wordcount/CacheArchiveCompressFile
Found 2 items
-rw-r--r--   1 hadoop supergroup          0 2018-02-02 10:51 /output/wordcount/CacheArchiveCompressFile/_SUCCESS
-rw-r--r--   1 hadoop supergroup         81 2018-02-02 10:51 /output/wordcount/CacheArchiveCompressFile/part-00000.gz
$ hadoop fs -get /output/wordcount/CacheArchiveCompressFile/part-00000.gz ./
$ gunzip part-00000.gz 
$ cat part-00000 
and 2573
had 1526
have    350
in  1694
or  253
the 5144
this    412
to  2782

2、hadoop streaming 语法参考


本文转自 巴利奇 51CTO博客,原文链接:http://blog.51cto.com/balich/2068046