hadoop 文件合并
来自:http://blog.csdn.net/dandingyy/article/details/7490046
众所周知,Hadoop对处理单个大文件比处理多个小文件更有效率,另外单个文件也非常占用HDFS的存储空间。所以往往要将其合并起来。
1,getmerge
hadoop有一个命令行工具getmerge,用于将一组HDFS上的文件复制到本地计算机以前进行合并
参考:http://hadoop.apache.org/common/docs/r0.19.2/cn/hdfs_shell.html
使用方法:hadoop fs -getmerge <src> <localdst> [addnl]
接受一个源目录和一个目标文件作为输入,并且将源目录中所有的文件连接成本地目标文件。addnl是可选的,用于指定在每个文件结尾添加一个换行符。
多嘴几句:调用文件系统(FS)Shell命令应使用 bin/hadoop fs <args>的形式。 所有的的FS shell命令使用URI路径作为参数。URI格式是scheme://authority/path。
2.putmerge
将本地小文件合并上传到HDFS文件系统中。
一种方法可以现在本地写一个脚本,先将一个文件合并为一个大文件,然后将整个大文件上传,这种方法占用大量的本地磁盘空间;
另一种方法如下,在复制的过程中上传。参考:《hadoop in action》
- import java.io.IOException;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.FSDataInputStream;
- import org.apache.hadoop.fs.FSDataOutputStream;
- import org.apache.hadoop.fs.FileStatus;
- import org.apache.hadoop.fs.FileSystem;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IOUtils;
- //参数1为本地目录,参数2为HDFS上的文件
- public class PutMerge {
- public static void putMergeFunc(String LocalDir, String fsFile) throws IOException
- {
- Configuration conf = new Configuration();
- FileSystem fs = FileSystem.get(conf); //fs是HDFS文件系统
- FileSystem local = FileSystem.getLocal(conf); //本地文件系统
- Path localDir = new Path(LocalDir);
- Path HDFSFile = new Path(fsFile);
- FileStatus[] status = local.listStatus(localDir); //得到输入目录
- FSDataOutputStream out = fs.create(HDFSFile); //在HDFS上创建输出文件
- for(FileStatus st: status)
- {
- Path temp = st.getPath();
- FSDataInputStream in = local.open(temp);
- IOUtils.copyBytes(in, out, 4096, false); //读取in流中的内容放入out
- in.close(); //完成后,关闭当前文件输入流
- }
- out.close();
- }
- public static void main(String [] args) throws IOException
- {
- String l = "/home/kqiao/hadoop/MyHadoopCodes/putmergeFiles";
- String f = "hdfs://ubuntu:9000/user/kqiao/test/PutMergeTest";
- putMergeFunc(l,f);
- }
- }
3.将小文件打包成SequenceFile的MapReduce任务
来自:《hadoop权威指南》
实现将整个文件作为一条记录处理的InputFormat:
- public class WholeFileInputFormat
- extends FileInputFormat<NullWritable, BytesWritable> {
- @Override
- protected boolean isSplitable(JobContext context, Path file) {
- return false;
- }
- @Override
- public RecordReader<NullWritable, BytesWritable> createRecordReader(
- InputSplit split, TaskAttemptContext context) throws IOException,
- InterruptedException {
- WholeFileRecordReader reader = new WholeFileRecordReader();
- reader.initialize(split, context);
- return reader;
- }
- }
实现上面类中使用的定制的RecordReader:
- /实现一个定制的RecordReader,这六个方法均为继承的RecordReader要求的虚函数。
- //实现的RecordReader,为自定义的InputFormat服务
- public class WholeFileRecordReader extends RecordReader<NullWritable, BytesWritable>{
- private FileSplit fileSplit;
- private Configuration conf;
- private BytesWritable value = new BytesWritable();
- private boolean processed = false;
- @Override
- public void close() throws IOException {
- // do nothing
- }
- @Override
- public NullWritable getCurrentKey() throws IOException,
- InterruptedException {
- return NullWritable.get();
- }
- @Override
- public BytesWritable getCurrentValue() throws IOException,
- InterruptedException {
- return value;
- }
- @Override
- public float getProgress() throws IOException, InterruptedException {
- return processed? 1.0f : 0.0f;
- }
- @Override
- public void initialize(InputSplit split, TaskAttemptContext context)
- throws IOException, InterruptedException {
- this.fileSplit = (FileSplit) split;
- this.conf = context.getConfiguration();
- }
- //process表示记录是否已经被处理过
- @Override
- public boolean nextKeyValue() throws IOException, InterruptedException {
- if (!processed) {
- byte[] contents = new byte[(int) fileSplit.getLength()];
- Path file = fileSplit.getPath();
- FileSystem fs = file.getFileSystem(conf);
- FSDataInputStream in = null;
- try {
- in = fs.open(file);
- //将file文件中 的内容放入contents数组中。使用了IOUtils实用类的readFully方法,将in流中得内容放入
- //contents字节数组中。
- IOUtils.readFully(in, contents, 0, contents.length);
- //BytesWritable是一个可用做key或value的字节序列,而ByteWritable是单个字节。
- //将value的内容设置为contents的值
- value.set(contents, 0, contents.length);
- } finally {
- IOUtils.closeStream(in);
- }
- processed = true;
- return true;
- }
- return false;
- }
- }
将小文件打包成SequenceFile:
- public class SmallFilesToSequenceFileConverter extends Configured implements Tool{
- //静态内部类,作为mapper
- static class SequenceFileMapper extends Mapper<NullWritable, BytesWritable, Text, BytesWritable>
- {
- private Text filenameKey;
- //setup在task开始前调用,这里主要是初始化filenamekey
- @Override
- protected void setup(Context context)
- {
- InputSplit split = context.getInputSplit();
- Path path = ((FileSplit) split).getPath();
- filenameKey = new Text(path.toString());
- }
- @Override
- public void map(NullWritable key, BytesWritable value, Context context)
- throws IOException, InterruptedException{
- context.write(filenameKey, value);
- }
- }
- @Override
- public int run(String[] args) throws Exception {
- Configuration conf = new Configuration();
- Job job = new Job(conf);
- job.setJobName("SmallFilesToSequenceFileConverter");
- FileInputFormat.addInputPath(job, new Path(args[0]));
- FileOutputFormat.setOutputPath(job, new Path(args[1]));
- //再次理解此处设置的输入输出格式。。。它表示的是一种对文件划分,索引的方法
- job.setInputFormatClass(WholeFileInputFormat.class);
- job.setOutputFormatClass(SequenceFileOutputFormat.class);
- //此处的设置是最终输出的key/value,一定要注意!
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(BytesWritable.class);
- job.setMapperClass(SequenceFileMapper.class);
- return job.waitForCompletion(true) ? 0 : 1;
- }
- public static void main(String [] args) throws Exception
- {
- int exitCode = ToolRunner.run(new SmallFilesToSequenceFileConverter(), args);
- System.exit(exitCode);
- }
- }
hadoop 文件合并的更多相关文章
- Hadoop MapReduce编程 API入门系列之小文件合并(二十九)
不多说,直接上代码. Hadoop 自身提供了几种机制来解决相关的问题,包括HAR,SequeueFile和CombineFileInputFormat. Hadoop 自身提供的几种小文件合并机制 ...
- 013_HDFS文件合并上传putmarge功能(类似于hadoop fs -getmerge)
场景 合并小文件,存放到HDFS上.例如,当需要分析来自许多服务器的Apache日志时,各个日志文件可能比较小,然而Hadoop更合适处理大文件,效率会更高,此时就需要合并分散的文件.如果先将所有文件 ...
- Hadoop经典案例(排序&Join&topk&小文件合并)
①自定义按某列排序,二次排序 writablecomparable中的compareto方法 ②topk a利用treemap,缺点:map中的key不允许重复:https://blog.csdn.n ...
- hive小文件合并设置参数
Hive的后端存储是HDFS,它对大文件的处理是非常高效的,如果合理配置文件系统的块大小,NameNode可以支持很大的数据量.但是在数据仓库中,越是上层的表其汇总程度就越高,数据量也就越小.而且这些 ...
- HDFS操作及小文件合并
小文件合并是针对文件上传到HDFS之前 这些文件夹里面都是小文件 参考代码 package com.gong.hadoop2; import java.io.IOException; import j ...
- MR案例:小文件合并SequeceFile
SequeceFile是Hadoop API提供的一种二进制文件支持.这种二进制文件直接将<key, value>对序列化到文件中.可以使用这种文件对小文件合并,即将文件名作为key,文件 ...
- Hive merge(小文件合并)
当Hive的输入由非常多个小文件组成时.假设不涉及文件合并的话.那么每一个小文件都会启动一个map task. 假设文件过小.以至于map任务启动和初始化的时间大于逻辑处理的时间,会造成资源浪费.甚至 ...
- hive优化之小文件合并
文件数目过多,会给HDFS带来压力,并且会影响处理效率,可以通过合并Map和Reduce的结果文件来消除这样的影响: set hive.merge.mapfiles = true ##在 map on ...
- Hadoop文件操作常用命令
1.创建目录 #hdfs dfs -mkidr /test 2.查询目录结构 #hdfs dfs -ls / 子命令 -R递归查看//查看具体的某个目录:例如#hdfs dfs -ls /test 3 ...
随机推荐
- Netty入门实例及分析
什么是netty?以下是官方文档的简单介绍: The Netty project is an effort to provide an asynchronous event-driven netwo ...
- 使用 Crash 工具分析 Linux dump 文件
转自:http://blog.csdn.net/commsea/article/details/11804897 简介: Linux 内核由于其复杂性,使得对内核出现的各种异常的追踪变得异常困难.本文 ...
- Swift - RotateView
Swift - RotateView 效果 源码 https://github.com/YouXianMing/Swift-Animations // // RotateView.swift // S ...
- .NetCore中EFCore for MySql整理
一.MySql官方提供了Ef Core对MySql的支持,但现在还处于预览版 Install-Package MySql.Data.EntityFrameworkCore -Pre Install-P ...
- 磁共振中的T1, T2 和 T2*的原理和区别
从物理的角度,要理解这几个概念的区别,需要对原子核的磁化有所了解,本文通过一些图示对这几个概念进行简明的介绍. 首先,磁共振最基本的原理就是氢原子核在磁场中自旋运动时所具有的量子力学特性.在一个均匀磁 ...
- [转]一次非常有意思的sql优化经历
From :http://www.cnblogs.com/tangyanbo/p/4462734.html 补充:看到这么多朋友对sql优化感兴趣,我又重新补充了下文章的内容,将更多关于sql优化的知 ...
- python抽象类的实现方式:abc模块
abc:abstract base class 文档:https://docs.python.org/zh-cn/3.7/library/abc.html 参考:https://www.cnblogs ...
- 里氏替换原则(Liskov Substitution Principle,LSP)
肯定有不少人跟我刚看到这项原则的时候一样,对这个原则的名字充满疑惑.其实原因就是这项原则最早是在1988年,由麻省理工学院的一位姓里的女士(Barbara Liskov)提出来的. 定义1:如果对每一 ...
- 【转】 Qt如何设置自动补全快捷键
原文:https://blog.csdn.net/u014597198/article/details/52797435 在用Qt编程的时,它默认是以“CTRL+空格”来作为自动补全的快捷键的,但是这 ...
- Easyui1.3.4+IIS6.0+IE8兼容问题解决
刚刚学习JQuery Easyui,就遇到了拦路虎,最新版本1.3.4下载下来部署到win2003 + IIS6.0中发现所有demo都不可以渲染,IE8提示错误如下: 详细内容如下: 用户代理: M ...