지도

2017-09-19 4 views
0

내가 내 자신에 의해 빅 데이터 하둡을 배우고 작동하지 않는 드라이버 코드를 줄이고 나는 간단한지도 호야를 작동하지 단어 개수에 대한 코드를 절감 썼다는 모습을지도

// importing all classes 

public class WordCount { 

public static class Map extends 
     Mapper<LongWritable, Text, Text, IntWritable> { 
    public void map(LongWritable key, Text value, Context context) 
      throws IOException, InterruptedException { 
     String Line = value.toString(); 
     StringTokenizer tokenizer = new StringTokenizer(Line); 
     while (tokenizer.hasMoreTokens()) { 
      value.set(tokenizer.nextToken()); 
      context.write(value, new IntWritable(1)); 
     } 
    } 
} 

public static class Reduce extends 
     Reducer<Text, IntWritable, Text, IntWritable> { 

    public void reduce(Text key, Iterable<IntWritable> values, 
      Context context) throws IOException, InterruptedException { 
     int sum = 0; 
     for (IntWritable x : values) { 
      sum = sum + x.get(); 
     } 
     context.write(key, new IntWritable(sum)); 
    } 

} 

public static void main(String[] args) throws Exception { 
    Configuration conf = new Configuration(); 
    Job job = new Job(conf, "Word Count"); 
    job.setJarByClass(WordCount.class); 
    job.setMapperClass(Map.class); 
    job.setReducerClass(Reduce.class); 
    job.setOutputKeyClass(Text.class); 
    job.setOutputValueClass(IntWritable.class); 
    job.setInputFormatClass(TextInputFormat.class); 
    job.setOutputFormatClass(TextOutputFormat.class); 

    FileInputFormat.addInputPath(job, new Path(args[0])); 
    FileOutputFormat.setOutputPath(job, new Path(args[1])); 

} 

} 

그러나 교체 한 후이 있습니다 이

Path outputPath = new Path(args[1]); 

    FileInputFormat.addInputPath(job, new Path(args[0])); 
    FileOutputFormat.setOutputPath(job, new Path(args[1])); 

    outputPath.getFileSystem(conf).delete(outputPath); 

    System.exit(job.waitForCompletion(true) ? 0 : 1); 

에서 드라이버 코드에서이 줄

FileInputFormat.addInputPath(job, new Path(args[0])); 
FileOutputFormat.setOutputPath(job, new Path(args[1])); 

는이 법과 적절하게 ks.

이유와 그 내용이 무엇인지 알 수 있습니다.

답변

0

당신이 오류가 아래 얻는 것, 그래서 내가, 당신이 말할 때 그것이 작동하지 않는 가정입니다 : -지도하기 전에 작업을 줄일 존재하지 않아야

org.apache.hadoop.mapred.FileAlreadyExistsException: **Output directory hdfs://localhost:54310/<<your_output_directory>> already exists** 

출력 디렉토리가 제출됩니다. 그래서 그것은 당신에게 위의 예외를 주었을 것입니다.

드라이버에서 사용한 새로운 코드 줄은 경로에서 파일 시스템 (conf/object 기반의 로컬/hdfs)을 가져 와서 맵 축소 작업을 제출하기 전에 출력 경로를 삭제합니다. 이제 출력 디렉토리가 존재하지 않기 때문에 작업이 실행됩니다.

+0

고맙습니다. .. 코드를 실행하기 전에 출력 디렉토리를 수동으로 삭제하면 작동합니다. –

+0

마지막 행의 의미.이 행은 System.exit (job.waitForCompletion (true)? 0 : 1); 이 줄이 없으면 프로그램이 실행되지도 않고 오류가 표시되지 않기 때문에이 줄을 사용해야합니다. –

+0

map reduce 작업의 실행을 시작하고 완료를 기다립니다. 작업 실행이 완료되면 Java 프로그램을 종료 할 System.exit에 코드를 리턴합니다. 네,이 라인을 가져야합니다. – SagarKhandagale

0

이전에 어떤 오류가 발생했는지 알려주실 수 있습니까? 이 오류는 출력 경로가 이미 존재할 수 있습니다. 여기에는 매번 출력 경로를 삭제하는 코드가 있습니다. 출력 경로가 이미 있으면 해당 경로가 삭제됩니다. 이 코드를 작성할 수 있습니다.

Path outputPath = new Path(args[1]); 
if (outputPath.getFileSystem(conf).exist()) { 
outputPath.getFileSystem(conf).delete(outputPath); 
} 
+0

예, 경로가 이미 있습니다. 그러나 새 디렉토리를 만들고 출력 디렉토리로 전달할 때도 동일한 오류가 발생합니다. –