概述
本问讲述在win10下,使用eclipse开发统计单词的hadoop程序。
创建工程
创建map/reduce工程wordcount
过程略。
新建测试类MyWordCount
package hdp.test;
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 MyWordCount {
public static class WordCountMapper extends Mapper<Object, Text, Text, IntWritable> {
private final IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer stn = new StringTokenizer(value.toString());
while (stn.hasMoreTokens()) {
word.set(stn.nextToken());
context.write(word, one);
}
}
}
public static class WordCountReducer 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 IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
String[] cliArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (cliArgs.length != 2) {
System.err.println("Usage: mywordcount <in> <out>");
System.exit(2);
}
Job myJob = Job.getInstance(conf, "My first job");
myJob.setJarByClass(MyWordCount.class);
myJob.setMapperClass(WordCountMapper.class);
myJob.setReducerClass(WordCountReducer.class);
myJob.setCombinerClass(WordCountReducer.class);
myJob.setOutputKeyClass(Text.class);
myJob.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(myJob, new Path(cliArgs[0]));
FileOutputFormat.setOutputPath(myJob, new Path(cliArgs[1]));
boolean isSucced = myJob.waitForCompletion(true);
System.out.println("result:" + isSucced);
System.exit(isSucced ? 0: 1);
}
}
代码说明:
数组String[] cliArgs其实是配置的运行参数:
[hdfs://localhost:9000/testdir/input, hdfs://localhost:9000/testdir/out]
设置WordCount运行参数
右键wordcount工程,Run As->Run Configurations,填入参数:
hdfs://localhost:9000/testdir/input
hdfs://localhost:9000/testdir/out
如下图:
配置运行时的input和output两个参数,我这里把本地文件上传到了input目录(已存在),hdfs目录作为输出,其中out目录在hdfs中不存在,如果已经存在则先删除,或使用其他名字,否则运行时会报错。
运行程序
运行方法同运行一般Java程序,运行成功会打印出:result:true,结果如下图示:
查看out目录下的part-r-00000文件:
结果分析:
因为input目录下有一个文件,其内容为:
hello hadoop hello hadoop
wordcount程序会统计这个文件里的所有单词。
遇到的错误
错误1:
Exception in thread "main" org.apache.hadoop.mapred.FileAlreadyExistsException: Output directory hdfs://localhost:9000/testdir/out already exists
at org.apache.hadoop.mapreduce.lib.output.FileOutputFormat.checkOutputSpecs(FileOutputFormat.java:146)
at org.apache.hadoop.mapreduce.JobSubmitter.checkSpecs(JobSubmitter.java:266)
at org.apache.hadoop.mapreduce.JobSubmitter.submitJobInternal(JobSubmitter.java:139)
at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1290)
at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1287)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Unknown Source)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1758)
at org.apache.hadoop.mapreduce.Job.submit(Job.java:1287)
at org.apache.hadoop.mapreduce.Job.waitForCompletion(Job.java:1308)
at hdp.test.MyWordCount.main(MyWordCount.java:66)
程序执行成功后,再次执行,会报错。
原因:out目录已存在。
解决:删除out目录,重新执行程序:
D:\develop\hadoop-2.7.6\bin>hadoop fs -rm -r hdfs://localhost:9000/testdir/out
18/07/07 19:10:37 INFO fs.TrashPolicyDefault: Namenode trash configuration: Deletion interval = 0 minutes, Emptier interval = 0 minutes.
Deleted hdfs://localhost:9000/testdir/out
错误2:
Exception in thread "main" org.apache.hadoop.mapreduce.lib.input.InvalidInputException: Input path does not exist: hdfs://localhost:9000/testdir/input
at org.apache.hadoop.mapreduce.lib.input.FileInputFormat.singleThreadedListStatus(FileInputFormat.java:323)
at org.apache.hadoop.mapreduce.lib.input.FileInputFormat.listStatus(FileInputFormat.java:265)
at org.apache.hadoop.mapreduce.lib.input.FileInputFormat.getSplits(FileInputFormat.java:387)
at org.apache.hadoop.mapreduce.JobSubmitter.writeNewSplits(JobSubmitter.java:301)
at org.apache.hadoop.mapreduce.JobSubmitter.writeSplits(JobSubmitter.java:318)
at org.apache.hadoop.mapreduce.JobSubmitter.submitJobInternal(JobSubmitter.java:196)
at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1290)
at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1287)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Unknown Source)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1758)
at org.apache.hadoop.mapreduce.Job.submit(Job.java:1287)
at org.apache.hadoop.mapreduce.Job.waitForCompletion(Job.java:1308)
at hdp.test.MyWordCount.main(MyWordCount.java:66)
原因:input目录不存在导致。
解决方法: 创建input目录
D:\develop\hadoop-2.7.6\bin>hadoop fs -mkdir hdfs://localhost:9000/testdir/input