java读取HDFS压缩文件乱码

本文介绍了解决Java直接读取HDFS压缩文件出现乱码的问题,提供了两种解决方案:一是通过API解码并处理;二是下载解压处理。推荐使用第一种方案,并附带介绍了MR作业中使用压缩提升效率的方法。

java通过调用HDFS系统的FileSystem等API 直接读取HDFS的压缩文件会产生乱码

解决方法:

1.调用解码的API,解码后通过IO流处理。

public static void main(String[] args) throws IOException {     
     Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);
        boolean tag=fs.exists(new Path(args[0]));
        String ftag=tag?"exist":"not exist";
        logger.info("===>the HDFS File :"+ args[0] +"is "+ftag);
        InputStream in=fs.open(new Path(args[0]));
        
    //核心转换部分
        CompressionCodecFactory factory = new CompressionCodecFactory(conf);  
        CompressionCodec codec = factory.getCodec(new Path(args[0])); 
        CompressionInputStream compin=codec.createInputStream(in);
        BufferedReader br= new BufferedReader(new InputStreamReader(compin));
         
        String line="";
        while((line=br.readLine())!=null){
           //TODO
        }
        //TODO 关闭流
}

2.文件不大的话,也可以hadoop fs -get xxxx,下载到本地解压后当成普通文件处理。

推荐使用第一种。

 

other

多个MR顺序执行时,中间如果结果较大几百G,可已使用

FileOutputFormat.setCompressOutput(job1, true);
FileOutputFormat.setOutputCompressorClass(job1, GzipCodec.class);

压缩比很高,可提高效率

转载于:https://www.cnblogs.com/yanghaolie/p/7085835.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值