Hadoop:读取hdfs上zip压缩包并解压到hdfs的实现代码
背景:
目前工作中遇到一大批的数据,如果不压缩直接上传到ftp上就会遇到ftp空间资源不足问题,没办法只能压缩后上传,上穿完成后在linux上下载。但是linux客户端的资源只有20G左右一个压缩包解压后就要占用16G左右的空间,因此想在linux上直接解压已经太折腾了(因为我们一共需要处理的这样的压缩包包含有30个左右)。
解决方案:
先把linux上下载到的zip压缩包上传到hdfs,等待所有zip压缩包都上传完成后,开始使用程序直接在读取hdfs上的压缩包文件,直接解压到hdfs上,之后把解压后的文件压缩为gzip,实现代码如下(参考:http://www.cnblogs.com/juefan/articles/2935163.html):
import java.io.File;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text; /**
* Created by Administrator on 12/10/2017.
*/
public class ConvertHdfsZipFileToGzipFile {
public static boolean isRecur = false; public static void main(String[] args) throws IOException {
if (args.length == 0)
errorMessage("1filesmerge [-r|-R] <hdfsTargetDir> <hdfsFileName>");
if (args[0].matches("^-[rR]$")) {
isRecur = true;
}
if ((isRecur && args.length != 4) || ( !isRecur && args.length != 3)) {
errorMessage("2filesmerge [-r|-R] <hdfsTargetDir> <hdfsFileName>");
} Configuration conf = new Configuration();
FileSystem hdfs = FileSystem.get(conf); Path inputDir;
Path hdfsFile;
Text pcgroupText; // hadoop jar myjar.jar ConvertHdfsZipFileToGzipFile -r /zip/(待转换文件路径,在HDFS上) /user/j/pconline/(转换完成后的文件存储地址,也在HDFS上) pconline(待转换的文件名包含的字符)
if(isRecur){
inputDir = new Path(args[1]);
hdfsFile = new Path(args[2]);
pcgroupText = new Text(args[3]);
}
// hadoop jar myjar.jar ConvertHdfsZipFileToGzipFile /zip/(待转换文件路径,在HDFS上) /user/j/pconline/(转换完成后的文件存储地址,也在HDFS上) pconline(待转换的文件名包含的字符)
else{
inputDir = new Path(args[0]);
hdfsFile = new Path(args[1]);
pcgroupText = new Text(args[2]);
} if (!hdfs.exists(inputDir)) {
errorMessage("3hdfsTargetDir not exist!");
}
if (hdfs.exists(hdfsFile)) {
errorMessage("4hdfsFileName exist!");
}
merge(inputDir, hdfsFile, hdfs, pcgroupText);
System.exit(0);
} /**
* @author
* @param inputDir zip文件的存储地址
* @param hdfsFile 解压结果的存储地址
* @param hdfs 分布式文件系统数据流
* @param pcgroupText 需要解压缩的文件关键名
*/
public static void merge(Path inputDir, Path hdfsFile,
FileSystem hdfs, Text pcgroupText) {
try {
//文件系统地址inputDir下的FileStatus
FileStatus[] inputFiles = hdfs.listStatus(inputDir);
for (int i = 0; i < inputFiles.length; i++) {
if (!hdfs.isFile(inputFiles[i].getPath())) {
if (isRecur){
merge(inputFiles[i].getPath(), hdfsFile, hdfs,pcgroupText);
return ;
}
else {
System.out.println(inputFiles[i].getPath().getName()
+ "is not file and not allow recursion, skip!");
continue;
}
}
//判断文件名是否在需要解压缩的关键名内
if(inputFiles[i].getPath().getName().contains(pcgroupText.toString()) == true){
//输出待解压的文件名
System.out.println(inputFiles[i].getPath().getName());
//将数据流指向待解压文件
FSDataInputStream in = hdfs.open(inputFiles[i].getPath());
/**
*数据的解压执行过程
*/
ZipInputStream zipInputStream = null;
try{
zipInputStream = new ZipInputStream(in);
ZipEntry entry;
//解压后有多个文件一并解压出来并实现合并
//合并后的地址
FSDataOutputStream mergerout = hdfs.create(new Path(hdfsFile + File.separator +
inputFiles[i].getPath().getName().substring(0, inputFiles[i].getPath().getName().indexOf("."))));
while((entry = zipInputStream.getNextEntry()) != null){
int bygeSize1=2*1024*1024;
byte[] buffer1 = new byte[bygeSize1];
int nNumber;
while((nNumber = zipInputStream.read(buffer1,0, bygeSize1)) != -1){
mergerout.write(buffer1, 0, nNumber);
}
} mergerout.flush();
mergerout.close();
zipInputStream.close();
}catch(IOException e){
continue;
}
in.close();
/**
*将解压合并后的数据压缩成gzip格式
*/
GZIPOutputStream gzipOutputStream = null;
try{
FSDataOutputStream outputStream = null;
outputStream = hdfs.create(new Path(hdfsFile + File.separator +
inputFiles[i].getPath().getName().substring(0, inputFiles[i].getPath().getName().indexOf(".")) + ".gz"));
FSDataInputStream inputStream = null;
gzipOutputStream = new GZIPOutputStream(outputStream);
inputStream = hdfs.open(new Path(hdfsFile + File.separator + inputFiles[i].getPath().getName().substring(0, inputFiles[i].getPath().getName().indexOf("."))));
int bygeSize=2*1024*1024;
byte[] buffer = new byte[bygeSize];
int len;
while((len = inputStream.read(buffer)) > 0){
gzipOutputStream.write(buffer, 0, len);
}
inputStream.close();
gzipOutputStream.finish();
gzipOutputStream.flush();
outputStream.close();
}catch (Exception exception){
exception.printStackTrace();
}
gzipOutputStream.close();
//删除zip文件解压合并后的临时文件
String tempfiles = hdfsFile + File.separator + inputFiles[i].getPath().getName().substring(0, inputFiles[i].getPath().getName().indexOf("."));
try{
if(hdfs.exists(new Path(tempfiles))){
hdfs.delete(new Path(tempfiles), true);
}
}catch(IOException ie){
ie.printStackTrace();
}
}
}
}catch (IOException e) {
e.printStackTrace();
}
} public static void errorMessage(String str) {
System.out.println("Error Message: " + str);
System.exit(1);
} }
调用:
[c@v09823]# hadoop jar myjar.jar [ConvertHdfsZipFileToGzipFile该main的类名根据打包方式决定是否需要] /zip/(待转换文件路径,在HDFS上) /user/j/pconline/(转换完成后的文件存储地址,也在HDFS上) pconline(待转换的文件名包含的字符)
如果要实现递归的话,可以在filesmerge后面加上 -r
执行过程中快照:
[c@v09823 ~]$ hadoop fs -ls /user/c/df/myzip
// :: INFO hdfs.PeerCache: SocketCache disabled.
Found items
-rw-r--r--+ c hadoop -- : user/c/df/myzip/myzip_0.zip
-rw-r--r--+ c hadoop -- : user/c/df/myzip/myzip_12.zip
-rw-r--r--+ c hadoop -- : user/c/df/myzip/myzip_15.zip
... [c@v09823 ~]$ yarn jar My_ConvertHdfsZipFileToGzipFile.jar /user/c/df/myzip user/c/df/mygzip .zip
// :: INFO hdfs.PeerCache: SocketCache disabled.
myzip_0.zip
myzip_12.zip
myzip_15.zip
... [catt@vq20skjh01 ~]$ hadoop fs -ls -h user/c/df/mygzip
// :: INFO hdfs.PeerCache: SocketCache disabled.
Found items
-rw-r--r--+ c hadoop 14.9 G -- : user/c/df/mygzip/myzip_0
-rw-r--r--+ c hadoop 14.9 G -- : user/c/df/mygzip/myzip_12
-rw-r--r--+ c hadoop G -- : user/c/df/mygzip/myzip_15
....
Hadoop:读取hdfs上zip压缩包并解压到hdfs的实现代码的更多相关文章
- 第1节 IMPALA:4、5、linux磁盘的挂载和上传压缩包并解压
第二步:开机之后进行磁盘挂载 分区,格式化,挂载新磁盘 磁盘挂载 df -lh fdisk -l 开始分区 fdisk /dev/sdb 这个命令执行后依次输 n p 1 回车 回车 w ...
- liunx之zip格式的解压命令
zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzip unzip -o -d /home/s ...
- 文件操作工具类: 文件/目录的创建、删除、移动、复制、zip压缩与解压.
FileOperationUtils.java package com.xnl.utils; import java.io.BufferedInputStream; import java.io.Bu ...
- 「Python实用秘技01」复杂zip文件的解压
本文完整示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/PythonPracticalSkills 这是我的新系列文章「Python实用秘技」的第1 ...
- ref:Spring Integration Zip 不安全解压(CVE-2018-1261)漏洞分析
ref:https://mp.weixin.qq.com/s/SJPXdZWNKypvWmL-roIE0Q 0x00 漏洞概览 漏洞名称:Spring Integration Zip不安全解压 漏洞编 ...
- java zip 压缩与解压
java zip 压缩与解压 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java. ...
- Linux tar.gz 、zip、rar 解压 压缩命令
tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个 ...
- ubuntu下各种压缩包的解压命令
.tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)-------------------------- ...
- 正确的 zip 压缩与解压代码
网上流传的zip压缩与解压 的代码有非常大的问题 尽管使用了ant进行压缩与解压,可是任务的流程还是用的java.util.zip 的方式写的,我在使用的过程中遇到了压缩的文件夹结构有误,甚至出现不同 ...
随机推荐
- 手机端原生js实现下拉刷新数据
HTML结构如下: <div class="outerScroller comment"> <div class='scroll comment'> < ...
- 【Flask】 使用Flask-Moment进行日期时间的管理
Flask-Moment Flask-Moment又是一个flask的扩展模块,用来处理时间日期等信息.用这个模块主要是考虑到两点,第一是为了让不同时区的用户看到的都是各自时区的实际时间,而不是服务器 ...
- Linux运维主流架构简单剖析
随着IT运维的不断发展,尤其的Linux的飞速发展,越来越多的企业开始使用Linux操作系统平台,例如CentOS.RedHat.Ubuntu.Fedora等等,成千上亿个网站涌现在当今互联网,互联网 ...
- Spring Cloud Contract
http://www.infoq.com/cn/news/2017/04/spring-cloud-contract 在默认情况下,我们希望用户以JAR文件的形式将生产者存根和契约发布到Maven库. ...
- java多线程(二)-线程的生命周期及线程间通信
一.摘要 当我们将线程创建并start时候,它不会一直占据着cpu执行,而是多个线程间会去执行着这个cpu,此时这些线程就会在多个状态之间进行着切换. 在线程的生命周期中,它会有5种状态,分别为 ...
- nginx session 配置失效解决
nginx 反向代理后台web服务器session path导致的session 失效,特此总结下配置方法: 配置如下: location ^~ /2016tyjf_dev/djwechat { pr ...
- Spring MVC之适配器的获取及执行(RequestMappingHandlerAdapter)
首先看下doDispatch()方法如何找到适合的适配器来执行方法的: protected HandlerAdapter getHandlerAdapter(Object handler) throw ...
- B-end
Beta冲刺成员名单和工作量比例 姓名 学号 负责内容 工作量比例 张梨贤 170327109 负责企业人员的委托/收回授权.第三方机构的委托授权管理.分级统计展示.分级列表展示 26% 黄腾飞 17 ...
- csrf学习笔记
CSRF全称Cross Site Request Forgery,即跨站点请求伪造.我们知道,攻击时常常伴随着各种各样的请求,而攻击的发生也是由各种请求造成的. CSRF攻击能够达到的目的是使受害者发 ...
- django启动uwsgi报错
查看uwsgi.log *** Starting uWSGI 2.0.17 (64bit) on [Thu Apr 5 17:46:15 2018] *** compiled with version ...