2017-11-03 1 views
0

2 개의 열이 탭으로 구분 된 데이터 세트에서 2 개의 키를 만들려고합니다. 1 키/값을 만드는 방법을 알고 있지만 키/값의 두 번째 쌍을 만드는 방법을 모르겠습니다. 본질적으로 각 열에 대해 키/값을 만들고 싶습니다. 그런 다음 감속기 부분에서 각 키 수의 차이를 가져옵니다. 수도, 내가 데이터를 감속기에 매퍼에서 전달되는 부분에 뭔가를했다고 생각Mapreduce Mapper는 감속기 계산을 위해 2 개의 키를 만듭니다.

public static class IntSumReducer 
     extends Reducer<Text,IntWritable,Text,IntWritable> { 
     private IntWritable result = new IntWritable(); 

     public void reduce(Text key, Iterable<IntWritable> values, 
          Context context 
          ) throws IOException, InterruptedException { 

      int sum0 = 0; 
      for (IntWritable val : values) { 
       sum0 += val.get() 
      } 
      int sum1 = 0; 
      for (IntWritable val : values) { 
       sum1 += val.get() 
      } 
      diff = sum0 - sum1; 
      result.set(diff); 
      context.write(key, diff); 
     } 
    } 

: 여기

public static class MyMapper extends Mapper<Object, Text, Text, IntWritable>{ 

     private IntWritable one = new IntWritable(1); 
     private Text nodeX = new Text(); 

     public void map(Object key, Text value, Context context 
         ) throws IOException, InterruptedException { 
      String[] data = value.toString().split("\\t"); 
      String node0 = data[0]; 
      String node1 = data[1]; 
      StringTokenizer itr = new StringTokenizer(data); 
      while(itr.hasMoreTokens()){ 
       nodeX.set(node0); 
       context.write(nodeX, one) 
       nodeY.set(node1); 
       context.write(nodeY, one) 
     } 
    } 

감속기 것 : 여기

내가 매퍼 부분이 무엇 2 개의 키가 필요합니다. Java를 처음 사용하고 올바르게 가져 오는 방법을 모릅니다.

123 543 
123 234 
543 135 
135 123 

그리고 나는 COL1 키와 COL2 키의 발생 합의 차이를 데려 갈거야 어디 출력이되고 싶습니다 :

내 입력 데이터는 다음과 같습니다.

123 1 
543 0 
135 0 
234 -1 

답변

0

단어로 줄을 나누고 단어가 숫자가되게하고 차이를 계산한다고 생각합니다. NLineInputFormat 키를 행 번호로 사용할 수 있으며, 값을 분할하고 계산합니다. 그렇지 않으면. 행 번호를 기록하기 위해 정적 long 형식을 정의 할 수 있습니다.

public static class TokenizerMapper extends 
     Mapper<LongWritable, Text, LongWritable, IntWritable> 
     { 

    private IntWritable diffen = new IntWritable(); 
    private static long row_num= 0; 

    public void map(LongWritable key, Text value, Context context) 
      throws IOException, InterruptedException { 
     String[] data = value.toString().split("\\t"); 
     String node0 = data[0]; 
     String node1 = data[1]; 
     int dif = Integer.parseInt(node1)-Integer.parseInt(node0); 
      diffen.set(dif); 
      row_num++; 
      context.write(new LongWritable(row_num), diffen); 
    } 
} 
당신은 또한이 개 부분에 reduce 및 분할에 값을 작성하고 다른 .ALL 괜찮 계산할 수

;

+0

dif에 대한 계산이 내 문제를 해결하는지 확실하지 않습니다. 방정식은 행별로 차이를 계산합니다. 여기서 키로 차이를 계산해야합니다. 나는 당신의 코드를 잘못 이해하지 않는 한. – ajax2000

관련 문제