지도

2017-09-20 12 views
0

2 개 파일의 내용을 가입 절감 사용하여 여러 파일의 결합 :지도

첫 번째 파일 (포함 직원 이름 데이터)

id,name 
101,Gaurav 
102,Rohit 
103,Karishma 
104,Darshan 
105,Divya 

두 번째 파일 (포함 직원 부서 데이터)

id,dept 
101,Sales 
102,Research 
103,NMG 
104,Admin 
105,HR 

=====================

,210

출력

id,name,dept 
101,Gaurav,Sales 
102,Rohit,Research 
103,Karishma,NMG 

어떻게 출력의이 종류를 acheive합니까?

지금부터 난 내가 ID, 이름, 부서와 같은 지정된 순서로 출력

.. 감속기 등의 출력을 랜덤 값을 원하는군요. 도움을 주시면 감사하겠습니다.

매퍼 클래스는 ...

public class JoinReducer extends Reducer<Text, Text, NullWritable, Text> { 
String merge = ""; 
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { 
    merge = key.toString(); // 101 
    for(Text value : values) { 
     merge += "," + value.toString(); 
    } 
    context.write(NullWritable.get(), new Text(merge)); 
} 
} 

드라이버 클래스는 다음과 같습니다 ...

public class JoinMapper extends Mapper<LongWritable, Text, Text, Text> { 
private Text keyEmit = new Text(); 
private Text valEmit = new Text(); 
public void map(LongWritable k, Text value, Context context) throws IOException, InterruptedException 
{ 
String line=value.toString(); 
String[] words=line.split(","); 
keyEmit.set(words[0]); 
valEmit.set(words[1]); 
context.write(keyEmit, valEmit); 
} 
} 

감속기 클래스는 다음과 같습니다 ... 다음과 같습니다

public class JoinDriver { 
public final static void main(final String[] args) throws Exception { 
    Configuration conf = new Configuration(); 
    Job job = new Job(conf, "Multiple join"); 

    job.setJarByClass(JoinDriver.class); 
    // job.setMapperClass(JoinMapper.class); 
    job.setReducerClass(JoinReducer.class); 

    MultipleInputs.addInputPath(job, new Path(args[0]), 
      TextInputFormat.class, JoinMapper.class); 

    MultipleInputs.addInputPath(job, new Path(args[1]), 
      TextInputFormat.class, JoinMapper.class); 

    job.setMapOutputKeyClass(Text.class); 
    job.setMapOutputValueClass(Text.class); 
    job.setOutputKeyClass(Text.class); 
    job.setOutputValueClass(Text.class); 
    job.setOutputFormatClass(TextOutputFormat.class); 
    FileOutputFormat.setOutputPath(job, new Path(args[2])); 
    System.exit(job.waitForCompletion(true) ? 0 : 1); 
} 
} 

출력은 다음과 같습니다. 나는 ID, 이름, 부서와 같은 순서대로 원합니다. 당신이 어떤 잘 모릅니다 때문에

output as of now

+0

질문에 코드를 입력해야합니다. –

+0

@BinaryNerd 코드를 확인하십시오. 내가 알기로는 감속기에 입력으로 전송되는 값이 무작위이며,이 특정 경우에 특정 순서로 전송되도록하는 방법입니다. –

답변

0

당신이 가진 가장 큰 문제는 값이 따라서 당신은 공통 키에 그룹화하고 있지만, 단지 문자열로 값을 전송, 분류되지 않습니다 대규모 도움이되지이다 이름과 부서입니다.

당신은 매퍼에서 더 많은 정보를 보낼 필요 모두 몇 가지 옵션이 있습니다

  1. 사용 보조 종류의
  2. 정렬 감속기의 값

가장 빠른 방법을 이렇게하려면 매퍼에서 출력 할 때 더 많은 정보를 값에 추가합니다 (실제로는 두 개의 Text 객체가 포함 된 합성 값을 사용하는 것이 가장 이상적입니다).

public class JoinMapperName extends Mapper<LongWritable, Text, Text, Text> { 
    public void map(LongWritable k, Text value, Context context) 
          throws IOException, InterruptedException { 

     String[] words = value.toString().split(","); 
     context.write(new Text(words[0]), new Text("name:" + words[1])); 
    } 
} 

public class JoinMapperDept extends Mapper<LongWritable, Text, Text, Text> { 
    public void map(LongWritable k, Text value, Context context) 
          throws IOException, InterruptedException { 

     String[] words = value.toString().split(","); 
     context.write(new Text(words[0]), new Text("dept:" + words[1])); 
    } 
} 

이제 각 데이터 원본마다 다른 매퍼가 있습니다. 그리고 감속기를 다음과 같이 변경해야합니다.

public class JoinReducer extends Reducer<Text, Text, NullWritable, Text> { 
    public void reduce(Text key, Iterable<Text> values, Context context) 
       throws IOException, InterruptedException { 

     String name = ""; 
     String dept = ""; 
     for(Text value : values) { 
      if (value.toString().startsWith("name")) { 
       name = value.toString().split(":")[1]; 
      } else { 
       dept = value.toString().split(":")[1]; 
      } 
     } 
     String merge = key + "," name + "," + dept; 
     context.write(NullWritable.get(), new Text(merge)); 
    } 
} 

이것은 어떻게 할 수 있는지에 대한 간단한 예입니다. 잘하면 주문을 시행 할 수있는 방법에 대한 아이디어를 얻을 수 있기를 바랍니다.

+0

고마워요! 그것은 내 문제를 해결했다. –