手机版
你好,游客 登录 注册
背景:
阅读新闻

Hadoop 3台虚拟机的小型集群

[日期:2012-07-25] 来源:Linux社区  作者:Linux [字体:大 中 小]

Map/Reduce程序

1. Map

  1. public class MaxTemperatureMapper extends  
  2.         Mapper<LongWritable, Text, Text, IntWritable> {  
  3.   
  4.     private static final int MISSIGN = 9999;  
  5.       
  6.     @Override  
  7.     public void map(LongWritable key, Text value, org.apache.Hadoop.mapreduce.Mapper<LongWritable,Text,Text,IntWritable>.Context context) throws IOException ,InterruptedException {  
  8.         String line = value.toString();  
  9.         String year = line.substring(15, 19);  
  10.   
  11.         int airTemperator = 0;  
  12.         if (line.charAt(87) == '+') {  
  13.             airTemperator = Integer.parseInt(line.substring(88, 92));  
  14.         } else {  
  15.             airTemperator = Integer.parseInt(line.substring(87, 92));  
  16.         }  
  17.         String quality = line.substring(92, 93);  
  18.         if (airTemperator < MISSIGN && quality.matches("[01459]")) {  
  19.             context.write(new Text(year), new IntWritable(airTemperator));  
  20.         }  
  21.           
  22.     };  
  23.   
  24. }  

2. Reduce

  1. public class MaxTemperatureReducer extends  
  2.         Reducer<Text, IntWritable, Text, IntWritable> {  
  3.   
  4.     @Override  
  5.     public void reduce(Text key, Iterable<IntWritable> values, Context context)  
  6.             throws IOException, InterruptedException {  
  7.         int maxValue = Integer.MIN_VALUE;  
  8.         for (IntWritable value : values) {  
  9.             maxValue = Math.max(maxValue, value.get());  
  10.         }  
  11.         context.write(key, new IntWritable(maxValue));  
  12.     };  
  13.   
  14. }  

3. Client

  1. public class MaxTemperature {  
  2.       
  3.     public static void main(String[] args) throws Exception {  
  4.         if (args.length != 2) {  
  5.             System.out.println("Usage MaxTemperator <input path> <output path>");  
  6.             System.exit(-1);  
  7.         }  
  8.         Job job = new Job();  
  9.           
  10.         job.setJarByClass(MaxTemperature.class);  
  11.           
  12.         job.setMapperClass(MaxTemperatureMapper.class);  
  13.         job.setReducerClass(MaxTemperatureReducer.class);  
  14.           
  15.         job.setOutputKeyClass(Text.class);  
  16.         job.setOutputValueClass(IntWritable.class);  
  17.           
  18.         FileInputFormat.addInputPath(job, new Path(args[0]));  
  19.         FileOutputFormat.setOutputPath(job, new Path(args[1]));  
  20.           
  21.         System.exit(job.waitForCompletion(true) ? 0 : 1);  
  22.     }  
  23. }  

4. 运行

将上述程序达成jar包放到$HADOOP_CLASSPATH路径下。

然后执行 hadoop jar test.jar com.tx.weahter.MaxTemerature input/sample.txt output

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款