Hadoop SequcenceFile 处理多个小文件

利用sequenceFile打包多个小文件,MapFile是sequenceFile的排序形式,程序如下:

public class testSequenceFile {

    public static void main(String[] args) throws IOException{
        Configuration conf = new Configuration();
        
        FileSystem fs = FileSystem.get(conf);
        FileStatus[] files = fs.listStatus(new Path(args[0]));
        
        Text key = new Text();
        Text value = new Text();
        SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, new Path(args[1]),key.getClass() , value.getClass());
        InputStream in = null;
        byte[] buffer = null;
        
        for(int i=0;i<files.length;i++){
            key.set(files[i].getPath().getName());
            in = fs.open(files[i].getPath());
            buffer = new byte[(int) files[i].getLen()];
            IOUtils.readFully(in, buffer, 0, buffer.length);
            value.set(buffer);
            IOUtils.closeStream(in);
            System.out.println(key.toString()+"\n"+value.toString());
            writer.append(key, value);
        }    
        
        IOUtils.closeStream(writer);
    }
}


这里需要注意的是sequenceFile是二进制文件,cat more less 之类的命令都不能以文本形式显示

顺序文件的内容,需要用到fs命令的-text选项,该选项可以查看文件的代码,检测出文件的类型并适当的转化成文本,如下图“

KeXie@KeXie-PC ~/hadoop-0.20.2
$ hadoop fs -cat soutput
SEQorg.apache.hadoop.io.Textorg.apache.hadoop.io.Text*org.apache.hadoop.io.compress.DefaultCodec▒A▒=▒▒=U▒2▒,a.txtx▒▒▒L▒,*▒▒,,M▒▒▒▒<▒A#b.txtx▒▒L▒H▒▒▒y▒\▒▒▒y▒\@6n:c.txtx▒▒+*▒▒,,M▒▒▒▒%▒▒
KeXie@KeXie-PC ~/hadoop-0.20.2
$ hadoop fs -text soutput
a.txt   xie     chen     liang
quan
b.txt   chen    chen    wen
an
wen
c.txt   mo     an
an


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值