设为首页 收藏本站
查看: 1180|回复: 0

[经验分享] hadoop文件合并

[复制链接]

尚未签到

发表于 2015-7-11 09:28:54 | 显示全部楼层 |阅读模式
  众所周知,Hadoop对处理单个大文件比处理多个小文件更有效率,另外单个文件也非常占用HDFS的存储空间。所以往往要将其合并起来。
  1,getmerge
  hadoop有一个命令行工具getmerge,用于将一组HDFS上的文件复制到本地计算机以前进行合并
  参考:http://hadoop.apache.org/common/docs/r0.19.2/cn/hdfs_shell.html
  使用方法:hadoop fs -getmerge   [addnl]
  接受一个源目录和一个目标文件作为输入,并且将源目录中所有的文件连接成本地目标文件。addnl是可选的,用于指定在每个文件结尾添加一个换行符。
  多嘴几句:调用文件系统(FS)Shell命令应使用 bin/hadoop fs 的形式。 所有的的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 {
@Override
protected boolean isSplitable(JobContext context, Path file) {
return false;
}
@Override
public RecordReader 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{
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
{
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);
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-85401-1-1.html 上篇帖子: hadoop上的pageRank算法 下篇帖子: Hadoop学习笔记(2) ——解读Hello World
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表