wiki:NCHCCloudCourse100928_3_HDFS
HDFS 的檔案系統操作
上傳、下載、刪除

Ex1. 從 local 上傳資料到 hdfs

  • 請在 local 端建立資料: /home/hadooper/input
  • 請檢查 hdfs 無該資料夾: /user/hadooper/program_put_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 PutToHdfs {
  // 將檔案從local上傳到 hdfs , src 為 local 的來源, dst 為 hdfs 的目的端
  static boolean putToHdfs(String src, String dst, Configuration conf) {
    Path dstPath = new Path(dst);
    try {
      // 產生操作hdfs的物件
      FileSystem hdfs = dstPath.getFileSystem(conf);
      // 上傳
      hdfs.copyFromLocalFile(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 = "/home/hadooper/input";
    String dst = "/user/hadooper/program_put_input";
    boolean status = putToHdfs(src, dst, conf);
    System.err.println("create? :" + status);
    
  }
}

  • 執行後請檢查 hdfs 是否有該資料夾: /user/hadooper/program_put_input

Ex2. 從 hdfs 下載資料到 local

  • 接續 Ex1. 開始作,並檢查 hdfs上有該資料夾: /user/hadooper/program_put_input
  • 請檢查 local 無此資料夾: /home/hadooper/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/hadooper/program_put_input";
    String dst = "/home/hadooper/program_get_input";
    boolean status = getFromHdfs(src, dst, conf);
    System.err.println("create? :" + status);
    
  }
}

  • 檢查 是否有此資料夾: /home/hadooper/program_get_input

Ex3. 檢查 hdfs 上的資料,有則刪除之

  • 接續 Ex2. 開始作,並檢查 hdfs 有此資料夾: /user/hadooper/program_put_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 CheckAndDelete {
  // checkAndDelete函式,檢查是否存在該資料夾,若有則刪除之
  static boolean checkAndDelete(final String path, Configuration conf) {
    Path dst_path = new Path(path);
    try {
      // 產生操作hdfs的物件
      FileSystem hdfs = dst_path.getFileSystem(conf);
      // 檢查是否存在
      if (hdfs.exists(dst_path)) {
        // 有則刪除
        hdfs.delete(dst_path, true);
        return true;
      }else{
        return false;
      }

    } catch (IOException e) {
      e.printStackTrace();
      return false;
      
    }
    
  }
  static public void main(String args[]){
    Configuration conf = new Configuration();
    String path = "/user/hadooper/program_put_input";

    boolean status = checkAndDelete( path, conf);
    System.err.println("delete? :" + status);
    
  }
}

  • 請檢查 hdfs 無該資料夾: /user/hadooper/program_put_input
Last modified 14 years ago Last modified on Sep 24, 2010, 6:01:18 PM