生产上的小文件问题很多,我们需要先根据集群的资源,测试出一个task最大能运行的size,然后根据这个参数,做coalesce()的小文件合并操作。 一般默认我们的生产集群给的size是1G。 以下代码即可测试出最小的coalesce数。
import java.net.URI
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileSystem, Path}
object FileUtils {
def getCoalesce(fileSystem: FileSystem, path:String, size:Int)={
var length = 0l
fileSystem.globStatus(new Path(path))
.map(x => {
length += x.getLen
})
(length/1024/1024/size).toInt+1
}
def main(args: Array[String]): Unit = {
val config = new Configuration()
val fs = FileSystem.get(new URI("hdfs://hadoop000:8020"),config)
val coalesce = getCoalesce(fs,"/offLine14/data/*",20)
println(coalesce)
}
}