用mapreduce 處理氣象數據集

用mapreduce 處理氣象數據集python

編寫程序求每日最高最低氣溫,區間最高最低氣溫app

  1. 氣象數據集下載地址爲:ftp://ftp.ncdc.noaa.gov/pub/data/noaa
  2. 按學號後三位下載不一樣年份月份的數據(例如201506110136號同窗,就下載2013年以6開頭的數據,看具體數據狀況稍有變通)
  3. 解壓數據集,並保存在文本文件中
  4. 對氣象數據格式進行解析
  5. 編寫map函數,reduce函數
  6. 將其權限做出相應修改
  7. 本機上測試運行代碼
  8. 放到HDFS上運行
    1. 將以前爬取的文本文件上傳到hdfs上
    2. 用Hadoop Streaming命令提交任務
  9. 查看運行結果
    import sys
    
    for line in sys.stdin:
        line = line.strip()
        print('%s\t%d' % (line[15:23], int(line[87:92])))
    from operator import itemgetter
    import sys
    
    current_date = None
    current_temperature = 0
    date = None
    
    for line in sys.stdin:
        line = line.strip()
        date, temperature = line.split('\t', 1)
        try:
            temperature = int(temperature)
        except ValueError:
            continue
    
        if current_date == date:
            if current_temperature < temperature:
                current_temperature = temperature
        else:
            if current_date:
                print('%s\t%d' % (current_date, current_temperature))
            current_temperature = temperature
            current_date = date
    
    if current_date == date:
        print('%s\t%d' % (current_date, current_temperature))
    chmod a+x mapper.py 
    chmod a+x reducer.py 
    cat test.txt | python mapper.py | python reducer.py 
相關文章
相關標籤/搜索