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 的方式写的,我在使用的过程中遇到了压缩的文件夹结构有误,甚至出现不同 ...
随机推荐
- 实用的HTML优化技巧
如何提升Web页面的性能,很多开发人员从多个方面来下手如JavaScript.图像优化.服务器配置,文件压缩或是调整CSS. 很显然HTML 已经达到了一个瓶颈,尽管它是Web开发 界面必备的核心语言 ...
- shiro授权
一.shiro-permission.ini shiro-permission.ini里面的内容相当于在数据库 #用户 [users] #用户zhang的密码是123,此用户具有role1和role2 ...
- Java IO流简介
Java中的流是什么? java中的流是一个抽象的概念,在java的程序中需要把文件从一个设备传输到另一个设备上,这个设备可以是内存,程序,文件,网络.把在这些之间传输的叫做流.官方的解释:流是一组有 ...
- 走近webpack(1)--多入口及devServer的使用
上一篇文章留下了一些问题,如果你没看过上一篇文章,可以在我的博客里查找,或者直接从这篇文章开始也是没问题的. const path = require('path'); module.exports= ...
- Webpack执行命令参数详解
一.概述前面的章节我们讲解了webpack的安装.webpack.config.js的 基本配置.webpack执行命名以及require方法的使用,不 知道大家有没有发现,当我们每次修改或者新增一个 ...
- 去除Vue在WebStorm中报命名空间的错误
Preferences -> Editor -> Inspections找到XML,把 Unbound XML namespace prefix的勾去掉
- 后台运行之[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil]
// 正常程序退出后,会在几秒内停止工作: // 要想申请更长的时间,需要用到 // beginBackgroundTaskWithExpirationHandler // endBackground ...
- .net core2.0下Ioc容器Autofac使用
.net core发布有一段时间了,最近两个月开始使用.net core2.0开发项目,大大小小遇到了一些问题.准备写个系列介绍一下是如何解决这些问题以及对应技术.先从IOC容器Autofac开始该系 ...
- bjtu 1819 二哥求和(前缀和)
题目 . 二哥的求和 时间限制 ms 内存限制 MB 题目描述 某一天,calfcamel问二哥,有道数学题怎么做呀?二哥看了一下说我不会呀,于是二哥找到了你,请你帮他解决这个问题,这样二哥就可以鄙视 ...
- 第2次作业:Wechat创作史
Wechat创作史 比尔盖茨曾经说过一句话:21世纪要么电子商务,要么无商可务. 2.1 介绍产品相关信息 -the information about Wechat 你选择的产品是? 选择微信作 ...