这里使用的是Hadoop 2.7.1版本,其他版本应该也大致通用
使用maven可以很轻松地配置
新建一个Maven项目,然后在pom.xml中添加如下依赖项后,update即可
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.1</version>
</dependency>
</dependencies>
update指的是:右键点击项目->Maven(大致在最下边)-> Update Project
如果你改完pom后,eclipse自动update了,就直接进行下一步即可
当然,你也可以下载Hadoop后,手动import这些包
直接新建一个WordCount类即可,然后类里面的代码如下(直接用的官方提供的2.7.1版本的example):
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;
import org.apache.hadoop.util.GenericOptionsParser;
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();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length < 2) {
System.err.println("Usage: wordcount <in> [<in>...] <out>");
System.exit(2);
}
Job job = Job.getInstance(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);
for (int i = 0; i < otherArgs.length - 1; ++i) {
FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
}
FileOutputFormat.setOutputPath(job,
new Path(otherArgs[otherArgs.length - 1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
如果想对本机的文件进行word count,直接在run configuration里面如下配置即可:
关于run configuration,点击Toolbar里面那个Run的图标旁边的向下的三角,就会出现了
这里最后一个参数是输出文件夹的路径,前面的是若干个input文件的路径
配置好直接运行就ok了
首先,你要配置好并启动namenode,可以参看我的上篇文章
将你要处理的文件放到HDFS中,比如:
cd {Hadoop的根目录}
bin\hdfs dfs -mkdir /input
bin\hdfs dfs -put etc/hadoop/*.xml /input
这样就把你的Hadoop的配置文件,放到了你HDFS中的/input目录
然后修改run configuration中的argument
即文件路径都需要加上前缀hdfs://localhost:9000
比如:hdfs://localhost:9000/input/capacity-scheduler.xml
配置好直接运行即可