转载请注明出处: http://blog.csdn.net/programmer_wei/article/details/45286749


我的intellij idea版本是14,hadoop版本2.6,使用《hadoop权威指南》的天气统计源码作为示例。


下面附上源码,数据集在http://hadoopbook.com/code.html可以下载1901和1902两年数据:

[java] view plain copy
  1. package com.hadoop.maxtemperature;  
  2.   
  3. import java.io.IOException;  
  4. import org.apache.hadoop.io.IntWritable;  
  5. import org.apache.hadoop.io.LongWritable;  
  6. import org.apache.hadoop.io.Text;  
  7. import org.apache.hadoop.mapreduce.Mapper;  
  8.   
  9. public class MaxTemperatureMapper  
  10.         extends Mapper<LongWritable, Text, Text, IntWritable> {  //注1  
  11.     private static final int MISSING = 9999;  
  12.     @Override  
  13.     public void map(LongWritable key, Text value, Context context)  
  14.             throws IOException, InterruptedException {  
  15.         String line = value.toString();  
  16.         String year = line.substring(1519);  
  17.         int airTemperature;  
  18.         if (line.charAt(87) == '+') { // parseInt doesn't like leading plus signs  
  19.             airTemperature = Integer.parseInt(line.substring(8892));  
  20.         } else {  
  21.             airTemperature = Integer.parseInt(line.substring(8792));  
  22.         }  
  23.         String quality = line.substring(9293);  
  24.         if (airTemperature != MISSING && quality.matches("[01459]")) {  
  25.             context.write(new Text(year), new IntWritable(airTemperature));  
  26.         }  
  27.     }  
  28. }  

[java] view plain copy
  1. package com.hadoop.maxtemperature;  
  2.   
  3.   
  4. import java.io.IOException;  
  5. import org.apache.hadoop.io.IntWritable;  
  6. import org.apache.hadoop.io.Text;  
  7. import org.apache.hadoop.mapreduce.Reducer;  
  8.   
  9. public class MaxTemperatureReducer  
  10.         extends Reducer<Text, IntWritable, Text, IntWritable> {  
  11.     @Override  
  12.     public void reduce(Text key, Iterable<IntWritable> values,  
  13.                        Context context)  
  14.             throws IOException, InterruptedException {  
  15.         int maxValue = Integer.MIN_VALUE;  
  16.         for (IntWritable value : values) {  
  17.             maxValue = Math.max(maxValue, value.get());  
  18.         }  
  19.         context.write(key, new IntWritable(maxValue));  
  20.     }  
  21. }  

[java] view plain copy
  1. package com.hadoop.maxtemperature;  
  2.   
  3.   
  4. import org.apache.hadoop.fs.Path;  
  5. import org.apache.hadoop.io.IntWritable;  
  6. import org.apache.hadoop.io.Text;  
  7. import org.apache.hadoop.mapreduce.Job;  
  8. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  9. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  10.   
  11. public class MaxTemperature {  
  12.     public static void main(String[] args) throws Exception {  
  13.         if (args.length != 2) {  
  14.             System.err.println("Usage: MaxTemperature <input path> <output path>");  
  15.             System.exit(-1);  
  16.         }  
  17.         Job job = new Job();  
  18.         job.setJarByClass(MaxTemperature.class);  
  19.         job.setJobName("Max temperature");  
  20.   
  21.         FileInputFormat.addInputPath(job, new Path(args[0]));  
  22.         FileOutputFormat.setOutputPath(job, new Path(args[1]));  
  23.   
  24.         job.setMapperClass(MaxTemperatureMapper.class);  
  25.         job.setReducerClass(MaxTemperatureReducer.class);  
  26.   
  27.         job.setOutputKeyClass(Text.class);              //注1  
  28.         job.setOutputValueClass(IntWritable.class);  
  29.   
  30.         System.exit(job.waitForCompletion(true) ? 0 : 1);  
  31.     }  
  32. }  




1、首先在hadoop官网上下载hadoop到本地(不需要进行环境变量的配置,仅仅只用下载hadoop的包即可)。

2、打开intellij idea创建一个空项目,并且将源码粘贴进去,如图

intellij idea本地开发调试hadoop的方法

2、这时可以看见代码中的许多类是无法识别的,别急。接下来打开project structure,在左侧找到modules

intellij idea本地开发调试hadoop的方法

3、点击下方箭头天假jar或目录

intellij idea本地开发调试hadoop的方法

4、将刚才下载的hadoop目录下的share文件夹中的相应目录添加进来

intellij idea本地开发调试hadoop的方法

5、点击左侧的artifacts,添加一个空jar

intellij idea本地开发调试hadoop的方法

6、输入jar的名称,这里我们输入maxtemperature

intellij idea本地开发调试hadoop的方法


7、点击output layout下方的小箭头,选择module output,然后勾选我们的项目,点击确定

intellij idea本地开发调试hadoop的方法

8、这时候,刚才显示的各种包和类的缺失错误信息就全部没有了。

intellij idea本地开发调试hadoop的方法


9、接下来,点击右上角的edit configurations,如果当前没有application则新建一个application

intellij idea本地开发调试hadoop的方法


10、application的名字我们这里取MaxTemperature,然后main class需要输入org.apache.hadoop.util.RunJar,再点击program arguments,填写参数,如下:

其中,第一个参数之前在project structure中填写的jar文件路径,第二个参数是输入文件的目录,第二个参数是输出文件的路径

intellij idea本地开发调试hadoop的方法


11、然后,我们需要新建一个输入路径,并将输入文件放进去,(输出文件不要创建,这个由系统自己创建)

intellij idea本地开发调试hadoop的方法

12、点击运行,却发现有错误提示,显示找不到类:

intellij idea本地开发调试hadoop的方法

13、经过查阅资料,发现刚才填写参数的地方着了一个参数,需要将main函数所在类的路径添加进去:

intellij idea本地开发调试hadoop的方法

14、再点击运行,发现运行成功

intellij idea本地开发调试hadoop的方法

15、这时候,在项目目录下面生成了一个output目录,里面则存放了运行结果

intellij idea本地开发调试hadoop的方法intellij idea本地开发调试hadoop的方法


过程中遇到的问题总结:

(1)、

在windows操作系统上,需要把windows64位平台的hadoop2.6插件包(hadoop.dll,winutils.exe)复制覆盖到hadoop文件的bin目录下。

下载地址:https://pan.baidu.com/s/1eaEv0KKfbEG790OXgC_A9A

相关文章:

  • 2022-01-06
  • 2021-05-08
  • 2021-09-19
  • 2021-06-22
  • 2021-05-04
  • 2021-08-11
  • 2021-08-20
猜你喜欢
  • 2022-12-23
  • 2021-10-14
  • 2021-05-02
  • 2021-05-22
  • 2021-10-15
相关资源
相似解决方案