Skip to content

Instantly share code, notes, and snippets.

@xiyoulaoyuanjia
Last active February 26, 2019 11:35
Show Gist options
  • Save xiyoulaoyuanjia/53d17ba2bf935c6721eb08c0168ce904 to your computer and use it in GitHub Desktop.
Save xiyoulaoyuanjia/53d17ba2bf935c6721eb08c0168ce904 to your computer and use it in GitHub Desktop.
解压文件到hdfs
public class IOUtils {
/**
* 解压缩本地文件到hdfs。
* @param srcPath
* @param tgtPath
*/
public static void gzipFileToHdfs(String srcPath,String tgtPath){
Configuration conf=new Configuration();
GzipCodec zip=new GzipCodec();
if(!srcPath.endsWith(zip.getDefaultExtension())) return;
zip.setConf(conf);
Path _srcPath=new Path(srcPath);
Path _tgtPath=new Path(tgtPath);
FileSystem srcDfs;
FileSystem tgtDfs;
try {
srcDfs = _srcPath.getFileSystem(conf);
tgtDfs = _tgtPath.getFileSystem(conf);
CompressionInputStream input=zip.createInputStream(srcDfs.open(_srcPath));
FSDataOutputStream out=tgtDfs.create(_tgtPath);
org.apache.hadoop.io.IOUtils.copyBytes(input,out,conf);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment