2013-04-04 57 views
-1

假設我在Reducer代碼中檢測到輸入鍵/值中的某些內容,應該實際運行哪些代碼以便reducer不再繼續,輸出中的任何已發出記錄都將寫入輸出文件和作業停止不再進一步減少發生?在Reducer代碼中以編程方式停止作業

回答

1

停止工作可能不是一個好主意。 但是,如果你需要它,一種方法是創建你自己的異常類,或許延伸InterruptedExceptionIOException,並且當你想要退出時,只要出現這種情況就拋出異常。

你的異常類可能如下:

@Override 
protected void reduce(Text key, Iterable values, Context context) throws IOException,InterruptedException { 
     ... 
     if(<condition to quit happen>){ 
      throw new QuitReducerException("Quitting reducer due to some specified reason");// You may add details of the reason you are quitting and this will be available in the job logs (in stderr) 
     } 
     ... 
    } 


PS:這不能保證

Class QuitReducerException extends InterruptedException { 

     //Parameterless Constructor 
     public QuitReducerException() {} 

     //Constructor that accepts a message 
     public QuitReducerException(String message) 
     { 
     super(message); 
     } 
} 

而在你的減少方法,你可以按如下使用由電流減速器發出的輸出將被承諾輸出文件。另外任何其他還未完成的reducer都不會提交這些文件。雖然已經完成的減員已經完成了他們的產出。

1

這可能是多個reducers必須在您的hadoop集羣上運行的情況。因此,即使您在輸入中檢測到錯誤並嘗試停止輸入,也不能確定狀態是否一致(即,一旦收到錯誤輸入,就不會處理記錄),因爲多個記錄可能會並行處理多個記錄。

所以我不認爲這是停止工作的好主意。

+1

+1,我同意這不是一個好主意,但在某些情況下可能需要。 – Amar 2013-04-04 19:16:54