2016-08-06 4 views
0

이것은 Mapper 클래스를 확장하는 Hadoop의 Map 클래스 [1]의 예입니다. [3]은 하둡의 Mapper 클래스입니다.Hadoop의 Mapper를 확장하는 클래스를 확장합니다.

을 확장하여 ExampleMapper을 확장하여 hadoop의 Mapper [2]을 확장하려고합니다. 왜냐하면 나는 MyExampleMapper 또는 기타 예제를 만들 때 ExampleMapper을 확장 했으므로 속성을 직접 설정하지 않아도되도록 ExampleMapper에 속성을 설정하기 때문에이 작업을 수행합니다. 이것을 할 수 있습니까?

[1] 예 매퍼

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] 내가 원하는 무엇

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]은 하둡의 매퍼 클래스

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

답변

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

그리고 연장 프로그램에서,

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); 
     } 
    } 
    } 
} 
관련 문제