HDFS 使用Java api實現上傳/下載/刪除檔案
阿新 • • 發佈:2019-01-10
- 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 {
- /**
- * 檔案上傳
- * @param src
- * @param dst
- * @param conf
- * @return
- */
- public static boolean
- Path dstPath = new Path(dst) ;
- try{
- FileSystem hdfs = dstPath.getFileSystem(conf) ;
- // FileSystem hdfs = FileSystem.get( URI.create(dst), conf) ;
- hdfs.copyFromLocalFile(false, new Path(src), dstPath) ;
- }catch(IOException ie){
- ie.printStackTrace() ;
- return false ;
- }
- return true ;
- }
- /**
- * 檔案下載
- * @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 ;
- }
- /**
- * 檔案檢測並刪除
- * @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 args
- */
- public static void main(String[] args) {
- // String src = "hdfs://xcloud:9000/user/xcloud/input/core-site.xml" ;
- String dst = "hdfs://xcloud:9000/user/xcloud/out" ;
- String src = "/home/xcloud/cdh3/hbase-0.90.4-cdh3u2/bin/loadtable.rb" ;
- boolean status = false ;
- Configuration conf = new Configuration() ;
- status = put2HSFS( src , dst , conf) ;
- System.out.println("status="+status) ;
- src = "hdfs://xcloud:9000/user/xcloud/out/loadtable.rb" ;
- dst = "/tmp/output" ;
- status = getFromHDFS( src , dst , conf) ;
- System.out.println("status="+status) ;
- src = "hdfs://xcloud:9000/user/xcloud/out/loadtable.rb" ;
- dst = "/tmp/output/loadtable.rb" ;
- status = checkAndDel( dst , conf) ;
- System.out.println("status="+status) ;
- }
- }