使用 MapReduce 实现倒排索引

编程要求

根据提示,在右侧编辑器的中的 begin-end 间补全 InvertIndex_origin 类中的 map 和 reduce 函数。具体实现如下。

读取 hdfs 中/input目录下的如下三个文件,文件内容如下:

file1.txt内容:

mapreduce is simple

file2.txt内容:

mapreduce is powerful and simple

file3.txt内容:

mapreduce and mapreduce

使用 mapreduce 处理后把结果输出到 hdfs 的/out目录下,预期输出内容如下:

and file3.txt:1;file2.txt:1;is file2.txt:1;file1.txt:1;mapreduce file1.txt:1;file2.txt:1;file3.txt:2;powerful file2.txt:1;simple file2.txt:1;file1.txt:1;

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.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.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;



public class InvertIndex_origin {

    public static class Map extends Mapper<Object, Text, Text, Text> {
        private Text keyInfo = new Text(); // 存储单词和URL组合
        private Text valueInfo = new Text(); // 存储词频
        private FileSplit split; // 存储Split对象
        // 实现map函数
        @Override
        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            // 获得<key,value>对所属的FileSplit对象
            split = (FileSplit) context.getInputSplit();
            StringTokenizer itr = new StringTokenizer(value.toString());
                /**********Begin**********/
            String pathname = split.getPath.getName();
            while(itr.hasMoreTokens()) {
                keyInfo.set(itr.nextToken()+":"+pathname);
                valueInfo.set("1");
                context.write(keyInfo,valueInfo);
            }
                /**********End**********/
        }
    }

    public static class Combine extends Reducer<Text, Text, Text, Text> {
        private Text info = new Text();
        // 实现reduce函数, 将相同key值的value加起来
        // 并将(单词:文件名, value) 转换为 (单词, 文件名:value)
        @Override
        public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            /**********Begin**********/

            // 统计词频
            int sum = 0;
            for(Text val : values) {
                sum += Integer.parseInt(val.toString());
            }

            String splitkey = key.toString().split(":");
            // 重新设置value值由URL和词频组成
            Text valueInfo = new Text(splitkey[1]+":"+String.valueOf(sum));


            // 重新设置key值为单词
            Text keyInfo = new Text(splitkey[0]);

            context.write(keyInfo,valueInfo);
            /**********End**********/

        }
    }

    public static class Reduce extends Reducer<Text, Text, Text, Text> {
        private Text result = new Text();
        // 实现reduce函数, 将相同单词的value聚合成一个总的value,每个value之间用`;`隔开, 最后以`;`结尾
        @Override
        public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
                /**********Begin**********/
                String valueInfo = "";
            for(Text val : values) {
                valueInfo += val.toString()+";";
            }
            context(key,new Text(valueInfo));
                /**********End**********/

        }
    }

    public static void main(String[] args) throws Exception {
        // 第一个参数为 输入文件目录路径, 第二个参数为输出结果路径
        Configuration conf = new Configuration();

        if (args.length != 2) {
            System.err.println("Usage: Inverted Index <in> <out>");
            System.exit(2);
        }

        Job job = new Job(conf, "Inverted Index");
        job.setJarByClass(InvertIndex_origin.class);

        // 设置Map、Combine和Reduce处理类
        job.setMapperClass(Map.class);
        job.setCombinerClass(Combine.class);
        job.setReducerClass(Reduce.class);

        // 设置Map输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        // 设置Reduce输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        // 设置输入和输出目录
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

results matching ""

    No results matching ""