내가 내 자신에 의해 빅 데이터 하둡을 배우고 작동하지 않는 드라이버 코드를 줄이고 나는 간단한지도 호야를 작동하지 단어 개수에 대한 코드를 절감 썼다는 모습을지도
// 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.
이유와 그 내용이 무엇인지 알 수 있습니다.
고맙습니다. .. 코드를 실행하기 전에 출력 디렉토리를 수동으로 삭제하면 작동합니다. –
마지막 행의 의미.이 행은 System.exit (job.waitForCompletion (true)? 0 : 1); 이 줄이 없으면 프로그램이 실행되지도 않고 오류가 표시되지 않기 때문에이 줄을 사용해야합니다. –
map reduce 작업의 실행을 시작하고 완료를 기다립니다. 작업 실행이 완료되면 Java 프로그램을 종료 할 System.exit에 코드를 리턴합니다. 네,이 라인을 가져야합니다. – SagarKhandagale