HadoopIntellijPlugin 插件还没安装好的可以参考这篇文章,IntelliJ IDEA搭建Hadoop开发环境(上),安装好插件后,下一步就是导入 hadoop 的依赖包,这些包可以在 hadoop 的 share/hadoop 目录下找到,这里以经典的 WordCount 程序来进行演示
1、新建 maven 项目
输入 GroupId 和 ArtifactId,然后 Next --> Finsh
2、新建class
名字输入 org.apache.hadoop.examples.WordCount
将下面代码复制过去
package org.apache.hadoop.examples;
import java.io.IOException;
import java.util.Iterator;
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 WordCount() {
}
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
private static final IntWritable one = new IntWritable(1);
private Text word = new Text();
public TokenizerMapper() {
}
public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value