標籤:+= count 下載 間隔 stream split() info 編寫程式 txt
1. 用Python編寫WordCount程式並提交任務
程式 |
WordCount |
輸入 |
一個包含大量單詞的文字檔 |
輸出 |
檔案中每個單詞及其出現次數(頻數),並按照單詞字母順序排序,每個單詞和其頻數佔一行,單詞和頻數之間有間隔 |
2.編寫map函數,reduce函數
import sysfor line in sys.stdin: line=line.strip() words=line.split() for word in words: print ‘%s\t%s‘ % (word,1)from operator import itemgetterimport syscurrent_word=Nonecurrent_count=0word=Nonefor line in sys.stdin: line=line.strip() word,count=line.split(‘\t‘,1) try: count=int(count) except ValueError: continue if current_word==word: current_count+=count else: if current_word: print ‘%s\t%s‘ % (current_word,current_count) current_count=count current_word=wordif current_word==word: print ‘%s\t%s‘ % (current_word,current_count)
3.將其許可權作出相應修改
chmod a+x /home/hadoop/wc/mapper.pychmod a+x /home/hadoop/wc/reducer.py
4.本機上測試回合代碼
5.查看運行結果
2. 用mapreduce 處理氣象資料集
編寫程式求每日最高最低氣溫,區間最高最低氣溫
- 氣象資料集為:ftp://ftp.ncdc.noaa.gov/pub/data/noaa
- 按學號後三位下載不同年份月份的資料(例如201506110136號同學,就下載2013年以6開頭的資料,看具體資料情況稍有變通)
wget -D --accept-regex=REGEX -p data -r -c ftp://ftp.ncdc.noaa.gov/pub/data/noaa/2013/6*
- 解壓資料集,並儲存在文字檔中
zcat ftp.ncdc.noaa.gov/pub/data/noaa/2013/6*.gz >qxdatazwt.txt
- 對氣象資料格式進行解析
5.編寫map函數,reduce函數
- 將其許可權作出相應修改
chmod a+x /home/hadoop/mapper.pychmod a+x /home/hadoop/wc/reducer.py
本機上測試回合代碼
放到HDFS上運行
將之前爬取的文字檔上傳到hdfs上
用Hadoop Streaming命令提交任務
查看運行結果
用Python編寫WordCount程式任務