1

我的输出中有很多重复的值,所以我实现了一个 reduce 函数,如下所示,但是这个 reduce 仍然作为一个标识函数工作,即即使我有一个 reduce,输出也没有区别。我的 reduce 功能有什么问题?

       public class search 
{      
    public static String str="And";
    public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, Text> 
    {
        String mname="";
        public void configure(JobConf job)
        {
             mname=job.get(str);
             job.set(mname,str);
        }

        private Text word = new Text();
        public Text Uinput =new Text("");
        public void map(LongWritable key, Text value, OutputCollector<Text, Text> output, Reporter reporter) throws IOException 
        {

            String mapstr=mname;
            Uinput.set(mapstr);
            String line = value.toString();
            Text fdata = new Text();

            StringTokenizer tokenizer = new StringTokenizer(line);
            while (tokenizer.hasMoreTokens())
            {
                word.set(tokenizer.nextToken());
                fdata.set(line);

                if(word.equals(Uinput))
                output.collect(fdata,new Text(""));
            }

        }
    } 

    public static class SReducer extends MapReduceBase implements Reducer<Text, Text, Text, Text> 
    {
        public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException 
        {

            boolean start = true;
            //System.out.println("inside reduce   :"+input);
            StringBuilder sb = new StringBuilder();
            while (values.hasNext()) 
            {
                if(!start)

                start=false;
                sb.append(values.next().toString());

            }
            //output.collect(key, new IntWritable(sum));
            output.collect(key, new Text(sb.toString()));
        }
    }

公共静态 void main(String[] args) 抛出异常 {

    JobConf conf = new JobConf(search.class);
    conf.setJobName("QueryIndex");
    //JobConf conf = new JobConf(getConf(), WordCount.class);
    conf.set(str,args[0]);

    conf.setOutputKeyClass(Text.class);
    conf.setOutputValueClass(Text.class);

    conf.setMapperClass(Map.class);
    //conf.setCombinerClass(SReducer.class);
    conf.setReducerClass(SReducer.class);

    conf.setInputFormat(TextInputFormat.class);
    conf.setOutputFormat(TextOutputFormat.class);



    FileInputFormat.setInputPaths(conf, new Path("IIndexOut"));
    FileOutputFormat.setOutputPath(conf, new Path("searchOut"));

    JobClient.runJob(conf);
}

}

4

3 回答 3

1

我没有仔细看代码,但我可以确定的一件事是布尔变量start在这里没有用,下面的代码if (!start)应该放在括号中以删除数据,否则你最终只会写所有您从 mapper 收到的 reducer 中的数据。

 public static class SReducer extends MapReduceBase implements Reducer<Text, Text, Text, Text> 
{
    public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException 
    {

        boolean start = true;
        //System.out.println("inside reduce   :"+input);
        StringBuilder sb = new StringBuilder();
        while (values.hasNext()) 
        {
            if(!start)
            {
               start=false;
               sb.append(values.next().toString());
            }

        }
        //output.collect(key, new IntWritable(sum));
        output.collect(key, new Text(sb.toString()));
    }
}

或者一个最佳的减少方法就是: -

public static class SReducer extends MapReduceBase implements Reducer<Text, Text, Text, Text> 
  {
  public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException 
{

   //System.out.println("inside reduce   :"+input);
    StringBuilder sb = new StringBuilder();
    sb.append(values.next().toString());

    //output.collect(key, new IntWritable(sum));
    output.collect(key, new Text(sb.toString()));
}

}

因为您只关心迭代器的第一个值。

于 2012-04-27T00:14:29.630 回答
0

也许你还没有将这个 reducer 设置为要使用的实际 reduce 函数?这是使用完成的

job.setReducerClass(). 

如果您不将类设置为您的类,则使用默认减速器。您应该执行以下操作:

job.setReducerClass(SReducer.class)

请发布您的主要功能,以便我们验证。

于 2012-04-26T20:02:15.663 回答
0

在 map 和 reduce 函数之前使用 @override 注释。这样您就可以非常确定,您正在覆盖基类方法。

于 2013-08-20T21:46:17.687 回答