2014-09-13 47 views
0

我得到這個奇怪的錯誤。我已經寫了一個wordCount程序來計算單詞在文件中重複的次數。MapReduce程序圖任務超時

因此,當我在hadoop上運行MR代碼時,代碼卡在「Map 100%,Reduce 0%」上。 基本模式是第一個地圖任務在600秒後超時,然後再次發生並且該作業自殺。

我檢查了Job Tracker,並且任務被卡住了,因爲Map任務沒有完成Reduce任務的啓動。

我一直在嘗試修復它2天,在此期間我刪除了原來的虛擬Ubuntu Cloudera並再次安裝它 - 所以我們可以確定它不是配置問題。

任何幫助表示讚賞。

以下是3個代碼文件。

WordCount.java

public class WordCount extends Configured implements Tool { 

@Override 
public int run(String[] args) throws Exception { 

    Configuration conf = super.getConf(); 

    Job job=new Job(conf, "Word Count Job"); 
    job.setJarByClass(WordCount.class); 

    job.setMapperClass(WordMapper.class); 
    job.setReducerClass(WordReducer.class); 

    job.setOutputKeyClass(Text.class); 
    job.setOutputValueClass(FloatWritable.class); 

    job.setInputFormatClass(TextInputFormat.class); 
    job.setOutputFormatClass(TextOutputFormat.class); 

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

    job.waitForCompletion(Boolean.TRUE); 

    return 0; 
} 

public static void main(String[] args) { 

    //Display error message in case insufficient arguments supplied 
    if(args.length<2){ 
     System.out.println("usage: WordCount <Input-Path> <Output-Path>"); 
    } 

    Configuration conf=new Configuration(Boolean.TRUE); 

    int i; 
    try { 
     //Run the overridden 'run' method code 
     i = ToolRunner.run(conf, new WordCount(), args); 

     //Print usage stats to out 
     //ToolRunner.printGenericCommandUsage(System.out); 

     //exit if job cannot start 
     System.exit(i); 

    } catch (Exception e) { 

     e.printStackTrace(); 

     System.exit(-1); 
    } 
} 
} 

WordMapper.java

public class WordMapper extends Mapper<LongWritable, Text, Text, FloatWritable> { 

@Override 
protected void map(LongWritable key, 
     Text value, 
     Mapper<LongWritable, Text, Text, FloatWritable>.Context context) 
     throws IOException, InterruptedException { 


    if(!value.toString().trim().isEmpty()){ 

     StringTokenizer valTokens = new StringTokenizer(value.toString()); 

     while(valTokens.hasMoreTokens()){ 
      context.write(new Text(valTokens.nextToken()), new FloatWritable(Float.parseFloat("1.00"))); 
     } 
    } 
} 
} 

WordReducer.java

public class WordReducer extends Reducer<Text, FloatWritable, Text, FloatWritable> { 

@Override 
protected void reduce(Text key, Iterable<FloatWritable> values, 
     Reducer<Text, FloatWritable, Text, FloatWritable>.Context context) 
     throws IOException, InterruptedException { 

    Iterator<FloatWritable> valsIter = values.iterator(); 
    int i = 0; 

    while(valsIter.hasNext()) 
     i++; 

    context.write(key, new FloatWritable((float)i)); 
} 
} 
+0

我剛試過這個,它解決了,但我希望有人能解釋它。我將_WordMapper.java_中的'while'循環更改爲_enhanced for循環__並且工作正常。 – theRealDarthVader 2014-09-13 16:41:31

+0

這突出了單元測試的重要性!你浪費了你自己的時間,陌生人,並將這個問題誤解爲Hadoop的事情,僅僅因爲你太花時間花10分鐘寫一個單元測試。 Tsk tsk tsk – samthebest 2014-09-14 09:54:25

回答

2

您的問題是在這行代碼:

while(valsIter.hasNext()) 
    i++; 

valsIter.hasNext檢查迭代器中是否存在下一個元素,但不移動指針的位置。因此檢查總是返回true。除非你調用valsIter.next()。

+0

是的,幫助,謝謝@Venkat – theRealDarthVader 2014-09-14 04:29:53

+0

@RaviChandraTimmavajjula你應該把這個標記爲已回答。 – Venkat 2014-09-19 15:21:30