目录
用Eclipse的配置可以参考 win10 Eclipse上配置Hadoop
0 上传被统计的文件
通用代码:
start-all.cmd // 启动Hadoop
hadoop fs -put [本地文件路径] [hdfs上的路径] // 上传文件
hadoop fs -put D:\word.txt /user/root/input // 举栗子
1 编码
(相关代码在文末)
①新建项目:File->new->other->Map/Reduce Project(不要选成java project啦)
②在src目录下新建一个package,Package下创建java类 WordCount.java
③WordCount.java写入统计代码 WordCount代码
④同时要在src目录下新建一个log4j.properties文件,避免报错。没找到这个后缀,可以先在桌面新建TXT文件,然后改名,再剪切到项目src目录下。 log4j代码
2 运行
①项目右键→Run as→Run Configurations→Java Application(双击新建)
② 如下面两张图配置,最后Apply→run
3 结果展示
PS:可以看到还是有一些问题,比如英文中的.?!之类的符号没有被识别出来。可以改进一下。
4 代码
参考:windows10上使用Eclipse配置Hadoop开发环境详细步骤+WordCount示例
WordCount代码
// WordCount.java
package 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;
import org.apache.hadoop.util.GenericOptionsParser;
public class WordCount {
// Map 映射
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); // 写入文档
}
}
}
// Reduce 合并统计
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);
}
}
// main
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> <out>");
System.exit(2);
}
@SuppressWarnings("deprecation")
Job job = new Job(conf, "word count"); // 创建新job
job.setJarByClass(WordCount.class);
// map/reduce
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(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
log4j代码
// log4j.properties
// 可以先在桌面新建TXT文件,然后改名,再剪切到项目src目录下。
# Configure logging for testing:optionally with log file
#log4j.rootLogger=debug,appender
log4j.rootLogger=info,appender
#log4j.rootLogger=error,appender
#\u8F93\u51FA\u5230\u63A7\u5236\u53F0
log4j.appender.appender=org.apache.log4j.ConsoleAppender
#\u6837\u5F0F\u4E3ATTCCLayout
log4j.appender.appender.layout=org.apache.log4j.TTCCLayout