java读取HDFS压缩文件乱码
2024-09-03 10:18:59
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);
压缩比很高,可提高效率
最新文章
- iOS UITextField的returnkey点击事件
- web页面隐藏鼠标
- Linux重定向相关(转载帖,供自己cookbook)
- Java:String、StringBuffer和StringBuilder的区别
- C#中string类型前加@标志的作用
- Linux makefile教程之概述一[转]
- 用原生js模仿jquery
- Namespace declaration statement has to be the very first
- [TPYBoard-Micropython之会python就能做硬件 7] 学习使用蓝牙模块及舵机
- 201521123084 《Java程序设计》第6周学习总结
- Python之路,进程、线程、协程篇
- uva11916 Emoogle Grid (BSGS)
- Android事件总线(三)otto用法全解析
- HttpServletResponse和HttpServletRequest
- ORA-16038: log 3 sequence# 103 cannot be archived
- python 形参
- 兼容性测试中如何切换和管理多个JDK版本
- 关于 img 父容器比img图片要多4个像素的问题
- python中动态导入模块
- 【BZOJ3232】圈地游戏 分数规划+最小割