打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Java实现远程HDFS的文件操作(新建、上传、下载、删除)
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;


public class HDFSTest01 {

/**
* @author dcx by 2015.11.19
* 新建文件 
* @param dsta
* @param conf
* @return
*/
public static boolean CreatDir(String dst , Configuration conf){
Path dstPath = new Path(dst) ;
try{
FileSystem dhfs = FileSystem.get(conf);
       dhfs.mkdirs(dstPath);
}
catch(IOException ie){
ie.printStackTrace() ;
return false ;
}
return true ;
}
 

/**
* @author dcx by 2015.11.19
* 文件上传
* @param src 
* @param dst
* @param conf
* @return
*/
public static boolean putToHDFS(String src , String dst , Configuration conf){
Path dstPath = new Path(dst) ;
try{
FileSystem hdfs = dstPath.getFileSystem(conf) ;
hdfs.copyFromLocalFile(false, new Path(src), dstPath) ;
}
catch(IOException ie){
ie.printStackTrace() ;
return false ;
}
return true ;
}

/**
*  @author dcx by 2015.11.19
* 文件下载
* @param src
* @param dst
* @param conf
* @return
*/
public static boolean getFromHDFS(String src , String dst , Configuration conf){
Path dstPath = new Path(dst) ;
try{
FileSystem dhfs = dstPath.getFileSystem(conf) ;
dhfs.copyToLocalFile(false, new Path(src), dstPath) ;
}catch(IOException ie){
ie.printStackTrace() ;
return false ;
}
return true ;
}

 
/**
* @author dcx by 2015.11.19
* 文件删除
* @param path
* @param conf
* @return
*/
public static boolean checkAndDel(final String path , Configuration conf){
Path dstPath = new Path(path) ;
try{
FileSystem dhfs = dstPath.getFileSystem(conf) ;
if(dhfs.exists(dstPath)){
dhfs.delete(dstPath, true) ;
}else{
return false ;
}
}catch(IOException ie ){
ie.printStackTrace() ;
return false ;
}
return true ;
}




/**
* @param 主函数测试
*/
public static void main(String[] args) {

boolean status = false ;
String dst1 = "hdfs://192.168.1.225:9000/EBLearn_data/new" ;
Configuration conf = new Configuration() ;
 
//java.lang.IllegalArgumentException: Wrong FS:            hdfs://192.168.1.225:9000/EBLearn_data/hello.txt, expected: file:///
    //解决这个错误的两个方案:
//方案1:下面这条命令必须加上,否则出现上面这个错误
conf.set("fs.default.name", "hdfs://192.168.1.225:9000"); // "hdfs://master:9000"  
    //方案2: 将core-site.xml 和hdfs-site.xml放入当前工程中
   status = CreatDir( dst1 ,  conf) ;
   System.out.println("status="+status) ;

   String dst = "hdfs://192.168.1.225:9000/EBLearn_data" ;
String src = "I:/hello.txt" ;

   status = putToHDFS( src ,  dst ,  conf) ;
System.out.println("status="+status) ;
    
src = "hdfs://192.168.1.225:9000/EBLearn_data/hello.txt" ;
dst = "I:/hadoop_need/" ;
status = getFromHDFS( src ,  dst ,  conf) ;
System.out.println("status="+status) ;
 
dst = "hdfs://192.168.1.225:9000/EBLearn_data/hello.txt" ;
status = checkAndDel( dst ,  conf) ;
System.out.println("status="+status) ;
}




}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
HDFS操作全记录
HDFS中JAVA API的使用
使用hadoop存储图片服务器
HDFS 常用文件操作命令
hadoop hdfs
python批量拷贝文件
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服