双MapReduce框架文件倒排索引案例(案例三)

需求

创建三个文件,放在同一文件夹下,求文件中每个单词在各个文件中出现的频率,输出格式为:
I a.txt 1,b.txt 1,c.txt 1
amin a.txt 1,c.txt 1
coco a.txt 1
hello a.txt 2,b.txt 2,c.txt 2

需求分析

Map1—>I-a.txt 1
I-b.txt 1
I-c.txt 1
Reduce1—>amin-a.txt 1
amin-c.txt 1
coco-a.txt 2
hello-a.txt 2
hello-b.txt 2
Map2—>amin a.txt 1
amin c.txt 1
coco a.txt 2
Reduce—>I a.txt 1,b.txt 1,c.txt 1
amin a.txt 1,c.txt 1
coco a.txt 1
hello a.txt 2,b.txt 2,c.txt 2

源代码

第一个MapReduce

public class CreateIndexOne {
    public static class MapTask extends Mapper<LongWritable, Text, Text, IntWritable>{
        String pathName = null;
        @Override
        protected void setup(Mapper<LongWritable, Text, Text, IntWritable>.Context context)
                throws IOException, InterruptedException {
            //获取文件名
            FileSplit fileSplit = (FileSplit) context.getInputSplit();
            pathName = fileSplit.getPath().getName();
        }
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
                throws IOException, InterruptedException {
            String[] split = value.toString().split(" ");
            for (String word : split) {
                context.write(new Text(word+"-"+pathName), new IntWritable(1));
            }

        }
    }

    public static class ReduceTask extends Reducer<Text, IntWritable, Text, IntWritable>{
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values,
                Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            int count = 0;
            for (IntWritable intWritable : values) {
                count++;
            }
            context.write(key, new IntWritable(count));
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf,"index1");

        job.setMapperClass(MapTask.class);
        job.setReducerClass(ReduceTask.class);
        job.setJarByClass(CreateIndexOne.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.addInputPath(job, new Path("D:\\a\\b"));
        FileOutputFormat.setOutputPath(job, new Path("D:\\a\\index1-out"));


        //判断文件是否存在
        File file = new File("D:\\a\\index1-out");
        if(file.exists()){
            FileUtils.deleteDirectory(file);
        }

        boolean completion = job.waitForCompletion(true);
        System.out.println(completion?"你很优秀!":"调bug");
    }
}

第二个MapReduce

public class CreateIndexTwo {
    public static class MapTask extends Mapper<LongWritable, Text, Text, Text>{

        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
                throws IOException, InterruptedException {
            String[] split = value.toString().split("-");
            String word = split[0];
            String num = split[1];
            context.write(new Text(word), new Text(num));

        }
    }

    public static class ReduceTask extends Reducer<Text, Text, Text, Text>{
        @Override
        protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context)
                throws IOException, InterruptedException {
            StringBuilder sb = new StringBuilder();
            /*boolean flag = true;
            for (Text text : values) {
                if(flag){
                    sb.append(text.toString());
                    flag = false;
                }else{
                    sb.append(",");
                    sb.append(text.toString());
                }
            }*/
            for (Text num : values) {
                sb.append(num.toString()).append(",");
            }
            context.write(key, new Text(sb.deleteCharAt(sb.length()-1).toString()));
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf,"index2");

        job.setMapperClass(MapTask.class);
        job.setReducerClass(ReduceTask.class);
        job.setJarByClass(CreateIndexTwo.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        FileInputFormat.addInputPath(job, new Path("D:\\a\\index1-out"));
        FileOutputFormat.setOutputPath(job, new Path("D:\\a\\index2-out"));


        //判断文件是否存在
        File file = new File("D:\\a\\index2-out");
        if(file.exists()){
            FileUtils.deleteDirectory(file);
        }

        boolean completion = job.waitForCompletion(true);
        System.out.println(completion?"你很优秀!":"调bug");
    }


}

Map切片流程图

这里写图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值