首先在网上找了好久没有找到从本地文件系统上传整个目录到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. 释放SQL Server占用的内存

    由于Sql Server对于系统内存的管理策略是有多少占多少,除非系统内存不够用了(大约到剩余内存为4M左右),Sql Server才会释放一点点内存.所以很多时候,我们会发现运行Sql Server ...

  2. 手势识别官方教程(3)识别移动手势(识别速度用VelocityTracker)

    moving手势在onTouchEvent()或onTouch()中就可识别,编程时主要是识别积云的速度用VelocityTracker等, Tracking Movement This lesson ...

  3. ActionBar官方教程(3)更改标题处的图片

    Using a logo instead of an icon By default, the system uses your application icon in the action bar, ...

  4. 【DataStructure In Python】Python实现各种排序算法

    使用Python实现直接插入排序.希尔排序.简单选择排序.冒泡排序.快速排序.归并排序.基数排序. #! /usr/bin/env python # DataStructure Sort # Inse ...

  5. Formatting is Specified but argument is not IFormattable

    private void DeviceSetText(TextBox textBox, string text) { //处理text的显示值 ") //小数位后保留2位 { //小数点后保 ...

  6. Alexander Grothendieck去世了

    Alexander Grothendieck (German: [ˈɡroːtn̩diːk]; French: [ɡʁɔtɛndik]; 28 March 1928 – 13 November 201 ...

  7. Mathtype(对齐设置)

    选左对齐 可以编辑->插入符号->空格符

  8. 【原】Spark中Client源码分析(一)

    在Spark Standalone中我们所谓的Client,它的任务其实是由AppClient和DriverClient共同完成的.AppClient是一个允许app(Client)和Spark集群通 ...

  9. 【CSS】Intermediate4:Background Images

    1. background:background-color url-background-image background-repeat(repeat/repeat-y/repeat-x/no-re ...

  10. PHP中的empty()和isset()的比较