| 1 | |
| 2 | |
| 3 | = Ex1. 從 local 上傳資料到 hdfs = |
| 4 | |
| 5 | * 請在 local 端建立資料: /home/hadooper/input |
| 6 | * 請檢查 hdfs 無該資料夾: /user/hadooper/program_put_input |
| 7 | |
| 8 | {{{ |
| 9 | #!java |
| 10 | package org.nchc.hadoop; |
| 11 | import java.io.IOException; |
| 12 | |
| 13 | import org.apache.hadoop.conf.Configuration; |
| 14 | import org.apache.hadoop.fs.FileSystem; |
| 15 | import org.apache.hadoop.fs.Path; |
| 16 | |
| 17 | |
| 18 | public class PutToHdfs { |
| 19 | // 將檔案從local上傳到 hdfs , src 為 local 的來源, dst 為 hdfs 的目的端 |
| 20 | static boolean putToHdfs(String src, String dst, Configuration conf) { |
| 21 | Path dstPath = new Path(dst); |
| 22 | try { |
| 23 | // 產生操作hdfs的物件 |
| 24 | FileSystem hdfs = dstPath.getFileSystem(conf); |
| 25 | // 上傳 |
| 26 | hdfs.copyFromLocalFile(false, new Path(src),new Path(dst)); |
| 27 | |
| 28 | } catch (IOException e) { |
| 29 | e.printStackTrace(); |
| 30 | return false; |
| 31 | } |
| 32 | return true; |
| 33 | } |
| 34 | static public void main(String args[]){ |
| 35 | Configuration conf = new Configuration(); |
| 36 | String src = "/home/hadooper/input"; |
| 37 | String dst = "/user/hadooper/program_put_input"; |
| 38 | boolean status = putToHdfs(src, dst, conf); |
| 39 | System.err.println("create? :" + status); |
| 40 | |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | }}} |
| 45 | |
| 46 | * 執行後請檢查 hdfs 是否有該資料夾: /user/hadooper/program_put_input |
| 47 | |
| 48 | = Ex2. 從 hdfs 下載資料到 local = |
| 49 | |
| 50 | * 接續 Ex1. 開始作,並檢查 hdfs上有該資料夾: /user/hadooper/program_put_input |
| 51 | * 請檢查 local 無此資料夾: /home/hadooper/program_get_input |
| 52 | |
| 53 | {{{ |
| 54 | #!java |
| 55 | package org.nchc.hadoop; |
| 56 | import java.io.IOException; |
| 57 | import org.apache.hadoop.conf.Configuration; |
| 58 | import org.apache.hadoop.fs.FileSystem; |
| 59 | import org.apache.hadoop.fs.Path; |
| 60 | public class GetFromHdfs { |
| 61 | // 將檔案從hdfs下載回local, src 為 hdfs的來源, dst 為 local 的目的端 |
| 62 | static boolean getFromHdfs(String src,String dst, Configuration conf) { |
| 63 | Path dstPath = new Path(src); |
| 64 | try { |
| 65 | // 產生操作hdfs的物件 |
| 66 | FileSystem hdfs = dstPath.getFileSystem(conf); |
| 67 | // 下載 |
| 68 | hdfs.copyToLocalFile(false, new Path(src),new Path(dst)); |
| 69 | |
| 70 | } catch (IOException e) { |
| 71 | e.printStackTrace(); |
| 72 | return false; |
| 73 | } |
| 74 | return true; |
| 75 | } |
| 76 | static public void main(String args[]){ |
| 77 | Configuration conf = new Configuration(); |
| 78 | String src = "/user/hadooper/program_put_input"; |
| 79 | String dst = "/home/hadooper/program_get_input"; |
| 80 | boolean status = getFromHdfs(src, dst, conf); |
| 81 | System.err.println("create? :" + status); |
| 82 | |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | }}} |
| 87 | |
| 88 | * 檢查 是否有此資料夾: /home/hadooper/program_get_input |
| 89 | |
| 90 | = Ex3. 檢查 hdfs 上的資料,有則刪除之 = |
| 91 | |
| 92 | * 接續 Ex2. 開始作,並檢查 hdfs 有此資料夾: /user/hadooper/program_put_input |
| 93 | |
| 94 | {{{ |
| 95 | #!java |
| 96 | package org.nchc.hadoop; |
| 97 | |
| 98 | import java.io.IOException; |
| 99 | |
| 100 | import org.apache.hadoop.conf.Configuration; |
| 101 | import org.apache.hadoop.fs.FileSystem; |
| 102 | import org.apache.hadoop.fs.Path; |
| 103 | |
| 104 | |
| 105 | public class CheckAndDelete { |
| 106 | // checkAndDelete函式,檢查是否存在該資料夾,若有則刪除之 |
| 107 | static boolean checkAndDelete(final String path, Configuration conf) { |
| 108 | Path dst_path = new Path(path); |
| 109 | try { |
| 110 | // 產生操作hdfs的物件 |
| 111 | FileSystem hdfs = dst_path.getFileSystem(conf); |
| 112 | // 檢查是否存在 |
| 113 | if (hdfs.exists(dst_path)) { |
| 114 | // 有則刪除 |
| 115 | hdfs.delete(dst_path, true); |
| 116 | return true; |
| 117 | }else{ |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | } catch (IOException e) { |
| 122 | e.printStackTrace(); |
| 123 | return false; |
| 124 | |
| 125 | } |
| 126 | |
| 127 | } |
| 128 | static public void main(String args[]){ |
| 129 | Configuration conf = new Configuration(); |
| 130 | String path = "/user/hadooper/program_put_input"; |
| 131 | |
| 132 | boolean status = checkAndDelete( path, conf); |
| 133 | System.err.println("delete? :" + status); |
| 134 | |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | }}} |
| 139 | |
| 140 | * 請檢查 hdfs 無該資料夾: /user/hadooper/program_put_input |