hadoop streaming anaconda python 計算平均值
原始Liunx 的python版本不帶numpy ,安裝了anaconda 之後,使用hadoop streaming 時無法調用anaconda python ,
後來發現是參數沒設置好。。。
進入正題:
環境:
4臺服務器:master slave1 slave2 slave3。
全部安裝anaconda2與anaconda3, 主環境py2 。anaconda2與anaconda3共存見:Ubuntu16.04 Liunx下同時安裝Anaconda2與Anaconda3
安裝目錄:/home/orient/anaconda2
Hadoop 版本2.4.0
數據準備:
inputFile.txt 一共100個數字 全部數據 下載:
0.970413
0.901817
0.828698
0.197744
0.466887
0.962147
0.187294
0.388509
0.243889
0.115732
0.616292
0.713436
0.761446
0.944123
0.200903
編寫mrMeanMapper.py
1 #!/usr/bin/env python
2 import sys
3 from numpy import mat, mean, power
4
5 def read_input(file):
6 for line in file:
7 yield line.rstrip()
8
9 input = read_input(sys.stdin)#creates a list of input lines
10 input = [float(line) for line in input] #overwrite with floats
11 numInputs = len(input)
12 input = mat(input)
13 sqInput = power(input,2)
14
15 #output size, mean, mean(square values)
16 print "%d\t%f\t%f" % (numInputs, mean(input), mean(sqInput)) # calc mean of columns
17 print >> sys.stderr, "report: still alive"
編寫mrMeanReducer.py
1 #!/usr/bin/env python
2 import sys
3 from numpy import mat, mean, power
4
5 def read_input(file):
6 for line in file:
7 yield line.rstrip()
8
9 input = read_input(sys.stdin)#creates a list of input lines
10
11 #split input lines into separate items and store in list of lists
12 mapperOut = [line.split(‘\t‘) for line in input]
13
14 #accumulate total number of samples, overall sum and overall sum sq
15 cumVal=0.0
16 cumSumSq=0.0
17 cumN=0.0
18 for instance in mapperOut:
19 nj = float(instance[0])
20 cumN += nj
21 cumVal += nj*float(instance[1])
22 cumSumSq += nj*float(instance[2])
23
24 #calculate means
25 mean = cumVal/cumN
26 meanSq = cumSumSq/cumN
27
28 #output size, mean, mean(square values)
29 print "%d\t%f\t%f" % (cumN, mean, meanSq)
30 print >> sys.stderr, "report: still alive"
本地測試mrMeanMapper.py ,mrMeanReducer.py
cat inputFile.txt |python mrMeanMapper.py |python mrMeanReducer.py
我把 inputFile.txt,mrMeanMapper.py ,mrMeanReducer.py都放在了同一目錄下 ~/zhangle/Ch15/hh/hh
所有的操作也都是這此目錄下!!!
將inputFile.txt上傳到hdfs
zhangle/mrmean-i 是HDFS上的目錄
hadoop fs -put inputFile.txt zhangle/mrmean-i
運行Hadoop streaming
1 hadoop jar /usr/programs/hadoop-2.4.0/share/hadoop/tools/lib/hadoop-streaming-2.4.0.jar 2 -input zhangle/mrmean-i 3 -output zhangle/output12222 4 -file mrMeanMapper.py 5 -file mrMeanReducer.py 6 -mapper "/home/orient/anaconda2/bin/python mrMeanMapper.py" 7 -reducer "/home/orient/anaconda2/bin/python mrMeanReducer.py"
參數解釋:
第一行:/usr/programs/hadoop-2.4.0/share/hadoop/tools/lib/hadoop-streaming-2.4.0.jar 是我Hadoop streaming 所在的目錄
第二行: zhangle/mrmean-i 是剛才將inputFile.txt 上傳的目錄
第三行:zhangle/mrmean-out12222 是結果輸出目錄,也是在HDFS上
第四行: mrMeanMapper.py是當前目錄下的mapper程序
第五行: mrMeanRdeducer.py是當前目錄下的reducer程序
第六行: /home/orient/anaconda2/bin/python 是anaconda2目錄下的python ,如果去掉,會直接調用自帶的python,自帶python沒有安裝numpy等python包。!!
第七行: 同第六行。
查看運行結果:
hadoop fs -cat zhangle/output12222/part-00000
問題解決
1. 出現“Error: java.lang.RuntimeException: PipeMapRed.waitOutputThreads(): subprocess failed with code 1”的錯誤
解決方法:
在hadoop上實施MapReduce之前,一定要在本地運行一下你的python程序,看
-
首先進入包含map和reduce兩個py腳本文件和數據文件inputFile.txt的文件夾中。然後輸入一下命令,看是否執行通過:
-
cat inputFile.txt |python mrMeanMapper.py |python mrMeanReducer.py
2.出現錯誤:“Error: java.lang.RuntimeException: PipeMapRed.waitOutputThreads(): subprocess failed with code 2”,或者出現jar文件找不到的情況,或者出現輸出文件夾已經存在的情況。
- Mapper.py和Reduce.py的最前面要加上:#!/usr/bin/env python 這條語句
- 在Hadoop Streaming命令中,請確保按以下的格式來輸入
-
1 hadoop jar /usr/programs/hadoop-2.4.0/share/hadoop/tools/lib/hadoop-streaming-2.4.0.jar 2 -input zhangle/mrmean-i 3 -output zhangle/output12222 4 -file mrMeanMapper.py 5 -file mrMeanReducer.py 6 -mapper "/home/orient/anaconda2/bin/python mrMeanMapper.py" 7 -reducer "/home/orient/anaconda2/bin/python mrMeanReducer.py"
- 要確保jar文件的路徑正確,hadoop 2.4版本的該文件是保存在:$HADOOP_HOME/share/hadoop/tools/lib/hadoop-streaming-2.4.0.jar中,不同的hadoop版本可能略有不同HDFS中的輸出文件夾(這裏是HDFS下的/user/hadoop/mr-ouput13),一定要是一個新的(之前不存在)的文件夾,因為即使上條Hadoop Streaming命令沒有執行成功,仍然會根據你的命令來創建輸出文件夾,而後面再輸入Hadoop Streaming命令如果使用相同的輸出文件夾時,就會出現“輸出文件夾已經存在的錯誤”;參數 –file後面是map和reduce的腳本,路徑可以是詳細的絕對路徑,,也可以是當前路徑,當前路徑下一定要有mapper,reducer 函數,但是在參數 -mapper 和-reducer之後,需要指定python腳本的環境目錄,而且用引號引起來。
3.出現錯誤:“Error: java.lang.RuntimeException: PipeMapRed.waitOutputThreads(): subprocess failed with code 127”.
腳本環境的問題 在第六行與第七行 加上python 環境目錄即可。
參考:
http://www.cnblogs.com/lzllovesyl/p/5286793.html
http://www.zhaizhouwei.cn/hadoop/190.html
http://blog.csdn.net/wangzhiqing3/article/details/8633208
hadoop streaming anaconda python 計算平均值