0

我一直在尝试执行一些允许我“仅”列出多个文件中存在的单词的代码;到目前为止我所做的是使用 wordcount 示例和感谢 Chris White 我设法编译它。我尝试在这里和那里阅读以使代码正常工作,但我得到的只是一个没有数据的空白页。映射器假设收集每个单词及其对应的位置;减速器应该收集常用词关于可能是什么问题的任何想法?代码是:

    package org.myorg;

import java.io.IOException;
import java.util.*;
import java.lang.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;

public class WordCount {



    public static class Map extends MapReduceBase implements Mapper<Text, Text, Text, Text> 
    {

        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

          private Text outvalue=new Text();
          private String filename = null;

        public void map(Text key, Text value, OutputCollector<Text, Text> output, Reporter reporter) throws IOException 
        {
        if (filename == null) 
        {
          filename = ((FileSplit) reporter.getInputSplit()).getPath().getName();
        }

        String line = value.toString();
        StringTokenizer tokenizer = new StringTokenizer(line);

        while (tokenizer.hasMoreTokens()) 
        {
          word.set(tokenizer.nextToken());
          outvalue.set(filename);
          output.collect(word, outvalue);
        }

        }
    }



    public static class Reduce extends MapReduceBase implements Reducer<Text, Text, Text, Text> 
    {


        private Text src = new Text();
        public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException 
        {


        int sum = 0;
        //List<Text> list = new ArrayList<Text>(); 

            while (values.hasNext()) // I believe this would have all locations of the same word in different files?
            {

                sum += values.next().get();
                src =values.next().get();

            }
        output.collect(key, src);
            //while(values.hasNext()) 
            //{ 
                //Text value = values.next(); 
                //list.add(new Text(value)); 
                //System.out.println(value.toString());       
            //} 
            //System.out.println(values.toString()); 
            //for(Text value : list) 
            //{ 
                //System.out.println(value.toString()); 
            //} 


        }

    }



    public static void main(String[] args) throws Exception 
    {

    JobConf conf = new JobConf(WordCount.class);
    conf.setJobName("wordcount");
    conf.setInputFormat(KeyValueTextInputFormat.class);
    conf.setOutputKeyClass(Text.class);
    conf.setOutputValueClass(Text.class);
    conf.setMapperClass(Map.class);
    conf.setCombinerClass(Reduce.class);
    conf.setReducerClass(Reduce.class);
    //conf.setInputFormat(TextInputFormat.class);
    conf.setOutputFormat(TextOutputFormat.class);
    FileInputFormat.setInputPaths(conf, new Path(args[0]));
    FileOutputFormat.setOutputPath(conf, new Path(args[1]));
    JobClient.runJob(conf);

    }

}

我错过了什么吗?非常感谢...我的 Hadoop 版本:0.20.203

4

2 回答 2

1

首先,您似乎正在使用旧的 Hadoop API (mapred),建议使用与 0.20.203 兼容的新 Hadoop API (mapreduce)

在新的 API 中,这里有一个可以工作的字数

import java.io.IOException;
import java.lang.InterruptedException;
import java.util.StringTokenizer;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {
/**
 * The map class of WordCount.
 */
public static class TokenCounterMapper
    extends Mapper<Object, Text, Text, IntWritable> {

    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map(Object key, Text value, Context context)
        throws IOException, InterruptedException {
        StringTokenizer itr = new StringTokenizer(value.toString());
        while (itr.hasMoreTokens()) {
            word.set(itr.nextToken());
            context.write(word, one);
        }
    }
}
/**
 * The reducer class of WordCount
 */
public static class TokenCounterReducer
    extends Reducer<Text, IntWritable, Text, IntWritable> {
    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));
    }
}
/**
 * The main entry point.
 */
public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    Job job = new Job(conf, "Example Hadoop 0.20.1 WordCount");
    job.setJarByClass(WordCount.class);
    job.setMapperClass(TokenCounterMapper.class);
    job.setReducerClass(TokenCounterReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}  

然后,我们构建这个文件并将结果打包到一个 jar 文件中:

mkdir classes
javac -classpath /path/to/hadoop-0.20.203/hadoop-0.20.203-core.jar:/path/to/hadoop-  0.20.203/lib/commons-cli-1.2.jar -d classes WordCount.java && jar -cvf wordcount.jar -C classes/ .

最后,我们以 Hadoop 的独立模式运行 jar 文件

echo "hello world bye world" > /tmp/in/0.txt
echo "hello hadoop goodebye hadoop" > /tmp/in/1.txt
hadoop jar wordcount.jar org.packagename.WordCount /tmp/in /tmp/out
于 2012-04-14T16:09:25.553 回答
1

在reducer中,维护一组观察到的值(mapper中发出的文件名),如果你消费完所有值后,这个集合大小为1,那么这个词只在一个文件中使用。

public static class Reduce extends MapReduceBase implements Reducer<Text, Text, Text, Text> 
{
    private TreeSet<Text> files = new TreeSet<Text>();

    public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException 
    {
        files.clear();

        for (Text file : values)
        {
            if (!files.contains(value))
            {
                // make a copy of value as hadoop re-uses the object
                files.add(new Text(value));
            }
        }

        if (files.size() == 1) {
            output.collect(key, files.first());
        }

        files.clear();
    }
}
于 2012-04-14T16:52:46.363 回答