统计多个文件夹中单词出现的次数和所在文件夹。
第一步:统计出每个文件夹中单词出现次数。
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
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.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.log4j.BasicConfigurator;
import java.io.File;
import java.io.IOException;
public class InveseIndexOne {
public static class InveseIndexOneMap extends Mapper<LongWritable, Text,Text, IntWritable>{
String FileName = null;
Text k = new Text();
@Override
protected void setup(Context context) throws IOException, InterruptedException {
//获取文件名
FileSplit inputSplit = (FileSplit) context.getInputSplit();
FileName = inputSplit.getPath().getName();
}
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] split = value.toString().split(" ");
for (String word:split) {
k.set(word + "-" + FileName);
context.write(k,new IntWritable(1));
}
}
}
public static class InveseIndexOneReduce extends Reducer<Text,IntWritable,Text,IntWritable>{
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
Integer count = 0;
for (IntWritable v:values) {
count++;
}
context.write(key,new IntWritable(count));
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
BasicConfigurator.configure();//自动快速使用缺省log4j的环境
Configuration conf = new Configuration();
// conf.set("yarn.resorcemanager.hostname","192.168.72.110");
// conf.set("fs.deafutFS", "hdfs://192.168.72.110:9000/");
Job job = Job.getInstance(conf);
job.setJarByClass(InveseIndexOne.class);
job.setMapperClass(InveseIndexOneMap.class);
job.setReducerClass(InveseIndexOneReduce.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// job.setInputFormatClass(TextInputFormat.class);
// job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.setInputPaths(job,new Path("D:\\eclipse\\wc\\input11"));
FileOutputFormat.setOutputPath(job,new Path("D:\\eclipse\\wc\\output-count"));
job.submit();
boolean b = job.waitForCompletion(true);
System.exit(b?0:1);
}
}
第二步:以第一步结果为起始,统计单词在每个文件夹中出现了几次。
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import java.io.IOException;
public class InveseIndexTwo {
public static class InveseIndexTwoMap extends Mapper<LongWritable, Text,Text,Text>{
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] split = value.toString().split("-");
String word = split[0];
String index = split[1];
context.write(new Text(word),new Text(index));
}
}
public static class InveseIndexTwoReduce extends Reducer<Text,Text,Text,Text>{
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
// 线程不安全的StringBulider效率更高,在此处并不涉及线程安全问题
StringBuilder builder = new StringBuilder();
for (Text c:values) {
builder.append(c).append(",");
}
context.write(key,new Text(builder.toString()));
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
// conf.set("yarn.resorcemanager.hostname","192.168.72.110");
// conf.set("fs.deafutFS", "hdfs://192.168.72.110:9000/");
Job job = Job.getInstance(conf);
job.setJarByClass(InveseIndexTwo.class);
job.setMapperClass(InveseIndexTwoMap.class);
job.setReducerClass(InveseIndexTwoReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.setInputPaths(job,new Path("D:\\eclipse\\wc\\output-count"));
FileOutputFormat.setOutputPath(job,new Path("D:\\eclipse\\wc\\output-"));
job.submit();
boolean b = job.waitForCompletion(true);
System.exit(b ? 0 : 1);
}
}