Hadoop Mapreduce: метод уменьшения не вызывается - PullRequest
0 голосов
/ 19 октября 2019

Я пишу карту уменьшения алогирта.

В моем коде метод reduce(Text key, Iterable<String> values, Context context) не вызывается. выше этого у меня есть @Override, выдающий ошибку: Method does not override method from its superclass.

Вот мой код:

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;
    }
}

Я пытался назвать его с помощью R caps, (Reduce()) но это тоже не сработало. Я предполагаю, что в аргументах, приведенных к методу, есть ошибка, но я не вижу там никаких проблем ... Есть идеи?

Во-вторых, есть ли способ, которым я могу установить формат вывода, например, .txt файл?

К вашему сведению, мой входной код JSON

{"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",
    }]
}


Спасибозаранее, авансом!

Ответы [ 2 ]

0 голосов
/ 14 ноября 2019

В конце концов, мы пошли на карту MongoBD.

Hadoop - слишком сложная задача.

Спасибо!

0 голосов
/ 19 октября 2019

Проверяются как IOException, так и InterruptedException, поэтому метод Reduce не переопределяется.

Метод Reduce в классе Reducer не выдает никаких исключений, и, следовательно, вы не можете объявить, что метод Reduce в вашем подклассе вызывает любые проверенныеисключения, но он может генерировать непроверенные исключения.

Возможно, вы захотите обработать исключение внутри метода Reduce.

...