| 1 | |
| 2 | {{{ |
| 3 | #!java |
| 4 | import java.io.IOException; |
| 5 | |
| 6 | import org.apache.hadoop.conf.Configuration; |
| 7 | import org.apache.hadoop.fs.FileSystem; |
| 8 | import org.apache.hadoop.fs.Path; |
| 9 | |
| 10 | |
| 11 | public class PutToHdfs { |
| 12 | // 將檔案從local上傳到 hdfs , src 為 local 的來源, dst 為 hdfs 的目的端 |
| 13 | static boolean putToHdfs(String src, String dst, Configuration conf) { |
| 14 | Path dstPath = new Path(dst); |
| 15 | try { |
| 16 | // 產生操作hdfs的物件 |
| 17 | FileSystem hdfs = dstPath.getFileSystem(conf); |
| 18 | // 上傳 |
| 19 | hdfs.copyFromLocalFile(false, new Path(src),new Path(dst)); |
| 20 | |
| 21 | } catch (IOException e) { |
| 22 | e.printStackTrace(); |
| 23 | return false; |
| 24 | } |
| 25 | return true; |
| 26 | } |
| 27 | static public void main(String args[]){ |
| 28 | Configuration conf = new Configuration(); |
| 29 | String src = "/home/waue/input"; |
| 30 | String dst = "/user/waue/program_put_input"; |
| 31 | boolean status = putToHdfs(src, dst, conf); |
| 32 | System.err.println("create? :" + status); |
| 33 | |
| 34 | } |
| 35 | } |
| 36 | }}} |