【问题标题】:Inverted list in Map Reduce HadoopMap Reduce Hadoop 中的倒排列表
【发布时间】:2017-10-01 02:28:16
【问题描述】:

我正在尝试修改此代码以生成完整的倒排列表。我的意思是,获取文件位置中每个单词的索引。也就是说,如果我们有两个包含单词的文件

  abc.txt =    I am coming to the park to play, yes i am.

  def.txt = Please come on over, i will be waiting for you

我应该有这样的东西:

i /home/abc.txt: 1 10 /home/def.txt: 5

这意味着字母 i 是文件 abc.txt 中的第 1 个和第 10 个单词,以及文件 def.txt 中的第 5 个单词

我已修改代码以提供“词位置和词频”,如下所示:

import java.io.IOException;
import java.util.*;

import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.lib.output.*;
import org.apache.hadoop.util.*;

public class WordCountByFile extends Configured implements Tool {

    public static void main(String args[]) throws Exception {
        String[] argsLocal = {
            "input#2", "output#2"
        };
        int res = ToolRunner.run(new WordCountByFile(), argsLocal);
        System.exit(res);
    }

    public int run(String[] args) throws Exception {
        Path inputPath = new Path(args[0]);
        Path outputPath = new Path(args[1]);

        Configuration conf = getConf();
        Job job = new Job(conf, this.getClass().toString());

        FileInputFormat.setInputPaths(job, inputPath);
        FileOutputFormat.setOutputPath(job, outputPath);

        job.setJobName("WordCountByFile");
        job.setJarByClass(WordCountByFile.class);
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        job.setMapperClass(Map.class);
        job.setCombinerClass(Reduce.class);
        job.setReducerClass(Reduce.class);

        return job.waitForCompletion(true) ? 0 : 1;
    }

    public static class Map extends Mapper < LongWritable, Text, Text, IntWritable > {
        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            StringTokenizer tokenizer = new StringTokenizer(line);
            while (tokenizer.hasMoreTokens()) {

                String filePathString = ((FileSplit) context.getInputSplit()).getPath().toString();

                word.set(tokenizer.nextToken() + " " + filePathString + " : ");
                context.write(word, one);
            }
        }
    }

    public static class Reduce extends Reducer < Text, IntWritable, Text, IntWritable > {

        @Override
        public void reduce(Text key, Iterable < IntWritable > values, Context context) 
            throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable value: values) {
                sum += value.get();
            }
            context.write(key, new IntWritable(sum));
        }
    }
}

我知道它必须与 Java 中的一些索引一起使用,但我试图弄清楚如何在 Hadoop Map Reduce 中做到这一点。有帮助吗?

【问题讨论】:

    标签: java python hadoop


    【解决方案1】:

    关于您的问题的一些想法。

    输入格式:

    TextInputFormat 使用输入文件的每一行作为输入记录。因此,您应该使用提供对整个文件的访问作为一个输入记录的输入格式。例如,您可以使用此WholeFileRecordReader

    映射器:

    Mapper 应该返回输入记录中每个单词的信息。返回键是单词,返回值是包含输入文件和文件中当前单词位置的任何结构。您可以编写自己的 Writable 类或将此信息合并到字符串并像现在一样返回 Text 类。

    减速机:

    Reducer 应该合并每个单词的信息。只需用一个键遍历所有传递给 reducer 的值,并以您描述的格式生成结果字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-07
      • 2011-07-21
      • 2014-03-22
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      相关资源
      最近更新 更多