MapReduce工作笔记——Hadoop Streaming多目录/多路输入

时间:2022-07-24
本文章向大家介绍MapReduce工作笔记——Hadoop Streaming多目录/多路输入,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

0. 前言

在工作中时常会遇到一个job需要多路径的输入,比如计算CTR,需要PV、Click的输入路径,或者是想对一周的数据做Merge等, 下面将提供三种方法来完成多目录/多路输入。

比如,我路径下有如下共12个文件:

$ hls  /home/wangcongying/test/

/home/wangcongying/test/20181101
/home/wangcongying/test/20181102
/home/wangcongying/test/20181103
/home/wangcongying/test/20181104
/home/wangcongying/test/20181105
/home/wangcongying/test/20181106
/home/wangcongying/test/20181107
/home/wangcongying/test/20181108
/home/wangcongying/test/20181109
/home/wangcongying/test/20181110
/home/wangcongying/test/20181111
/home/wangcongying/test/otherFile

每一个路径下有10个part,如下:

$ hls  /home/wangcongying/test/20181101

/home/wangcongying/test/20181101/part-00000
/home/wangcongying/test/20181101/part-00001
/home/wangcongying/test/20181101/part-00002
/home/wangcongying/test/20181101/part-00003
/home/wangcongying/test/20181101/part-00004
/home/wangcongying/test/20181101/part-00005
/home/wangcongying/test/20181101/part-00006
/home/wangcongying/test/20181101/part-00007
/home/wangcongying/test/20181101/part-00008
/home/wangcongying/test/20181101/part-00009

设置一下几个多路输入问题:

  1. 输入20181101-20181109 所有目录下的文件
  2. 输入20181101以及otherFile目录下的所有文件
  3. 输入20181101以及otherFile目录下的前五个part

1. 使用通配符*

使用通配符*是最简单的方法,下面将给出几个简单常用的示例: 解决方案:

  1. 输入20181101-20181109 所有目录下的文件
-input /home/wangcongying/test/2018110[1-9]
  1. 输入20181101以及otherFile目录下的所有文件
-input /home/wangcongying/test/{20181101,otherFile}
  1. 输入20181101以及otherFile目录下的前五个part
-input /home/wangcongying/test/{20181101,otherFile}/part-0000[0-4]

2. 逗号分割

  1. 输入20181101-20181109 所有目录下的文件
-input /home/wangcongying/test/20181101,/home/wangcongying/test/20181102,/home/wangcongying/test/20181103,/home/wangcongying/test/20181104,/home/wangcongying/test/20181105,/home/wangcongying/test/20181106,/home/wangcongying/test/20181107,/home/wangcongying/test/20181108,/home/wangcongying/test/20181109
  1. 输入20181101以及otherFile目录下的所有文件
-input /home/wangcongying/test/20181101,/home/wangcongying/test/otherFile
  1. 输入20181101以及otherFile目录下的前五个part
-input /home/wangcongying/test/20181101/part-0000[0-4],/home/wangcongying/test/otherFile/part-0000[0-4]

3. 输入数组形式

  1. 输入20181101-20181109 所有目录下的文件
input_file=("/home/wangcongying/test/2018110[1-9]")
...
...
Hadoop streaming ...
	...
	-input ${input_file[@]} 
	...
  1. 输入20181101以及otherFile目录下的所有文件
input_file=("/home/wangcongying/test/20181101"
			  "/home/wangcongying/test/otherFile")
...
...
Hadoop streaming ...
	...
	-input ${input_file[@]} 
	...
  1. 输入20181101以及otherFile目录下的前五个part
input_file=("/home/wangcongying/test/20181101/part-0000[0-4]"
			  "/home/wangcongying/test/otherFile/part-0000[0-4]")
...
...
Hadoop streaming ...
	...
	-input ${input_file[@]} 
	...

4. 总结

在使用的时候可以两两或者三个混合使用,具体根据需求来实现即可。