首先在网上找了好久没有找到从本地文件系统上传整个目录到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. ruby oop学习

    class Man def initialize(name,age) @name=name @age=age end def sayName puts @name end def sayAge put ...

  2. 【HDOJ】1114 Piggy-Bank

    DP,先将coins按照重量排序可以优化. #include <stdio.h> #include <stdlib.h> #define MAXNUM 10005 #defin ...

  3. Visual Studio Solution Configuration

    https://msdn.microsoft.com/en-us/library/bb166577.aspx Solution configurations store solution-level ...

  4. Android开发之BroadcastReceiver的使用

    1.静态注册. 在manifest中注册. <receiver android:name="com.exce.learnbroadcastreceiver.MyReceiver&quo ...

  5. VM8下安装Mac OS X 10.7

    下载Mac OS X  10.7 安装包http://115.com/file/clj1iu8m#            下载HJMac http://115.com/file/cljyu1rh#   ...

  6. ☀【组件】字符串 string

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  7. SharePoint 2010 Ribbon的实现

    转:http://blog.csdn.net/wang4237/article/details/5306335 SharePoint2010的页面风格发生了很大的改变,其页面风格类似于Office的视 ...

  8. Tdxtreelist 行变色

    ACanvas.Font.Color := clRed;   //如果有加印的  变颜色

  9. NPOI读取Excel案例

    3.4用NPOI操作EXCEL--从Excel中抽取文本 我们知道,搜索引擎最擅长处理的就是文本,而Excel中的内容并不是以文本方式存储的.那么如果想要搜索引擎爬虫能够抓取到Excel中的内容是比较 ...

  10. c# 中Intern的作用

    1. 函数如下 public static string Intern(string str) { if(str == null) { throw new ArgumentNullException( ...