本文详述如何在windows开发环境下通过mvn+eclipse构建hadoop项目并运行
必备环境
- windows7操作系统
- eclipse-4.4.2
- mvn-3.0.3及用mvn生成项目架构(参阅http://blog.csdn.net/tang9140/article/details/39157439)
- hadoop-2.5.2(直接上hadoop官网http://hadoop.apache.org/下载hadoop-2.5.2.tar.gz并解压到某个目录)
windows7下环境配置
1、本地hadoop环境配置
添加环境变量HADOOP_HOME=E:\doc_api\ebook\hadoop-2.5.2
追加环境变量path内容:%HADOOP_HOME%\bin
2、bin下增加hadoop.dll,winutils.exe文件
从https://github.com/srccodes/hadoop-common-2.2.0-bin或从……下载hadoop.dll,winutils.exe,放置到${HADOOP_HOME}\bin目录下
构建hadoop项目
下面以经典的WordCount为例,构建我们第一个hadoop项目。
- 引包
pom文件中加入依赖包
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-common</artifactId>
<version>2.5.2</version>
</dependency>
- 编写WordCount类如下
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
/**
* @version 1.0
* @author tangqian
*/
public class WordCount extends Configured implements Tool {
public static void main(String[] args) throws Exception {
int result = ToolRunner.run(new Configuration(),new WordCount(), args);
System.exit(result);
}
@Override
public int run(String[] args) throws Exception {
Path inputPath, outputPath;
if(args.length == 2){
inputPath = new Path(args[0]);
outputPath = new Path(args[1]);
}else{
System.out.println("usage <input> <output>");
return 1;
}
Configuration conf = getConf();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, inputPath);
FileOutputFormat.setOutputPath(job, outputPath);
return job.waitForCompletion(true) ? 0 : 1;
}
public static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable value : values) {
sum += value.get();
}
result.set(sum);
context.write(key, result);
}
}
}
然后在该类上右键Run As->Run Configurations->Arguments标签的Program arguments中指定输入路径和输出路径如下:
file:///e:/word.txt file:///e:/hadoop/result2
点Run即可运行该类,此时可在Console看到输出信息。等完成后,可到e:/hadoop/result2看到结果文件part-r-00000内容如下
is 1
test 2
this 1
two 1
说明:由于是在本地hadoop单机模式下运行,故采用本地文件系统(以file://开头指定输入输出路径)。
附
hadoop-2.5.2集群安装指南(参阅http://blog.csdn.net/tang9140/article/details/42869531)
如何修改Windows7下的hosts文件?
hosts文件一般在C:\Windows\System32\drivers\etc目录下,在windows7下如果不是管理员身份登录,可能无权限修改,此时可右键hosts文件->属性->安全->编辑,选择当前登录用户,开放修改权限即可,具体操作如下图。