设为首页 收藏本站
查看: 996|回复: 0

[经验分享] hadoop实验:求气象数据的最低温度

[复制链接]

尚未签到

发表于 2017-12-17 18:42:41 | 显示全部楼层 |阅读模式
  

  1.下载部分数据。由于实验就仅仅下载2003年的部分气象数据
DSC0000.jpg

  2.通过zcat *gz > sample.txt命令解压重定向
[hadoop@Master test_data]$ zcat *gz > /home/hadoop/input/sample.txt

  3.查看数据格式
DSC0001.jpg

  4.把文件sample.txt放进hdfs文件系统里
[hadoop@Master input]$ hadoop fs -put /home/hadoop/input/sample.txt  /user/hadoop/in/sample.txt

DSC0002.jpg

  5.Maper : MinTemperatureMapper.java
  

DSC0003.jpg

  

import java.io.IOException;  import org.apache.hadoop.io.IntWritable;
  import org.apache.hadoop.io.LongWritable;
  import org.apache.hadoop.io.Text;
  import org.apache.hadoop.mapreduce.Mapper;
  


  public>  extends Mapper<LongWritable, Text, Text, IntWritable>
  {
  

  private static final int MISSING = -9999;
  

  @Override
  public void map(LongWritable key, Text value, Context context)
  throws IOException, InterruptedException{
  

  String line = value.toString();
  String year = line.substring(0,4);
  int airTemperature;
  airTemperature= Integer.parseInt(line.substring(14, 19).trim());
  

  if (airTemperature!= MISSING) {
  context.write(new Text(year), new IntWritable(airTemperature));
  }
  }
  

  

  6.Reducer :MinTemperatureReducer.java
  

import java.io.IOException;  
import org.apache.hadoop.io.IntWritable;
  
import org.apache.hadoop.io.Text;
  
import org.apache.hadoop.mapreduce.Reducer;
  


  
public>  extends Reducer<Text, IntWritable, Text, IntWritable>
  
{
  

  @Override
  public void reduce(Text key, Iterable<IntWritable> values,Context context)
  throws IOException, InterruptedException
  {
  

  int minValue= Integer.MAX_VALUE;
  for (IntWritable value : values)
  {
  minValue= Math.min(minValue, value.get());
  }
  context.write(key, new IntWritable(minValue));
  }
  
}
  

  

  

  7.M-R Job :MinTemperature.java
  

import org.apache.hadoop.mapreduce.Job;  
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  


  
public>  
{
  public static void main(String[] args) throws Exception
  {
  if (args.length!= 2)
  {
  System.err.println("Usage: MinTemperature<input path> <output path>");
  System.exit(-1);
  }
  Job job= new Job();
  job.setJarByClass(MinTemperature.class);
  job.setJobName("Min temperature");
  FileInputFormat.addInputPath(job, new Path(args[0]));
  FileOutputFormat.setOutputPath(job, new Path(args[1]));
  job.setMapperClass(MinTemperatureMapper.class);
  job.setReducerClass(MinTemperatureReducer.class);
  job.setOutputKeyClass(Text.class);
  job.setOutputValueClass(IntWritable.class);
  System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
  
}
  

  

  

  8.编译,压缩成jar 包
  
[hadoop@Master myclass]$ javac -classpath /usr/hadoop/hadoop-core-1.2.1.jar  MinTemperature*.java
  
[hadoop@Master myclass]$ jar cvf MinTemperature.jar MinTemperature*.class
  
added manifest
  
adding: MinTemperature.class(in = 1417) (out= 799)(deflated 43%)
  
adding: MinTemperatureMapper.class(in = 1740) (out= 722)(deflated 58%)
  
adding: MinTemperatureReducer.class(in = 1664) (out= 707)(deflated 57%)
  

DSC0004.jpg

  

  9.运行作业
[hadoop@Master myclass]$ hadoop jar /usr/hadoop/myclass/MinTemperature.jar MinTemperature  /user/hadoop/in/sample.txt  ./out2

  

  运行报错。发现报错,信息例如以下
  

DSC0005.jpg

  

  找了半天原因。发现是没删掉class ,程序找不到类。在myclass 文件下删掉class文件。仅仅保留生成的jar包
[hadoop@Master myclass]$ rm MinTemperature*.class

DSC0006.jpg

  

  10.查看结果
   DSC0007.jpg
  

  

  

  

  

  

  

  




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-425101-1-1.html 上篇帖子: Hadoop安全(1) 下篇帖子: hadoop Datanode Uuid unassigned
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表