新聞中心
setup 函數(shù)原碼:(摘自《hadoop實戰(zhàn)》)
*Called once at the start of the task.
protected void setup(Context context) throws IOException,InterruptedException{}
從注釋可得知,setup函數(shù)在Task啟動時就調(diào)用。
在MapReduce中作業(yè)會被組織成MapTask和ReduceTask。
每個Task都以Map類或Reduce類為處理方法主體,
輸入分片為處理方法的輸入,自己的分片處理完后Task就銷毀了。
從這里看出,setup函數(shù)在task啟動后數(shù)據(jù)處理前就調(diào)用一次
而覆蓋的Map函數(shù)和Reduce函數(shù)會針對輸入分片的每個Key調(diào)用一次,
所以setup函數(shù)可以看作Task上一個全局處理。
利用setup函數(shù)的特性,可以將Map或Reduce函數(shù)中的的重復(fù)處理放到setup函數(shù)中。
如老師給的Exercise_2中的"name"
但需要注意的是,調(diào)用setup函數(shù)只是對應(yīng)的Task上全局操作,而不是整個作業(yè)的全局操作。
創(chuàng)新互聯(lián)建站服務(wù)緊隨時代發(fā)展步伐,進行技術(shù)革新和技術(shù)進步,經(jīng)過十余年的發(fā)展和積累,已經(jīng)匯集了一批資深網(wǎng)站策劃師、設(shè)計師、專業(yè)的網(wǎng)站實施團隊以及高素質(zhì)售后服務(wù)人員,并且完全形成了一套成熟的業(yè)務(wù)流程,能夠完全依照客戶要求對網(wǎng)站進行網(wǎng)站建設(shè)、網(wǎng)站制作、建設(shè)、維護、更新和改版,實現(xiàn)客戶網(wǎng)站對外宣傳展示的首要目的,并為客戶企業(yè)品牌互聯(lián)網(wǎng)化提供全面的解決方案。
可以先用api把本地的文件傳到hdfs中的 /user/hadoop/test 里去
//本地文件上傳到HDFS上
public static void upload(String src,String dst) throws FileNotFoundException,IOException{
InputStream in = new BufferedInputStream(new FileInputStream(src));
//得到配置對象
Configuration conf = new Configuration();
//文件系統(tǒng)
FileSystem fs = FileSystem.get(URI.create(dst), conf);
//輸出流
OutputStream out = fs.create(new Path(dst), new Progressable() {
public void progress() {
System.out.println("上傳完一個設(shè)定緩存區(qū)大小容量的文件!");
}
});
//連接兩個流,形成通道,使輸入流向輸出流傳輸數(shù)據(jù)
IOUtils.copyBytes(in, out, 4096,true);
}
上傳的時候調(diào)用這個函數(shù)就可以了
例如
upload("/home/jack/test/test.txt","/user/hadoop/test/test");
前面的是本地目錄中的文件,后面是hdfs中的文件
注意 必須兩者都必須是“路徑+文件名” 不能沒有文件名
Configuration conf = new Configuration();
conf.setStrings("job_parms", "aaabbc"); //關(guān)鍵就是這一句
Job job = new Job(conf, "load analysis");
job.setJarByClass(LoadAnalysis.class);
job.setMapperClass(LoadMapper.class);
job.setReducerClass(LoadIntoHbaseReduce.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
@Override
protected void setup(Context context)
throws IOException, InterruptedException {
try {
//從全局配置獲取配置參數(shù)
Configuration conf = context.getConfiguration();
String parmStr = conf.get("job_parms"); //這樣就拿到了
......
} catch (SQLException e) {
e.printStackTrace();
}
}
全局文件:hadoop有distributed cache來保存全局文件,保證所有node都可以訪問,使用類名為DistributedCache
網(wǎng)頁名稱:Hadoop讀取環(huán)境變量及setup函數(shù)
鏈接分享:http://ef60e0e.cn/article/jdojoi.html