wiki:waue/2011/0426_2_2
HDFS 的檔案系統操作
下載

第一關 << 第一關 > 下一關

Ex2. 從 hdfs 下載資料到 local

  • 接續 Ex1. 開始作,並檢查 hdfs上有該資料夾: /user/hadoop/program_put_input
  • 請檢查 local 無此資料夾: /home/hadoop/program_get_input
package itri;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.util.GenericOptionsParser;


public class HdfsDownload {
  // 將檔案從hdfs下載回local, src 為 hdfs的來源, dst 為 local 的目的端
  static String getFromHdfs(String src,String dst, Configuration conf) {
    Path dstPath = new Path(src);
    try {
      // 產生操作hdfs的物件
      FileSystem hdfs = dstPath.getFileSystem(conf);
      // 下載
      if (hdfs.exists(new Path(src))){
        hdfs.copyToLocalFile(false, new Path(src),new Path(dst));
        return "ok";
      }else{
        return src+" not found";
      }
    } catch (IOException e) {
      e.printStackTrace();
      return "Error";
    }
    
  }
  static public void main(String argv[]){
//    String[] argc = { "input", "/tmp/myinput1" }; argv = argc;
    
    Configuration conf = new Configuration();
    String[] args = new GenericOptionsParser(conf, argv).getRemainingArgs();
    if (args.length < 2) {
      System.out.println("HdfsDownload <hdfs_path> <local_path>");
      return;
    }
    
    String status = getFromHdfs(args[0], args[1], conf);
    System.err.println("Download hdfs:\""+ args[0] +"\" to local:\""+ args[1] +"\" ? =>" + status);
    
  }
}
  • 檢查 是否有此資料夾: /home/hadoop/program_get_input
Last modified 13 years ago Last modified on Apr 25, 2011, 3:24:12 PM