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