zoukankan      html  css  js  c++  java
  • 用mapreduce 处理气象数据集

    用mapreduce 处理气象数据集

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

    1. 气象数据集下载地址为:ftp://ftp.ncdc.noaa.gov/pub/data/noaa
    2. 按学号后三位下载不同年份月份的数据(例如201506110136号同学,就下载2013年以6开头的数据,看具体数据情况稍有变通)
      cd /usr/hadoop
      sodu mkdir qx
      cd /usr/hadoop/qx
      
      wget -D --accept-regex=REGEX -P data -r -c ftp://ftp.ncdc.noaa.gov/pub/data/noaa/2018/9*
    3. 解压数据集,并保存在文本文件中
      cd /usr/hadoop/qx/data/ftp.ncdc.noaa.gov/pub/data/noaa/2018
      sudo zcat 9*.gz >qxdata.txt
    4. 对气象数据格式进行解析
      head qxdata.txt
    5. 编写map函数,reduce函数   
      gedit mapper.py
      gedit reducer.py

      使用 mapper.py 编写map函数

    6. #!/usr/bin/env python
      import sys
      for i in sys.stdin:
           i = i.strip()
           d = i[15:23]
           t = i[87:92]
      
           print '%s	%s' % (d,t) 

      使用reducer.py 编写reducer函数

      #!/usr/bin/env python
      from operator import itemggetter
      import sys
      
      current_word = None
      current_count = 0
      word = None
      
      for i in sys.stdin:
           i = i.strip()
           word,count = i.split('	', 1)
           try:
                count = int(count)
           except ValueError:
                continue
      
           if current_word == word:
               if current_count > count:
                    current_count = count
           else:
               if current_word:
                   print '%s	%s' % (current_word, current_count)
               current_count = count
               current_word = word
      
      if current_word == word:
           print '%s	%s' % (current_word, current_count)
    7. 将其权限作出相应修改
      chmod a+x /usr/hadoop/qx/mapper.py
      chmod a+x /usr/hadoop/qx/reducer.py
    8. 本机上测试运行代码
    9. 放到HDFS上运行
      1. 将之前爬取的文本文件上传到hdfs上
      2. 用Hadoop Streaming命令提交任务
    10. 查看运行结果
  • 相关阅读:
    ios 常用的数学函数
    iOS已发布应用中对异常信息捕获和处理
    iOS 制作framework步骤
    iOS 静态库和动态库.a和framework
    C#实现发送和接收pop3邮件方法
    C#实现给手机发送短信
    C#DataTable DataSet DataRow区别详解
    gridview合并相同的行
    ASP.NET导出EXCEl方法使用EXCEl对象
    ASP.NET导出EXCEl方法使用COM.EXCEL不使用EXCEl对象
  • 原文地址:https://www.cnblogs.com/ashh/p/9016938.html
Copyright © 2011-2022 走看看