从本地上传整个目录到hdfs的java程序
首先在网上找了好久没有找到从本地文件系统上传整个目录到hdfs文件系统的程序,权威指南上也没有,都是单个文件上传,所以这里自己编写了一个程序,封装成jar包执行能够复制。
先说明一下代码:须要手动输入两个路径,一个本地文件/目录路径,第二个是hdfs目录路径。好直接上代码:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable; /**
*
* @author zlqiao
*
*/
public class Copy {
public static void main(String[] args) throws Exception {
if(args.length < 2){
System.out.println("Please input two number");
System.exit(2);
}
String localSrc = args[0];
String dst = args[1];
Configuration conf = new Configuration();
File srcFile = new File(localSrc);
if(srcFile.isDirectory()){
copyDirectory(localSrc , dst , conf);
}else{
copyFile(localSrc, dst, conf);
}
}
/**
* 复制文件
* @param src
* @param dst
* @param conf
* @return
* @throws Exception
*/
public static boolean copyFile(String src , String dst , Configuration conf) throws Exception{
FileSystem fs = FileSystem.get(conf);
fs.exists(new Path(dst));
//FileStatus status = fs.getFileStatus(new Path(dst));
File file = new File(src); InputStream in = new BufferedInputStream(new FileInputStream(file));
/**
* FieSystem的create方法能够为文件不存在的父文件夹进行创建,
*/
OutputStream out = fs.create(new Path(dst) , new Progressable() {
public void progress() {
System.out.print(".");
}
});
IOUtils.copyBytes(in, out, 4096, true); return true;
}
/**
* 复制文件夹
* @param src
* @param dst
* @param conf
* @return
* @throws Exception
*/
public static boolean copyDirectory(String src , String dst , Configuration conf) throws Exception{ FileSystem fs = FileSystem.get(conf);
if(!fs.exists(new Path(dst))){
fs.mkdirs(new Path(dst));
}
System.out.println("copyDirectory:"+dst);
FileStatus status = fs.getFileStatus(new Path(dst));
File file = new File(src); if(status.isFile()){
System.exit(2);
System.out.println("You put in the "+dst + "is file !");
}else{
dst = cutDir(dst);
}
File[] files = file.listFiles();
for(int i = 0 ;i< files.length; i ++){
File f = files[i];
if(f.isDirectory()){
copyDirectory(f.getPath(),dst,conf);
}else{
copyFile(f.getPath(),dst+files[i].getName(),conf);
} }
return true;
}
public static String cutDir(String str){
String[] strs = str.split(File.pathSeparator);
String result = "";
if("hdfs"==strs[0]){
result += "hdfs://";
for(int i = 1 ; i < strs.length ; i++){
result += strs[i] + File.separator;
}
}else{
for(int i = 0 ; i < strs.length ; i++){
result += strs[i] + File.separator;
}
} return result;
}
}
从本地上传整个目录到hdfs的java程序的更多相关文章
- 使用递归方法实现,向FTP服务器上传整个目录结构、从FTP服务器下载整个目录到本地的功能
我最近由于在做一个关于FTP文件上传和下载的功能时候,发现Apache FTP jar包没有提供对整个目录结构的上传和下载功能,只能非目录类型的文件进行上传和下载操作,后来我查阅很多网上的实现方法,再 ...
- 利用Java API通过路径过滤上传多文件至HDFS
在本地文件上传至HDFS过程中,很多情况下一个目录包含很多个文件,而我们需要对这些文件进行筛选,选出符合我们要求的文件,上传至HDFS.这时就需要我们用到文件模式. 在项目开始前,我们先掌握文件模式 ...
- 用winscp从本地上传文件到服务器上出现复制文件到远端时错误。
用winscp从本地上传文件到服务器上出现复制文件到远端时错误. 错误码:4 服务器返回的错误消息:write failed 报错如下图所示: 分析过程: 1.刚开始以为是权限不够,后面上网查了一下是 ...
- Git本地上传到服务器
Git本地上传到服务器 2018年05月17日 10:45:02 VV-King 阅读数:643 标签: git 1.本机window系统的话先下载msysgit 下载后在开始菜单里面找到 &q ...
- springMVC + hadoop + httpclient 文件上传请求直接写入hdfs
1.首先是一个基于httpclient的java 应用程序,代码在这篇文章的开头:点击打开链接 2.我们首先写一个基于springMVC框架的简单接收请求上传的文件保存本地文件系统的demo,程序代码 ...
- 利用git把本地项目传到github+将github中已有项目从本地上传更新
利用git把本地项目传到github中 1.打开git bash命令行,进入到要上传的项目中,比如Spring项目,在此目录下执行git init 的命令,会发下在当前目录中多了一个.git的文件夹( ...
- 大数据学习——服务器定期上传nginx日志到hdfs
需求:按照所学知识完成如下: 服务器定期上传nginx日志到hdfs 提示: Hdfs的创建文件夹命令: Hadoop fs -mkdir /文件夹名称 Hdfs的上传命令: Hadoop fs -p ...
- kindeditor本地上传报错,只限初学者
困扰了我三天的问题,话说百度真的害死人啊,百度上有说路劲错了的,有说包没导的,有说还要改plugins里面的文件的!其实这个都不用动,也有说服务器问题的,还有说缓存的,还有说是ecplise的,反正我 ...
- Linux中ftp不能上传文件/目录的解决办法
在linux中不能上传文件或文件夹最多的问题就是权限问题,但有时也不一定是权限问题了,像我就是空间不够用了,下面我来总结一些ftp不能上传文件/目录的解决办法 在排除用户组和权限等问题后,最可能引 ...
随机推荐
- 程序模拟浏览器请求及会话保持-python实现
http://www.cnblogs.com/zxlovenet/p/4006649.html
- iOS,Android网络抓包教程之tcpdump
现在的移动端应用几乎都会通过网络请求来和服务器交互,通过抓包来诊断和网络相关的bug是程序员的重要技能之一.抓包的手段有很多:针对http和https可以使用Charles设置代理来做,对于更广泛的协 ...
- Ember.js demo3
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery.j ...
- wzplayer2 for windows ActiveX 试用地址
提供wzplayer2 for windows ActiveX,测试地址:http://www.coolradio.cn/IE.htm 大家使用时候必须允许未签名ActiveX下载和运行,否则将无法正 ...
- WPF——数据绑定及属性改变事件
一.首先需要封装一下文本框的属性,并且在实体类中添加一个实体类的属性改变函数 public class User : INotifyPropertyChanged //INotifyPropertyC ...
- BZOJ_1833_[ZJOI2010]_数字计数_(数位dp)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1833 统计\(a~b\)中数字\(0,1,2,...,9\)分别出现了多少次. 分析 数位dp ...
- rfc all download
http://www.rfc-editor.org/download.html about RTSP http://en.wikipedia.org/wiki/Real_Time_Streaming_ ...
- [摘]selenium-ide命令
关于,selenium 命令这一部分,为了便于像我一样的菜鸟理解,我采用通过例子讲命令的方式. 菜鸟Selenium 命令通常被称为selenese,有一系列运行测试案例所需的命令构成. ----// ...
- C#实现Zip压缩解压实例【转】
本文只列举一个压缩帮助类,使用的是有要添加一个dll引用ICSharpCode.SharpZipLib.dll[下载地址]. 另外说明一下的是,这个类压缩格式是ZIP的,所以文件的后缀写成 .zip. ...
- java 异常架构图
cu 红色为检查异常,就是eclipse要提示你是try catch 还是throws. 非检查异常,就是/0,nullpointexception,数据越界访问indexOfOutBounds 异常 ...