HDFS 使用Java api实现上传/下载/删除文件

来源:互联网 发布:131458淘宝买家工具箱 编辑:程序博客网 时间:2024/05/01 17:02
  1. package Hadoop;  
  2.   
  3.   
  4. import java.io.IOException;  
  5.   
  6.   
  7. import org.apache.hadoop.conf.Configuration;  
  8. import org.apache.hadoop.fs.FileSystem;  
  9. import org.apache.hadoop.fs.Path;  
  10.   
  11.   
  12. public class HDFSTest01 {  
  13.       
  14.     /** 
  15.      * 文件上传 
  16.      * @param src  
  17.      * @param dst 
  18.      * @param conf 
  19.      * @return 
  20.      */  
  21.     public static boolean put2HSFS(String src , String dst , Configuration conf){  
  22.         Path dstPath = new Path(dst) ;  
  23.         try{  
  24.             FileSystem hdfs = dstPath.getFileSystem(conf) ;  
  25. //          FileSystem hdfs = FileSystem.get( URI.create(dst), conf) ;   
  26.             hdfs.copyFromLocalFile(falsenew Path(src), dstPath) ;  
  27.         }catch(IOException ie){  
  28.             ie.printStackTrace() ;  
  29.             return false ;  
  30.         }  
  31.         return true ;  
  32.     }  
  33.       
  34.     /** 
  35.      * 文件下载 
  36.      * @param src 
  37.      * @param dst 
  38.      * @param conf 
  39.      * @return 
  40.      */  
  41.     public static boolean getFromHDFS(String src , String dst , Configuration conf){  
  42.         Path dstPath = new Path(dst) ;  
  43.         try{  
  44.             FileSystem dhfs = dstPath.getFileSystem(conf) ;  
  45.             dhfs.copyToLocalFile(falsenew Path(src), dstPath) ;  
  46.         }catch(IOException ie){  
  47.             ie.printStackTrace() ;  
  48.             return false ;  
  49.         }  
  50.         return true ;  
  51.     }  
  52.       
  53.     /** 
  54.      * 文件检测并删除 
  55.      * @param path 
  56.      * @param conf 
  57.      * @return 
  58.      */  
  59.     public static boolean checkAndDel(final String path , Configuration conf){  
  60.         Path dstPath = new Path(path) ;  
  61.         try{  
  62.             FileSystem dhfs = dstPath.getFileSystem(conf) ;  
  63.             if(dhfs.exists(dstPath)){  
  64.                 dhfs.delete(dstPath, true) ;  
  65.             }else{  
  66.                 return false ;  
  67.             }  
  68.         }catch(IOException ie ){  
  69.             ie.printStackTrace() ;  
  70.             return false ;  
  71.         }  
  72.         return true ;  
  73.     }  
  74.   
  75.   
  76.     /** 
  77.      * @param args 
  78.      */  
  79.     public static void main(String[] args) {  
  80. //      String src = "hdfs://xcloud:9000/user/xcloud/input/core-site.xml" ;   
  81.         String dst = "hdfs://xcloud:9000/user/xcloud/out" ;  
  82.         String src = "/home/xcloud/cdh3/hbase-0.90.4-cdh3u2/bin/loadtable.rb" ;  
  83.         boolean status = false ;  
  84.           
  85.           
  86.         Configuration conf = new Configuration() ;  
  87.         status = put2HSFS( src ,  dst ,  conf) ;  
  88.         System.out.println("status="+status) ;  
  89.           
  90.         src = "hdfs://xcloud:9000/user/xcloud/out/loadtable.rb" ;  
  91.         dst = "/tmp/output" ;  
  92.         status = getFromHDFS( src ,  dst ,  conf) ;  
  93.         System.out.println("status="+status) ;  
  94.           
  95.         src = "hdfs://xcloud:9000/user/xcloud/out/loadtable.rb" ;  
  96.         dst = "/tmp/output/loadtable.rb" ;  
  97.         status = checkAndDel( dst ,  conf) ;  
  98.         System.out.println("status="+status) ;  
  99.     }  
  100.   
  101.   
  102. }  


参考:

hadoop-0.20_程式设计.pdf  见 http://www.linuxidc.com/Linux/2012-01/50878.htm

0 0
原创粉丝点击