MapReduce将小文件合并成大文件,并设置每个切片的大小的案例

测试代码:

package cn.toto.bigdata.combinefile;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * 当遇到小文件处理时,每个文件会被当成一个split,那么资源消耗非常大,hadoop支持将小文件合并后当成一个切片处理。(默认)
 */
public class SmallFileCombiner {
	
	static class SmallFileCombinerMapper extends Mapper<LongWritable, Text, Text, NullWritable>{
		NullWritable v = NullWritable.get();
		@Override
		protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
			//通过这种方式相当于是直接把值打印到磁盘文件中。value其实就是每一样的的文件内容
			context.write(value, v);
		}
		
	}
	
	/**
	 * 如果生产环境中,小文件的数量太多,那么累计起来的数量也是很庞大的,那么这时候就要设置切片的大小了。
	 * 
	 * 即使用:CombineTextInputFormat.setMaxInputSplitSize(job, 1024*1024*150);
	 */
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);
		
		job.setJarByClass(SmallFileCombiner.class);
		
		job.setMapperClass(SmallFileCombinerMapper.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(NullWritable.class);
		
		//下面的方式将小文件划分为一个切片。
		job.setInputFormatClass(CombineTextInputFormat.class);
		//如果小文件的总和为224M,将setMaxInputSplitSize中的第二个参数设置成300M的时候,在
		//E:\wordcount\output下只会生成一个part-m-00000这种文件
		//如果将setMaxInputSplitSize中的第二个参数设置成150M的时候,在
		//E:\wordcount\output下会生成part-m-00000 和 part-m-00001 两个文件
		CombineTextInputFormat.setMaxInputSplitSize(job, 1024*1024*150);
		CombineTextInputFormat.setInputPaths(job, new Path("e:/wordcount/input/"));
		FileOutputFormat.setOutputPath(job, new Path("e:/wordcount/output/"));
		
		job.setNumReduceTasks(0);
		
		job.waitForCompletion(true);
	}
}

准备数据:

在E:\wordcount\input目录下准备小文件(总大小为224M),如:


其中文件内容类似:


执行完成程序之后,进入E:\wordcount\output查看内容:



其中part-m-00000的内容如下:







  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值