| | 1 | {{{ |
| | 2 | #!html |
| | 3 | <div style="text-align: center; color:#151B8D"><big style="font-weight: bold;"><big><big> |
| | 4 | HDFS 的檔案系統操作 |
| | 5 | </big></big></big></div> <div style="text-align: center; color:#7E2217"><big style="font-weight: bold;"><big> |
| | 6 | 上傳 |
| | 7 | </big></big></div> |
| | 8 | }}} |
| | 9 | |
| | 10 | [wiki:NCHCCloudCourse100928 回課程大綱 << ] 第一關 [wiki:NCHCCloudCourse100928_3_EXE2 > 下一關 ] |
| | 11 | |
| | 12 | = Ex1. 從 local 上傳資料到 hdfs = |
| | 13 | |
| | 14 | * 請在 local 端建立資料: /home/hadooper/input |
| | 15 | * 請檢查 hdfs 無該資料夾: /user/hadooper/program_put_input |
| | 16 | |
| | 17 | {{{ |
| | 18 | #!java |
| | 19 | package org.nchc.hadoop; |
| | 20 | import java.io.IOException; |
| | 21 | |
| | 22 | import org.apache.hadoop.conf.Configuration; |
| | 23 | import org.apache.hadoop.fs.FileSystem; |
| | 24 | import org.apache.hadoop.fs.Path; |
| | 25 | |
| | 26 | |
| | 27 | public class PutToHdfs { |
| | 28 | // 將檔案從local上傳到 hdfs , src 為 local 的來源, dst 為 hdfs 的目的端 |
| | 29 | static boolean putToHdfs(String src, String dst, Configuration conf) { |
| | 30 | Path dstPath = new Path(dst); |
| | 31 | try { |
| | 32 | // 產生操作hdfs的物件 |
| | 33 | FileSystem hdfs = dstPath.getFileSystem(conf); |
| | 34 | // 上傳 |
| | 35 | hdfs.copyFromLocalFile(false, new Path(src),new Path(dst)); |
| | 36 | |
| | 37 | } catch (IOException e) { |
| | 38 | e.printStackTrace(); |
| | 39 | return false; |
| | 40 | } |
| | 41 | return true; |
| | 42 | } |
| | 43 | static public void main(String args[]){ |
| | 44 | Configuration conf = new Configuration(); |
| | 45 | String src = "/home/hadooper/input"; |
| | 46 | String dst = "/user/hadooper/program_put_input"; |
| | 47 | boolean status = putToHdfs(src, dst, conf); |
| | 48 | System.err.println("create? :" + status); |
| | 49 | |
| | 50 | } |
| | 51 | } |
| | 52 | |
| | 53 | }}} |
| | 54 | |
| | 55 | * 執行後請檢查 hdfs 是否有該資料夾: /user/hadooper/program_put_input |
| | 56 | |