{{{ #!html
HDFS 的檔案系統操作
上傳
}}} [wiki:waue/2011/0426 回課程大綱 << ] 第一關 [wiki:waue/2011/0426_2_2 > 下一關 ] = Ex1. 從 local 上傳資料到 hdfs = * 請在 local 端建立資料: /home/hadoop/input * 請檢查 hdfs 無該資料夾: /user/hadoop/program_put_input {{{ #!java 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 HdfsUpload { // 將檔案從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 argv[]){ // String[] argc = { "/tmp/input","program_put_input" }; argv = argc; Configuration conf = new Configuration(); String[] args = new GenericOptionsParser(conf, argv).getRemainingArgs(); if (args.length < 2) { System.out.println("HdfsUpload "); return; } boolean status = putToHdfs(args[0], args[1], conf); System.err.println("Upload? :" + status); } } }}} * 執行後請檢查 hdfs 是否有該資料夾: /user/hadoop/program_put_input