代碼見:https://github.com/JNUpython/hadoop_spark/tree/master/src/main/java/org/shangu/serialization

image.png
小文件hdfs存儲(chǔ)
每個(gè)文件都會(huì)占用一個(gè)block(默認(rèn)yarn block_size=128mb),文件多直接導(dǎo)致namenode資源耗盡,
image.png
小文件切片
小文件分塊和切片是兩個(gè)完全不同概念:前者為物理過程,將數(shù)據(jù)存儲(chǔ)在磁盤上;后者為數(shù)據(jù)讀取邏輯處理過程,讀取數(shù)據(jù)作為maptask的輸入,因此切片數(shù)量直接對(duì)應(yīng)maptask開啟的數(shù)量。
如果直接采用下面代碼讀取數(shù)據(jù):
FileInputFormat.setInputPaths(job, new Path(args[0]));
因?yàn)閿?shù)據(jù)切片,不考慮數(shù)據(jù)整體大小而是分文件單獨(dú)考慮那么直接采用該方法就會(huì)有4個(gè)切片,即多少個(gè)小文件對(duì)應(yīng)多少個(gè)切片,實(shí)際根本不要這么多task,maptask啟動(dòng)也是很費(fèi)時(shí)間。
[INFO ] 2019-05-05 11:39:27,437 method:org.apache.hadoop.mapreduce.JobSubmitter.submitJobInternal(JobSubmitter.java:198)
number of splits:4
上面是利用 FileInputFormat 提交job信息輸出 切片數(shù)量為4, 對(duì)用4個(gè)maptask
小文件切片:CombineTextInputFormat
FileInputFormat運(yùn)行之前設(shè)置:4mb
job.setInputFormatClass(CombineTextInputFormat.class);
CombineTextInputFormat.setMaxInputSplitSize(job, 4194304);
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
切片數(shù)量變?yōu)?
[INFO ] 2019-05-05 11:53:15,298 method:org.apache.hadoop.mapreduce.JobSubmitter.submitJobInternal(JobSubmitter.java:198)
number of splits:1