| | 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:waue/2011/0426_2 第一關 << ] 第一關 [wiki:waue/2011/0426_2_3 > 下一關 ] |
| | 11 | |
| | 12 | = Ex2. 從 hdfs 下載資料到 local = |
| | 13 | |
| | 14 | * 接續 Ex1. 開始作,並檢查 hdfs上有該資料夾: /user/hadoop/program_put_input |
| | 15 | * 請檢查 local 無此資料夾: /home/hadoop/program_get_input |
| | 16 | |
| | 17 | {{{ |
| | 18 | #!java |
| | 19 | |
| | 20 | package itri; |
| | 21 | |
| | 22 | import java.io.IOException; |
| | 23 | |
| | 24 | import org.apache.hadoop.conf.Configuration; |
| | 25 | import org.apache.hadoop.fs.FileSystem; |
| | 26 | import org.apache.hadoop.fs.Path; |
| | 27 | import org.apache.hadoop.util.GenericOptionsParser; |
| | 28 | |
| | 29 | |
| | 30 | public class HdfsDownload { |
| | 31 | // 將檔案從hdfs下載回local, src 為 hdfs的來源, dst 為 local 的目的端 |
| | 32 | static String getFromHdfs(String src,String dst, Configuration conf) { |
| | 33 | Path dstPath = new Path(src); |
| | 34 | try { |
| | 35 | // 產生操作hdfs的物件 |
| | 36 | FileSystem hdfs = dstPath.getFileSystem(conf); |
| | 37 | // 下載 |
| | 38 | if (hdfs.exists(new Path(src))){ |
| | 39 | hdfs.copyToLocalFile(false, new Path(src),new Path(dst)); |
| | 40 | return "ok"; |
| | 41 | }else{ |
| | 42 | return src+" not found"; |
| | 43 | } |
| | 44 | } catch (IOException e) { |
| | 45 | e.printStackTrace(); |
| | 46 | return "Error"; |
| | 47 | } |
| | 48 | |
| | 49 | } |
| | 50 | static public void main(String argv[]){ |
| | 51 | // String[] argc = { "input", "/tmp/myinput1" }; argv = argc; |
| | 52 | |
| | 53 | Configuration conf = new Configuration(); |
| | 54 | String[] args = new GenericOptionsParser(conf, argv).getRemainingArgs(); |
| | 55 | if (args.length < 2) { |
| | 56 | System.out.println("HdfsDownload <hdfs_path> <local_path>"); |
| | 57 | return; |
| | 58 | } |
| | 59 | |
| | 60 | String status = getFromHdfs(args[0], args[1], conf); |
| | 61 | System.err.println("Download hdfs:\""+ args[0] +"\" to local:\""+ args[1] +"\" ? =>" + status); |
| | 62 | |
| | 63 | } |
| | 64 | } |
| | 65 | }}} |
| | 66 | |
| | 67 | * 檢查 是否有此資料夾: /home/hadoop/program_get_input |
| | 68 | |