我正在为以下代码做mapreduce。当我运行此作业时,一切正常。但是输出显示00。我怀疑这可能是由于TryparseInt()方法引起的,因为我之前没有对其进行快速定义。最初没有TryparseInt()方法。所以我创建了一个,任何人都可以检查代码是否正确,尤其是TryParseInt方法是否正确,并告诉我任何建议可以成功运行此程序。

输入看起来像:

提前致谢

    import java.io.IOException;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.conf.Configuration;
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.Reducer;
import org.apache.hadoop.io.LongWritable;


public class MaxPubYear {

public static class MaxPubYearMapper extends Mapper<LongWritable , Text, IntWritable,Text>
    {
        public void map(LongWritable key, Text value , Context context)

                throws IOException, InterruptedException
                {
            String delim = "\t";
            Text valtosend = new Text();
            String tokens[] = value.toString().split(delim);
            if (tokens.length == 2)
            {
                valtosend.set(tokens[0] + ";"+ tokens[1]);
                context.write(new IntWritable(1), valtosend);
            }

                }
    }


public static class MaxPubYearReducer extends Reducer<IntWritable ,Text, Text, IntWritable>
    {

        public void reduce(IntWritable key, Iterable<Text> values , Context context) throws IOException, InterruptedException
        {
            int maxiValue = Integer.MIN_VALUE;
            String maxiYear = "";
            for(Text value:values)          {
                String token[] = value.toString().split(";");
                if(token.length == 2 && TryParseInt(token[1]).intValue()> maxiValue)
                {
                    maxiValue = TryParseInt(token[1]);
                    maxiYear = token[0];
                }
            }
            context.write(new Text(maxiYear), new IntWritable(maxiValue));
        }
    }

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

            Configuration conf = new Configuration();
            Job Job = new Job(conf, "Maximum Publication year");
            Job.setJarByClass(MaxPubYear.class);

            Job.setOutputKeyClass(Text.class);
            Job.setOutputValueClass(IntWritable.class);

            Job.setMapOutputKeyClass(IntWritable.class);
            Job.setMapOutputValueClass(Text.class);

            Job.setMapperClass(MaxPubYearMapper.class);
            Job.setReducerClass(MaxPubYearReducer.class);

            FileInputFormat.addInputPath(Job,new Path(args[0]));
            FileOutputFormat.setOutputPath(Job,new Path(args[1]));
            System.exit(Job.waitForCompletion(true)?0:1);

        }

public static Integer TryParseInt(String string) {
    // TODO Auto-generated method stub
    return(0);
}
    }

最佳答案

这些错误正好说明了他们所说的:对于三个“无法解析为类型”错误,您很可能忘记了导入正确的类。错误2只是表示您必须在其中创建类TryParseInt(String)的类中没有方法MaxPubYear.MaxPubYearReducer

关于java - 无法识别该程序中的错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25618406/

10-14 09:54
查看更多