Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Map Reduce Hadoop中的倒排列表_Java_Python_Hadoop - Fatal编程技术网

Java Map Reduce Hadoop中的倒排列表

Java Map Reduce Hadoop中的倒排列表,java,python,hadoop,Java,Python,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文件中的第一个和

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

  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文件中的第一个和第十个单词,是def.txt文件中的第五个单词

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

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));
        }
    }
}
import java.io.IOException;
导入java.util.*;
导入org.apache.hadoop.conf.*;
导入org.apache.hadoop.fs.*;
导入org.apache.hadoop.io.*;
导入org.apache.hadoop.mapreduce.*;
导入org.apache.hadoop.mapreduce.lib.input.*;
导入org.apache.hadoop.mapreduce.lib.output.*;
导入org.apache.hadoop.util.*;
公共类WordCountByFile扩展配置的实现工具{
公共静态void main(字符串args[])引发异常{
字符串[]argsLocal={
“输入#2”,“输出#2”
};
int res=ToolRunner.run(新的WordCountByFile(),argsLocal);
系统退出(res);
}
公共int运行(字符串[]args)引发异常{
路径输入路径=新路径(args[0]);
路径outputPath=新路径(args[1]);
配置conf=getConf();
Job Job=新作业(conf,this.getClass().toString());
setInputPath(作业,inputPath);
setOutputPath(作业,outputPath);
job.setJobName(“WordCountByFile”);
job.setJarByClass(WordCountByFile.class);
setInputFormatClass(TextInputFormat.class);
setOutputFormatClass(TextOutputFormat.class);
job.setMapOutputKeyClass(Text.class);
setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(Map.class);
job.setCombinerClass(Reduce.class);
job.setReducerClass(Reduce.class);
返回作业。waitForCompletion(true)?0:1;
}
公共静态类映射扩展了映射器{
私有最终静态IntWritable one=新的IntWritable(1);
私有文本字=新文本();
公共void映射(LongWritable键、文本值、上下文上下文)引发IOException、InterruptedException{
字符串行=value.toString();
StringTokenizer标记器=新的StringTokenizer(行);
while(tokenizer.hasMoreTokens()){
字符串filePathString=((FileSplit)context.getInputSplit()).getPath().toString();
set(tokenizer.nextToken()+“”+filePathString+“:”);
上下文。写(单词,一);
}
}
}
公共静态类Reduce扩展Reducer{
@凌驾
public void reduce(文本键,Iterable值,上下文)
抛出IOException、InterruptedException{
整数和=0;
for(可写入值:值){
sum+=value.get();
}
write(key,newintwriteable(sum));
}
}
}

我知道它必须与Java中的一些索引一起使用,但我正在尝试在Hadoop Map Reduce中解决这个问题。有人帮你吗?

只是想一想你的问题

输入格式:

TextInputFormat使用每一行输入文件作为输入记录。因此,您应该使用输入格式,将整个文件作为一个输入记录进行访问。例如,您可以使用这个

映射器:

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

减速器:

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