2016-08-06 8 views
0

Dies ist ein Beispiel für eine Map-Klasse [1] aus dem Hadoop, die die Mapper-Klasse erweitert. [3] ist die Hadoop-Mapper-Klasse.Erweitert eine Klasse, die den Hadoop-Mapper erweitert

Ich möchte meine MyExampleMapper erstellen, die die ExampleMapper erweitert, die auch die Hadoop [2] erweitert. Ich mache das, weil ich nur eine Eigenschaft in der ExampleMapper setzen möchte, so dass, wenn ich die MyExampleMapper oder andere Beispiele erstellen, muss ich nicht die Eigenschaft selbst einstellen, weil ich die ExampleMapper erweitert habe. Ist es möglich, dies zu tun?

[1] Beispiel Mapper

import org.apache.hadoop.mapreduce.Mapper; 

public class ExampleMapper 
    extends Mapper<Object, Text, Text, IntWritable>{ 

    private final static IntWritable one = new IntWritable(1); 
    private Text word = new Text(); 

    public void map(Object key, Text value, Context context) throws IOException, InterruptedException { 
    StringTokenizer itr = new StringTokenizer(value.toString()); 
    while (itr.hasMoreTokens()) { 
     word.set(itr.nextToken()); 
     context.write(word, one); 
    } 
    } 
} 

[2] Was ich will,

import org.apache.hadoop.mapreduce.Mapper; 

public class MyExampleMapper 
    extends ExampleMapper<Object, Text, Text, IntWritable>{ 

    private final static IntWritable one = new IntWritable(1); 
    private Text word = new Text(); 

    public void map(Object key, Text value, Context context) throws IOException, InterruptedException { 
    StringTokenizer itr = new StringTokenizer(value.toString()); 

    String result = System.getProperty("job.examplemapper") 

    if (result.equals("true")) { 
     while (itr.hasMoreTokens()) { 
     word.set(itr.nextToken()); 
     context.write(word, one); 
     } 
    } 
    } 
} 


public class ExampleMapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT> 
    extends Mapper{ 

    System.setProperty("job.examplemapper", "true"); 
} 

[3] Dies ist die Mapper Klasse Hadoop

public class Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT> { 
    public Mapper() { 
    } 

    protected void setup(Mapper.Context context) throws IOException, InterruptedException { 
    } 

    protected void map(KEYIN key, VALUEIN value, Mapper.Context context) throws IOException, InterruptedException { 
     context.write(key, value); 
    } 

    protected void cleanup(Mapper.Context context) throws IOException, InterruptedException { 
    } 

    public void run(Mapper.Context context) throws IOException, InterruptedException { 
     this.setup(context); 

     try { 
      while(context.nextKeyValue()) { 
       this.map(context.getCurrentKey(), context.getCurrentValue(), context); 
      } 
     } finally { 
      this.cleanup(context); 
     } 

    } 

    public class Context extends MapContext<KEYIN, VALUEIN, KEYOUT, VALUEOUT> { 
     public Context(Configuration var1, TaskAttemptID conf, RecordReader<KEYIN, VALUEIN> taskid, RecordWriter<KEYOUT, VALUEOUT> reader, OutputCommitter writer, StatusReporter committer, InputSplit reporter) throws IOException, InterruptedException { 
      super(conf, taskid, reader, writer, committer, reporter, split); 
     } 
    } 
} 

Antwort

2
import org.apache.hadoop.mapreduce.Mapper; 

public class ExampleMapper<T, X, Y, Z> extends Mapper<T, X, Y, Z> { 
    static { 
     System.setProperty("job.examplemapper", "true"); 
    } 
} 

Dann verlängern es, in Ihrem Programm

public class MyExampleMapper 
    extends ExampleMapper<Object, Text, Text, IntWritable>{ 

    private final static IntWritable one = new IntWritable(1); 
    private Text word = new Text(); 

    public void map(Object key, Text value, Context context) throws IOException, InterruptedException { 
    StringTokenizer itr = new StringTokenizer(value.toString()); 

    String result = System.getProperty("job.examplemapper") 

    if (result.equals("true")) { 
     while (itr.hasMoreTokens()) { 
     word.set(itr.nextToken()); 
     context.write(word, one); 
     } 
    } 
    } 
}