从本地上传整个目录到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不能上传文件/目录的解决办法 在排除用户组和权限等问题后,最可能引 ...
随机推荐
- 第十章Composite设备
10.1 Composite设备介绍 USB的Composite类是USB 复合设备类,一个USB设备具有多种设备功能,比如一个USB设备同时具有鼠标和键盘功能.单一的USB设备开发相对简单,但在很多 ...
- Android java.net.SocketException四大异常解决方案
java.net.SocketException如何才能更好的使用呢?这个就需要我们先要了解有关这个语言的相关问题.希望大家有所帮助.那么我们就来看看有关java.net.SocketExceptio ...
- org.springframework.jdbc.datasource
org.springframework.jdbc.datasource.DataSourceUtils /** * Actually obtain a JDBC Connection from the ...
- leetcode面试准备: Maximal Rectangle
leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...
- bzoj1875
我们知道,邻接矩阵自乘t次就是经过t条边的路径数但这道题显然不能这样,因为不能走回头路于是我们可以构造边的邻接矩阵矩乘即可 ; type way=record po,fr:longint; end; ...
- Master Nginx(5) - Reverse Proxy Advanced Topics
Security through separtion Encrypting traffic with SSL Authenticating clients using SSL Blocking tra ...
- Dell笔记本禁用触摸板的方法
一·找到触摸板驱动所在的文件夹(其他型号 的本本,请自己探索一下,找到驱动在哪就行),一般 在 C:\program files\delltpad 中(若没有请下载安 装 ),如图: 二·双击 Del ...
- 惠威的M200MK3的前级电子分频板
M200MKIII是惠威融合了尖端有源电子分频技术而诞生的全新产品:双4声道运算放大器.高档玻璃纤维电路板.全SMT制作工艺.红宝石滤波电容阵列.进口金属化聚丙稀分频电容.超大功率TDA7294功放芯 ...
- android手机屏幕分辨率 及 sp dip(dp) px 区别 及高中低分辨率时处理
分辨率,是指单位长度内包含的像素点的数量,它的单位通常为像素/英寸(ppi).以分辨率为1024×768的屏幕来说,即每一条水平线上包含有1024个像素点,共有768条线,即扫描列数为1024列,行数 ...
- 【原】Spark中Job的提交源码解读
版权声明:本文为原创文章,未经允许不得转载. Spark程序程序job的运行是通过actions算子触发的,每一个action算子其实是一个runJob方法的运行,详见文章 SparkContex源码 ...