Unzip files using hadoop streaming

2019-02-25 04:39发布

I have many files in HDFS, all of them a zip file with one CSV file inside it. I'm trying to uncompress the files so I can run a streaming job on them.

I tried:

hadoop jar /usr/lib/hadoop-mapreduce/hadoop-streaming.jar \
    -D mapred.reduce.tasks=0 \
    -mapper /bin/zcat -reducer /bin/cat \
    -input /path/to/files/ \
    -output /path/to/output

However I get an error (subprocess failed with code 1) I also tried running on a single file, same error.

Any advice?

4条回答
Root(大扎)
2楼-- · 2019-02-25 04:48

After experimenting around, I discovered that if you do this modification to hadoop streaming, you will get all your gzipped files uncompressed in a new directory. The file names are all lost (renamed to the typical part-XXXX name), but this worked for me.

I speculate this works because hadoop automatically uncompresses gzipped files under the hood, and cat is just echoing that unzipped output

hadoop jar /usr/iop/4.2.0.0/hadoop-mapreduce/hadoop-streaming.jar \
    -D mapred.reduce.tasks=0 \
    -mapper /bin/cat \
    -input  /path-to-gzip-files-directory \
    -output /your-gunzipped-directory
查看更多
不美不萌又怎样
3楼-- · 2019-02-25 05:03

Hadoop can read files compressed in the gzip format, but that's different from the zip format. Hadoop cannot read zip files AFAIK.

查看更多
三岁会撩人
4楼-- · 2019-02-25 05:04

The root cause of the problem is: you get many (text-)infos from hadoop (before you can receive the data).

e.g. hdfs dfs -cat hdfs://hdm1.gphd.local:8020/hive/gphd/warehouse/my.db/my/part-m-00000.gz | zcat | wc -l will NOT work either - with "gzip: stdin: not in gzip format" error message.

Therefore you should skip this "unneccesary" infos. In my case I have to skip 86 lines

Therefore my one line command will be this (for counting the records): hdfs dfs -cat hdfs://hdm1.gphd.local:8020/hive/gphd/warehouse/my.db/my/part-m-00000.gz |tail -n+86 | zcat | wc -l

Note: this is a workaround (not a real solution) and very ugly - because of "86" - but it works fine :)

查看更多
▲ chillily
5楼-- · 2019-02-25 05:04

A Simple way to unzip / uncompress a file within HDFS for whatever reason

hadoop fs -text /hdfs-path-to-zipped-file.gz | hadoop fs -put - /hdfs-path-to-unzipped-file.txt
查看更多
登录 后发表回答