用mapreduce 处理气象数据集

用mapreduce 处理气象数据集

编写程序求每日最高最低气温,区间最高最低气温

  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 

     

posted @ 2018-05-09 20:59  168李文辉  阅读(173)  评论(0编辑  收藏  举报