1. 程式人生 > >HDFS 使用Java api實現上傳/下載/刪除檔案

HDFS 使用Java api實現上傳/下載/刪除檔案

  1. import java.io.IOException;  
  2. import org.apache.hadoop.conf.Configuration;  
  3. import org.apache.hadoop.fs.FileSystem;  
  4. import org.apache.hadoop.fs.Path;  
  5. public class HDFSTest01 {  
  6.     /** 
  7.      * 檔案上傳 
  8.      * @param src  
  9.      * @param dst 
  10.      * @param conf 
  11.      * @return 
  12.      */  
  13.     public static boolean
     put2HSFS(String src , String dst , Configuration conf){  
  14.         Path dstPath = new Path(dst) ;  
  15.         try{  
  16.             FileSystem hdfs = dstPath.getFileSystem(conf) ;  
  17. //          FileSystem hdfs = FileSystem.get( URI.create(dst), conf) ;   
  18.             hdfs.copyFromLocalFile(falsenew Path(src), dstPath) ;  
  19.         }catch(IOException ie){  
  20.             ie.printStackTrace() ;  
  21.             return false ;  
  22.         }  
  23.         return true ;  
  24.     }  
  25.     /** 
  26.      * 檔案下載 
  27.      * @param src 
  28.      * @param dst 
  29.      * @param conf 
  30.      * @return 
  31.      */  
  32.     public static boolean getFromHDFS(String src , String dst , Configuration conf){  
  33.         Path dstPath = new Path(dst) ;  
  34.         try{  
  35.             FileSystem dhfs = dstPath.getFileSystem(conf) ;  
  36.             dhfs.copyToLocalFile(falsenew Path(src), dstPath) ;  
  37.         }catch(IOException ie){  
  38.             ie.printStackTrace() ;  
  39.             return false ;  
  40.         }  
  41.         return true ;  
  42.     }  
  43.     /** 
  44.      * 檔案檢測並刪除 
  45.      * @param path 
  46.      * @param conf 
  47.      * @return 
  48.      */  
  49.     public static boolean checkAndDel(final String path , Configuration conf){  
  50.         Path dstPath = new Path(path) ;  
  51.         try{  
  52.             FileSystem dhfs = dstPath.getFileSystem(conf) ;  
  53.             if(dhfs.exists(dstPath)){  
  54.                 dhfs.delete(dstPath, true) ;  
  55.             }else{  
  56.                 return false ;  
  57.             }  
  58.         }catch(IOException ie ){  
  59.             ie.printStackTrace() ;  
  60.             return false ;  
  61.         }  
  62.         return true ;  
  63.     }  
  64.     /** 
  65.      * @param args 
  66.      */  
  67.     public static void main(String[] args) {  
  68. //      String src = "hdfs://xcloud:9000/user/xcloud/input/core-site.xml" ;   
  69.         String dst = "hdfs://xcloud:9000/user/xcloud/out" ;  
  70.         String src = "/home/xcloud/cdh3/hbase-0.90.4-cdh3u2/bin/loadtable.rb" ;  
  71.         boolean status = false ;  
  72.         Configuration conf = new Configuration() ;  
  73.         status = put2HSFS( src ,  dst ,  conf) ;  
  74.         System.out.println("status="+status) ;  
  75.         src = "hdfs://xcloud:9000/user/xcloud/out/loadtable.rb" ;  
  76.         dst = "/tmp/output" ;  
  77.         status = getFromHDFS( src ,  dst ,  conf) ;  
  78.         System.out.println("status="+status) ;  
  79.         src = "hdfs://xcloud:9000/user/xcloud/out/loadtable.rb" ;  
  80.         dst = "/tmp/output/loadtable.rb" ;  
  81.         status = checkAndDel( dst ,  conf) ;  
  82.         System.out.println("status="+status) ;  
  83.     }  
  84. }