2014-08-28 90 views
0

我看過關於這個主題的其他線程,仍然沒有找到答案......豬訪問分佈式緩存StoreFunc

簡單地說,我想從一個豬StoreFunc訪問Hadoop分佈式高速緩存,以及不是直接在UDF內。

相關PIG行代碼:

DEFINE CustomStorage KeyValStorage('param1','param2','param3'); 
... 
STORE BLAH INTO /path/ using CustomStorage(); 

相關的Java代碼:

public class KeyValStorage<M extends Message> extends BaseStoreFunc /* ElephantBird Storage which inherits from StoreFunc */ { 

... 
public KeyValStorage(String param1, String param2, String param3) { 
    ... 
     try { 
      InputStream is = new FileInputStream(configName); 
      try { 
       prop.load(is); 
      } catch (IOException e) { 
       System.out.println("PROPERTY LOADING FAILED"); 
       e.printStackTrace(); 
      } 
     } catch (FileNotFoundException e) { 
      System.out.println("FILE NOT FOUND"); 
      e.printStackTrace(); 
     } 
    } 
... 
} 

配置名稱是本地文件,我應該能夠從分佈式緩存讀取的名字,但是,我得到一個FileNotFoundException。當我直接在PIG UDF中使用EXACT相同的代碼時,該文件被找到,所以我知道該文件通過分佈式緩存發送。我設置適當的參數,以確保發生這種情況:

<property><name>mapred.cache.files</name><value>/path/to/file/file.properties#configName</value></property> 

任何想法如何解決這個問題?

謝謝!

回答

0

StroreFunc的構造函數被稱爲前端後端。當它從前端被調用時(在作業啓動之前),那麼你會得到FileNotFoundException,因爲此時來自分佈式緩存的文件還沒有被複制到節點的本地磁盤。
您可以檢查您是否在後端(時正在執行的任務),只有在這種情況下加載該文件e.g:

DEFINE CustomStorage KeyValStorage('param1','param2','param3'); 
set mapreduce.job.cache.files hdfs://host/user/cache/file.txt#config 
... 
STORE BLAH INTO /path/ using CustomStorage(); 

public KeyValStorage(String param1, String param2, String param3) { 
    ... 
    try { 
    if (!UDFContext.getUDFContext().isFrontend()) { 
     InputStream is = new FileInputStream("./config"); 
     BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
     ... 
    ... 
}