[英]Inverted list in Map Reduce Hadoop
我正在嘗試修改此代碼以生成完整的反向列表。 我的意思是,獲取文件位置中每個單詞的索引。 也就是說,如果我們有兩個文件包含
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中做到這一點。 有幫助嗎?
關於您的問題的幾點思考。
輸入格式:
TextInputFormat使用輸入文件的每一行作為輸入記錄。 因此,您應該使用輸入格式,該格式提供對整個文件的訪問,作為一個輸入記錄。 例如,您可以使用此WholeFileRecordReader 。
制圖員:
映射器應返回有關輸入記錄中每個單詞的信息。 返回鍵是單詞,返回值是包含輸入文件和當前單詞在文件中位置的任何結構。 您可以編寫自己的Writable類或將此信息合並到字符串中,然后像現在一樣返回Text類。
減速器:
Reducer應該合並每個單詞的信息。 只需用一個鍵循環遍歷傳遞給reducer的所有值,然后以您描述的格式生成結果字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.