| 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_3_EXE 上一關 < ] 第二關 [wiki:NCHCCloudCourse100928_3_EXE3 > 下一關] |
| 11 | |
| 12 | = Ex2. 從 hdfs 下載資料到 local = |
| 13 | |
| 14 | * 接續 Ex1. 開始作,並檢查 hdfs上有該資料夾: /user/hadooper/program_put_input |
| 15 | * 請檢查 local 無此資料夾: /home/hadooper/program_get_input |
| 16 | |
| 17 | {{{ |
| 18 | #!java |
| 19 | package org.nchc.hadoop; |
| 20 | import java.io.IOException; |
| 21 | import org.apache.hadoop.conf.Configuration; |
| 22 | import org.apache.hadoop.fs.FileSystem; |
| 23 | import org.apache.hadoop.fs.Path; |
| 24 | public class GetFromHdfs { |
| 25 | // 將檔案從hdfs下載回local, src 為 hdfs的來源, dst 為 local 的目的端 |
| 26 | static boolean getFromHdfs(String src,String dst, Configuration conf) { |
| 27 | Path dstPath = new Path(src); |
| 28 | try { |
| 29 | // 產生操作hdfs的物件 |
| 30 | FileSystem hdfs = dstPath.getFileSystem(conf); |
| 31 | // 下載 |
| 32 | hdfs.copyToLocalFile(false, new Path(src),new Path(dst)); |
| 33 | |
| 34 | } catch (IOException e) { |
| 35 | e.printStackTrace(); |
| 36 | return false; |
| 37 | } |
| 38 | return true; |
| 39 | } |
| 40 | static public void main(String args[]){ |
| 41 | Configuration conf = new Configuration(); |
| 42 | String src = "/user/hadooper/program_put_input"; |
| 43 | String dst = "/home/hadooper/program_get_input"; |
| 44 | boolean status = getFromHdfs(src, dst, conf); |
| 45 | System.err.println("create? :" + status); |
| 46 | |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | }}} |
| 51 | |
| 52 | * 檢查 是否有此資料夾: /home/hadooper/program_get_input |
| 53 | |