Hadoop:读取hdfs上zip压缩包并解压到hdfs的实现代码

时间:2022-09-20 16:55:46

背景:

目前工作中遇到一大批的数据,如果不压缩直接上传到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. 第1节 IMPALA:4、5、linux磁盘的挂载和上传压缩包并解压

    第二步:开机之后进行磁盘挂载 分区,格式化,挂载新磁盘 磁盘挂载 df -lh fdisk -l 开始分区 fdisk /dev/sdb   这个命令执行后依次输 n  p  1  回车  回车  w ...

  2. liunx之zip格式的解压命令

    zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzip unzip -o -d /home/s ...

  3. 文件操作工具类: 文件&sol;目录的创建、删除、移动、复制、zip压缩与解压&period;

    FileOperationUtils.java package com.xnl.utils; import java.io.BufferedInputStream; import java.io.Bu ...

  4. 「Python实用秘技01」复杂zip文件的解压

    本文完整示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/PythonPracticalSkills 这是我的新系列文章「Python实用秘技」的第1 ...

  5. ref&colon;Spring Integration Zip 不安全解压(CVE-2018-1261)漏洞分析

    ref:https://mp.weixin.qq.com/s/SJPXdZWNKypvWmL-roIE0Q 0x00 漏洞概览 漏洞名称:Spring Integration Zip不安全解压 漏洞编 ...

  6. java zip 压缩与解压

    java zip 压缩与解压 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java. ...

  7. Linux tar&period;gz 、zip、rar 解压 压缩命令

    tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个 ...

  8. ubuntu下各种压缩包的解压命令

    .tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)-------------------------- ...

  9. 正确的 zip 压缩与解压代码

    网上流传的zip压缩与解压 的代码有非常大的问题 尽管使用了ant进行压缩与解压,可是任务的流程还是用的java.util.zip 的方式写的,我在使用的过程中遇到了压缩的文件夹结构有误,甚至出现不同 ...

随机推荐

  1. upload&amp&semi;&amp&semi;download

    package am.demo;  import java.io.File;  import java.io.IOException;  import java.util.Iterator;  imp ...

  2. Jquery下拉列表添加移除数据

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 学习之路三十九:新手学习 - Windows API

    来到了新公司,一开始就要做个程序去获取另外一个程序里的数据,哇,挑战性很大. 经过两周的学习,终于搞定,主要还是对Windows API有了更多的了解. 文中所有的消息常量,API,结构体都整理出来了 ...

  4. JAVA&lowbar;基础面试题

    1.面向对象的特征有哪些方面   1.抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节.抽 ...

  5. SCP和SFTP(都使用SSH。但SCP上传不能中断,而SFTP可以续传,这是最大区别)

    不管SCP还是SFTP,都是SSH的功能之一.都是使用SSH协议来传输文件的. 不用说文件内容,就是登录时的用户信息都是经过SSH加密后才传输的,所以说SCP和SFTP实现了安全的文件传输. SCP和 ...

  6. Segment(技巧 相乘转换成相加 &plus; java)

     Segment Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  7. chrome console 命令简记

    1.快速迭代元素 $$('tr.dispute-num td strong a').map(function (el) { return el.innerHTML; }) 2.复选框选中/取消选中 c ...

  8. 文件描述符与FILE

    1. 文件描述符(重点) 在Linux系统中一切皆可以看成是文件,文件又可分为:普通文件.目录文件.链接文件和设备文件.文件描述符(file descriptor)是内核为了高效管理已被打开的文件所创 ...

  9. 典型分布式系统分析之MapReduce

    在 <分布式学习最佳实践:从分布式系统的特征开始(附思维导图)>一文中,提到学习分布式系统的一个好方法是思考分布式系统要解决的问题,有哪些衡量标准,为了解决这些问题:提出了哪些理论.协议. ...

  10. cordova&plus;vue打包webapp

    使用cordova+vue打包webapp,可以快速给网页套上一个android和ios壳子,完成一个app的开发. 1. 环境准备. (1)node.js  下载地址: https://nodejs ...