Java Hadoop Mapreduce:未调用Reduce方法

Java Hadoop Mapreduce:未调用Reduce方法,java,hadoop,mapreduce,reduce,Java,Hadoop,Mapreduce,Reduce,我正在写一个mapreduce算法 在我的代码中,不调用reduce(文本键、Iterable值、上下文)方法。在它上面我有一个@Override给出了一个错误:方法没有从它的超类中重写方法 这是我的密码: package WordCountP; import java.io.FileReader; import java.io.IOException; import java.util.Iterator; import org.apache.hadoop.conf.Configured;

我正在写一个mapreduce算法

在我的代码中,不调用
reduce(文本键、Iterable值、上下文)
方法。在它上面我有一个
@Override
给出了一个错误:
方法没有从它的超类中重写方法

这是我的密码:

package WordCountP;

import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
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.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;


public class popularity extends Configured implements Tool{

    public class PopularityMapper extends Mapper<Text, Text, Text, Text> {

        @Override
        protected void map(Text key, Text value,
                           Context context)
                throws IOException, InterruptedException {

                JSONParser jsonParser = new JSONParser();
                try {
                    JSONObject jsonobject = (JSONObject) jsonParser.parse(new FileReader("src\\testinput.json"));
                    JSONArray jsonArray = (JSONArray) jsonobject.get("votes");

                    Iterator<JSONObject> iterator = jsonArray.iterator();
                    while(iterator.hasNext()) {
                        JSONObject obj = iterator.next();
                        String song_id_rave_id = (String) obj.get("song_ID") + "|" + (String) obj.get("rave_ID");
                        String preference = (String) obj.get("preference");
                        System.out.println(song_id_rave_id + "||" + preference);
                        context.write(new Text(song_id_rave_id), new Text(preference));
                    }
                }catch(ParseException e) {
                    e.printStackTrace();
                }
        }

    }

    public class PopularityReducer extends Reducer<Text, Iterable<String>, Text, Text> {

        @Override
        protected void reduce(Text key, Iterable<String> values, Context context)
                throws IOException, InterruptedException {

            int sum = 0;
            for ( String val: values){
                if (val == "true"){
                    sum +=1;
                }
                else if (val == "false"){
                    sum -=1;
                }

            }
            String result = Integer.toString(sum);
            context.write(new Text(key), new Text(result));
        }
    }



    public static void main(String[] args) throws Exception{
        int exitCode = ToolRunner.run(new popularity(), args);
        System.exit(exitCode);
    }



    public int run(String[] args) throws Exception {
        if (args.length != 2) {
            System.err.printf("Usage: %s [generic options] <input> <output>\n",
                    getClass().getSimpleName());
            ToolRunner.printGenericCommandUsage(System.err);
            return -1;
        }

        Job job = new org.apache.hadoop.mapreduce.Job();
        job.setJarByClass(popularity.class);
        job.setJobName("PopularityCounter");

        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        job.setMapperClass(PopularityMapper.class);
        job.setReducerClass(PopularityReducer.class);

        int returnValue = job.waitForCompletion(true) ? 0:1;
        System.out.println("job.isSuccessful " + job.isSuccessful());
        return returnValue;
    }
}

提前谢谢

同时检查IOException和InterruptedException,因此不会重写reduce方法

类Reducer中的方法reduce不会引发任何异常,因此您不能声明子类中的方法reduce会引发任何选中的异常,但它可以引发未选中的异常


您可能希望在reduce方法中处理异常。

最后,我们选择了MongoBD-mapreduce

Hadoop太难了


谢谢

我试图在reduce方法中调整异常的位置,但这并没有解决问题。为什么这是一个问题?因为map()方法也会在override方法中抛出异常@TNelen是的,所以你也需要在map方法中做同样的更改,我相信当创建对象时,会使用父类引用,因此它会在父类中调用reduce和map方法,而不是你创建的方法。代码没有编译,也不是说没有调用该方法。。。一旦它运行,映射器是否工作正常?您没有覆盖默认的inputformat,因此地图类型不正确。我将reduce方法输入格式更改为,错误消失。不确定代码是否正常工作,我现在要测试。String不是writeable的子类,因此也不应该工作。默认映射器输入必须是
LongWritable,Text
我知道了!这确实是我的映射器输入中的一个错误
{"votes":[{
    "song_ID": "Piece of your heart",
    "mbr_ID": "001",
    "preference": "true",
    "timestamp": "11:22:33",
    "rave_ID": "rave001",
    },
    {
    "song_ID": "Piece of your heart",
    "mbr_ID": "002",
    "preference": "true",
    "timestamp": "11:22:33",
    "rave_ID": "rave001",
    },
    {
    "song_ID": "Atje voor de sfeer",
    "mbr_ID": "001",
    "preference": "false",
    "timestamp": "11:44:33",
    "rave_ID": "rave001",
    },
    {
    "song_ID": "Atje voor de sfeer",
    "mbr_ID": "002",
    "preference": "false",
    "timestamp": "11:44:33",
    "rave_ID": "rave001",
    },
    {
    "song_ID": "Atje voor de sfeer",
    "mbr_ID": "003",
    "preference": "true",
    "timestamp": "11:44:33",
    "rave_ID": "rave001",
    }]
}