【问题标题】:hadoop compilinghadoop 编译
【发布时间】:2013-03-30 02:58:06
【问题描述】:

我是 hadoop 新手。我从网上得到了这段代码

import java.io.IOException;
import java.util.*;

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 Gender {

   private static String genderCheck = "female";

   public static class Map extends MapReduceBase implements Mapper {
       private final static IntWritable one = new IntWritable(1);
       private Text locText = new Text();

       public void map(LongWritable key, Text value, OutputCollector output, Reporter reporter) throws IOException {
           String line = value.toString();
           String location = line.split(",")[14] + "," + line.split(",")[15];
           long male = 0L;
           long female = 0L;
           if (line.split(",")[17].matches("\d+") && line.split(",")[18].matches("\d+")) {
               male = Long.parseLong(line.split(",")[17]);
               female = Long.parseLong(line.split(",")[18]);
           }
           long diff = male - female;
           locText.set(location);
           if (Gender.genderCheck.toLowerCase().equals("female") && diff < 0) {
               output.collect(locText, new LongWritable(diff * -1L));
           }
           else if (Gender.genderCheck.toLowerCase().equals("male") && diff
> 0) {
               output.collect(locText, new LongWritable(diff));
           }
       }    }

   public static void main(String[] args) throws Exception {
       JobConf conf = new JobConf(Gender.class);
       conf.setJobName("gender");
       conf.setOutputKeyClass(Text.class);
       conf.setOutputValueClass(LongWritable.class);
       conf.setMapperClass(Map.class);

       if (args.length != 3) {
           System.out.println("Usage:");
           System.out.println("[male/female] /path/to/2kh/files /path/to/output");
           System.exit(1);
       }

       if (!args[0].equalsIgnoreCase("male") && !args[0].equalsIgnoreCase("female")) {
           System.out.println("first argument must be male or female");
           System.exit(1);
       }
       Gender.genderCheck = args[0];

       conf.setInputFormat(TextInputFormat.class);
       conf.setOutputFormat(TextOutputFormat.class);
       FileInputFormat.setInputPaths(conf, new Path(args[1]));
       FileOutputFormat.setOutputPath(conf, new Path(args[2]));
       JobClient.runJob(conf);    }

}

当我使用“javac -cp /usr/local/hadoop/hadoop-core-1.0.3.jar Gender.java”编译这段代码时

得到以下错误:

"Gender.Map 不是抽象的,不会覆盖抽象方法 地图(java.lang.Object,java.lang.Object,org.apache.hadoop.mapred.OutputCollector,org.apache.hadoop.mapred.Reporter) 在 org.apache.hadoop.mapred.Mapper 公共静态类 Map 扩展 MapReduceBase 实现 Mapper"

如何正确编译?

【问题讨论】:

    标签: hadoop


    【解决方案1】:

    改变地图类如下:

    public static class Map extends MapReduceBase implements Mapper <Input key, Input value, Output Key , Output Value>
    

    在您的情况下,输入键为LongWritable,输入值为Text,输出键为Text,输出值为LongWritable

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

    【讨论】:

      【解决方案2】:

      改变class Maper类声明如下:

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

      如果你不指定任何具体的类名,你需要有如下的map函数:

      @Override
      public void map(Object arg0, Object arg1, OutputCollector arg2, Reporter arg3) throws IOException {
          // TODO Auto-generated method stub
      }
      

      现在,特定类型在这里表示预期的输入键值对类型和映射器的输出键值类型。

      在您的情况下,输入键值对是LongWritable-Text

      而且,根据您的 output.collect 方法调用猜测,您的映射器输出键值对是 Text-LongWritable

      因此,您的 Map 类应实现 Mapper&lt;LongWritable,Text,Text, LongWritable&gt;

      您的代码中还有一个错误 -

      使用"\d+" 将不会编译,因为\d 没有任何意义,在反斜杠之后它需要一个特殊的转义序列,所以我猜你应该可以使用以下方法: line.split(",")[17].matches("\\d+")

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-09
        • 2013-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多