之前自己有搭过一次,不过是在ubuntu上弄的,然后这次想在windows上弄一个源码阅读环境,说干就干,网上找了一些资料,然后结合自己的情况弄了一套,下面是步骤:
一.下载hadoop的安装包,将其解压缩,然后下载winutils.exe和hadoop.dll将其放在hadoop的bin目录下。
下载地址如下:
链接:https://pan.baidu.com/s/1TnwIbXdVzGWD2qa2saCVjQ
提取码:q5ve
二.在环境变量里面配置hadoop_home以及在path里面添加%hadoop_home%\bin。
三.在idea中添加需要debug的代码,我这里是word count
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class WordCount {
// TokenizerMapper作为Map阶段,需要继承Mapper,并重写map()函数
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
// 用StringTokenizer作为分词器,对value进行分词
StringTokenizer itr = new StringTokenizer(value.toString());
// 遍历分词后结果
while (itr.hasMoreTokens()) {
// 将String设置入Text类型word
word.set(itr.nextToken());
// 将(word,1),即(Text,IntWritable)写入上下文context,供后续Reduce阶段使用
context.write(word, one);
}
}
}
// IntSumReducer作为Reduce阶段,需要继承Reducer,并重写reduce()函数
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
// 遍历map阶段输出结果中的values中每个val,累加至sum
for (IntWritable val : values) {
sum += val.get();
}
// 将sum设置入IntWritable类型result
result.set(sum);
// 通过上下文context的write()方法,输出结果(key, result),即(Text,IntWritable)
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
// 加载hadoop配置
Configuration conf = new Configuration();
conf.set("mapreduce.framework.name","local");
conf.set("fs.defaultFS","file:///");
// 构造一个Job实例job,并命名为"word count"
Job job = new Job(conf, "word count");
// 设置jar
job.setJarByClass(WordCount.class);
// 设置Mapper
job.setMapperClass(TokenizerMapper.class);
// 设置Combiner
job.setCombinerClass(IntSumReducer.class);
// 设置Reducer
job.setReducerClass(IntSumReducer.class);
// 设置OutputKey
job.setOutputKeyClass(Text.class);
// 设置OutputValue
job.setOutputValueClass(IntWritable.class);
// 添加输入路径
FileInputFormat.addInputPath(job, new Path("file:///F:\\hello.txt"));
// 添加输出路径
FileOutputFormat.setOutputPath(job, new Path("file:///F:\\result"));
// 等待作业job运行完成并退出
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
四.在工程中添加jar包:可通过maven添加也可通过咱们下载好的hadoop下载包中share中的jar包(我这里介绍的是第二种,通过hadoop安装包导入jar包到工程中)
通过idea中的project settings的modules中的dependencies,然后点击右边的绿色+号导入jar包。
点开之后选择JARS or directories,然后导入这几个目录下的jar包
五.运行程序,大功告成,可以开始自行debug程序了,如果说中途遇到要绑定源码的提示,那么可以下载一个对应版本的source文件,然后绑定上去。
ps:另外,直接用hadoop对应版本的src包直接创建工程,然后跑src里面自己带的例子,应该也可以debug。