zoukankan      html  css  js  c++  java
  • 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);

    压缩比很高,可提高效率

  • 相关阅读:
    PHP异常与错误处理机制
    工作中图片上传遇到的一个问题
    PHP遍历目录四种方法
    ssh框架中.xml文件小技巧分离xml
    读取XML文件内容
    spring_AOP
    spring_AOP_XML
    spring_AOP_annotation
    js异步刷新局部页面
    HQL count(*)
  • 原文地址:https://www.cnblogs.com/yanghaolie/p/7085835.html
Copyright © 2011-2022 走看看