wiki:NCHCCloudCourse100928_3_EXE2
HDFS 的檔案系統操作
下載

上一關 < 第二關 > 下一關

Ex2. 從 hdfs 下載資料到 local

  • 接續 Ex1. 開始作,並檢查 hdfs上有該資料夾: /user/hadoop/program_put_input
  • 請檢查 local 無此資料夾: /home/hadoop/program_get_input
package org.nchc.hadoop;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class GetFromHdfs {
  // 將檔案從hdfs下載回local, src 為 hdfs的來源, dst 為 local 的目的端
  static boolean getFromHdfs(String src,String dst, Configuration conf) {
    Path dstPath = new Path(src);
    try {
      // 產生操作hdfs的物件
      FileSystem hdfs = dstPath.getFileSystem(conf);
      // 下載
      hdfs.copyToLocalFile(false, new Path(src),new Path(dst));
      
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }
  static public void main(String args[]){
    Configuration conf = new Configuration();
    String src = "/user/hadoop/program_put_input";
    String dst = "/home/hadoop/program_get_input";
    boolean status = getFromHdfs(src, dst, conf);
    System.err.println("download? :" + status);
    
  }
}

  • 檢查 是否有此資料夾: /home/hadoop/program_get_input
Last modified 14 years ago Last modified on Sep 28, 2010, 10:43:04 AM