首先在网上找了好久没有找到从本地文件系统上传整个目录到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程序的更多相关文章

  1. 使用递归方法实现,向FTP服务器上传整个目录结构、从FTP服务器下载整个目录到本地的功能

    我最近由于在做一个关于FTP文件上传和下载的功能时候,发现Apache FTP jar包没有提供对整个目录结构的上传和下载功能,只能非目录类型的文件进行上传和下载操作,后来我查阅很多网上的实现方法,再 ...

  2. 利用Java API通过路径过滤上传多文件至HDFS

    在本地文件上传至HDFS过程中,很多情况下一个目录包含很多个文件,而我们需要对这些文件进行筛选,选出符合我们要求的文件,上传至HDFS.这时就需要我们用到文件模式. 在项目开始前,我们先掌握文件模式 ...

  3. 用winscp从本地上传文件到服务器上出现复制文件到远端时错误。

    用winscp从本地上传文件到服务器上出现复制文件到远端时错误. 错误码:4 服务器返回的错误消息:write failed 报错如下图所示: 分析过程: 1.刚开始以为是权限不够,后面上网查了一下是 ...

  4. Git本地上传到服务器

    Git本地上传到服务器 2018年05月17日 10:45:02 VV-King 阅读数:643 标签: git   1.本机window系统的话先下载msysgit  下载后在开始菜单里面找到 &q ...

  5. springMVC + hadoop + httpclient 文件上传请求直接写入hdfs

    1.首先是一个基于httpclient的java 应用程序,代码在这篇文章的开头:点击打开链接 2.我们首先写一个基于springMVC框架的简单接收请求上传的文件保存本地文件系统的demo,程序代码 ...

  6. 利用git把本地项目传到github+将github中已有项目从本地上传更新

    利用git把本地项目传到github中 1.打开git bash命令行,进入到要上传的项目中,比如Spring项目,在此目录下执行git init 的命令,会发下在当前目录中多了一个.git的文件夹( ...

  7. 大数据学习——服务器定期上传nginx日志到hdfs

    需求:按照所学知识完成如下: 服务器定期上传nginx日志到hdfs 提示: Hdfs的创建文件夹命令: Hadoop fs -mkdir /文件夹名称 Hdfs的上传命令: Hadoop fs -p ...

  8. kindeditor本地上传报错,只限初学者

    困扰了我三天的问题,话说百度真的害死人啊,百度上有说路劲错了的,有说包没导的,有说还要改plugins里面的文件的!其实这个都不用动,也有说服务器问题的,还有说缓存的,还有说是ecplise的,反正我 ...

  9. Linux中ftp不能上传文件/目录的解决办法

    在linux中不能上传文件或文件夹最多的问题就是权限问题,但有时也不一定是权限问题了,像我就是空间不够用了,下面我来总结一些ftp不能上传文件/目录的解决办法   在排除用户组和权限等问题后,最可能引 ...

随机推荐

  1. ANDROID_MARS学习笔记_S01原始版_020_Mp3player001_歌曲列表

    一.项目设计 二.歌曲列表简介 1.利用java.net.HttpURLConnection以流的形式下载xml文件为String 2.自定义ContentHandler-->Mp3ListCo ...

  2. QReadWriteLock 读写锁的区别

    QReadWriteLock 这个允许多个进程同时读,但是只有一个写.而且写读不能同时进行. 文档里语焉不详,这是我自己的理解: lockForWrite 为写而锁,就是要修改数据,外人连想进来读数据 ...

  3. proc 文件系统调节参数介绍

    /proc/net/* snmp文件 Ip: ip项 Forwarding        : 是否开启ip_forward,1开启,2关闭 DefaultTTL       : IP默认ttl. In ...

  4. Android开发之AlertDialog

    http://www.cnblogs.com/Gaojiecai/archive/2011/12/10/2283156.html http://www.2cto.com/kf/201205/13187 ...

  5. phpMyAdmin 'import.php'跨站脚本漏洞

    漏洞版本: phpMyAdmin phpMyAdmin 3.4.9 phpMyAdmin phpMyAdmin 3.4.8 phpMyAdmin phpMyAdmin 3.4.6 phpMyAdmin ...

  6. c语言时间库函数#include<time.h>

    日期与时间函数<time.h> 头文件<time.h>中说明了一些用于处理日期和时间的类型和函数.其中的一部分函数用于处理当地时间,因为时区等原因,当地时间与日历时间可能不相同 ...

  7. NSString 转换 float 的精度问题, 换double类型可以解决

    @"0.01" 转换成float时, 经常会变成  0.009999799 这种形式, 因为float类型无法精准保存, 系统会选一个接近的值来代替. 而double类型则可以有更 ...

  8. grb文件的读取

    grb文件的读取(转自:http://blog.sciencenet.cn/blog-922140-713837.html) read_grib.r4.rar 今天来斟酌了下grb文件格式的读取,现在 ...

  9. 【转】Visual Studio快捷键汇总

    原文网址:http://www.cnblogs.com/lanxuezaipiao/p/3451943.html Visual Studio最好用的快捷键(你最喜欢哪个) 每次在网上搜关于VS有哪些常 ...

  10. [Raobin] Ext.net 页面由于CMB的store和对图像同时执行,所以不会触发非空验证 所以会在后台直接调Js去验证

    X.Call("valid", vm.ID_EDIT_FORM); x.Call("前台的js的方法名称"," 参数为集合");