最近是用hadoop遇到些需求,要想抽離出reduce裏面的某個算法包,從而能夠動態改動算法
(1)如何動態調用抽離出的算法包Jar
這裏就涉及到URLClassLoader的東西
loader = new URLClassLoader(new URL[]{
new URL(jarFile)
}, Thread.currentThread().getContextClassLoader());
Class<? extends Commander> myClass = (Class<? extends Commander>) loader.loadClass("com.out.CommanderImpl");
cmd = myClass.newInstance();
當然這裏有個前提,就是要讓算法包和調用者都是用同一個接口來實現實例化和調用。
(2)如何在hadoop的MR上實現
MR要讀取文件,當然是放在hdfs集羣上,所以需要調用hdfs://xxxx的路徑。
因爲URL解析需要protocol,而原始的Java是沒有hdfs解析協議的,所以需要加上一句
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
這個就放在static{}語句塊裏執行就好了
(3)如何告訴MR我的算法包url位置
現在url如果寫死在程序,是可以直接調用的,但是如果想要動態存入,就需要傳入參數。
想要在 Job和Mapper/Reducer之間傳遞參數,需要用到Conf.set() Conf.get()
OK,萬事具備,MR寫好,動態更新算法包就可以了。
附上幾個有用的鏈接:
http://blog.csdn.net/wawmg/article/details/17961815
http://blog.csdn.net/wisgood/article/details/17080207