java读取HDFS压缩文件乱码

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);
压缩比很高,可提高效率
以上是 java读取HDFS压缩文件乱码 的全部内容, 来源链接: utcz.com/z/394956.html








