环境要求
1、jdk1.8(Hadoop官方推荐1.7)
2、intellij idea
3、windows 下需要安装x64 cygwin,否则会出现如下错误:
Cannot run program "chmod": CreateProcess error=2
解决方法:到cygwin官网下载setup-x86_64.exe
安装之后,把bin目录配置到windows的环境变量path中,记得重启intellij idea。
本教程不需要安装任何模式的Hadoop。
WordCount
这里以Hadoop的官方示例程序WordCount为例,演示如何一步步编写程序直到运行。
项目搭建
使用idea新建一个普通maven项目
添加pom依赖
这里只需要用到基础依赖hadoop-core和hadoop-common;如果需要读写HDFS,则还需要依赖hadoop-hdfs和hadoop-client;如果需要读写HBase,则还需要依赖hbase-client。
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
注意:hadoop-core使用1.2.1版本会出现以下错误:
Failed to set permissions of path: \tmp\ .staging to 0700
解决方法:
- 1、hadoop-core的版本换成0.20.2版本,相应的main方法里面的 Job job = Job.getInstance(conf, "word count");也要改成Job job = new Job(conf, "word count");就可以了
- 2、自己下载1.2.1源码包(官网已经没有此源码包),把其中的报错那行给注掉,然后打包,再使用。(未验证)
新建WordCount类
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 {
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 itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
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;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
// Job job = Job.getInstance(conf, "word count");
Job job = new Job(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
此代码来自Hadoop官方教程,出处见参考。
新建input文件夹
配置运行参数
在Intellij菜单栏中选择Run->Edit Configurations,在弹出来的对话框中点击+,新建一个Application配置。配置Main class为WordCount(可以点击右边的...选择),Program arguments为input/ output/,即输入路径为刚才创建的input文件夹,输出为output。
运行
上述配置完成后,点击菜单栏Run->Run 'WordCount'即开始运行此MapReduce程序,Intellij下方会显示Hadoop的运行输出。待程序运行完毕后,Intellij左上方会出现新的文件夹output,其中的part-r-00000就是运行的结果了!
由于Hadoop的设定,下次运行时务必删除output文件夹!
参考资料
Hadoop: Intellij结合Maven本地运行和调试MapReduce程序 (无需搭载Hadoop和HDFS环境)