hadoop的DistributedCache
DistributedCache類主要用來設定文件,jar等。其的思想就是將指定的文件(必須在hdfs文件系統上面)在每一個task運行的地方都複製一份數據,所以在map或reduce函數中讀取DistributedCache設定的文件就可以在每一個task運行上的機器本地讀取,而不用再走網絡了。
下面是一個例子:
1.jobRun裏面
JobConf jobConf = new JobConf(xxx.class);
DistributedCache.createSymlink(jobConf);//這個地方設定纔會成功
String path = args[3];//path的路徑要像:/lll/llx 或 lll/llx(此處是相對路徑,不要./lll/llx然後用new URI(path),這樣會導致map或reduce函數中找不到文件)
Path filePath = new Path(path);//爲hadoop中的類
String uriWithLink = filePath.toUri().toString() + "#" + "ex.txt";//uri非常重要,否則會發生文件找不到的情況
System.out.println("uriWithLink:" + uriWithLink);
try {
DistributedCache.addCacheFile(new URI(uriWithLink), jobConf);
} catch (URISyntaxException e) {
e.printStackTrace();
}
2.map或reduce裏面
FileReader fr = new FileReader(cacheFile);//cacheFile="ex.txt"
BufferedReader bf = new BufferedReader(fr);
String line = null ;
while( (line = bf.readLine() ) != null){
System.out.println(line);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.