Changes between Initial Version and Version 1 of NCHCCloudCourse100928_3_EXE


Ignore:
Timestamp:
Sep 27, 2010, 11:27:42 AM (14 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • NCHCCloudCourse100928_3_EXE

    v1 v1  
     1{{{
     2#!html
     3<div style="text-align: center; color:#151B8D"><big style="font-weight: bold;"><big><big>
     4HDFS 的檔案系統操作
     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 回課程大綱 << ] 第一關 [wiki:NCHCCloudCourse100928_3_EXE2 > 下一關 ]
     11
     12= Ex1. 從 local 上傳資料到 hdfs  =
     13
     14 * 請在 local 端建立資料: /home/hadooper/input
     15 * 請檢查 hdfs 無該資料夾: /user/hadooper/program_put_input
     16
     17{{{
     18#!java
     19package org.nchc.hadoop;
     20import java.io.IOException;
     21
     22import org.apache.hadoop.conf.Configuration;
     23import org.apache.hadoop.fs.FileSystem;
     24import org.apache.hadoop.fs.Path;
     25
     26
     27public class PutToHdfs {
     28        // 將檔案從local上傳到 hdfs , src 為 local 的來源, dst 為 hdfs 的目的端
     29        static boolean putToHdfs(String src, String dst, Configuration conf) {
     30                Path dstPath = new Path(dst);
     31                try {
     32                        // 產生操作hdfs的物件
     33                        FileSystem hdfs = dstPath.getFileSystem(conf);
     34                        // 上傳
     35                        hdfs.copyFromLocalFile(false, new Path(src),new Path(dst));
     36
     37                } catch (IOException e) {
     38                        e.printStackTrace();
     39                        return false;
     40                }
     41                return true;
     42        }
     43        static public void main(String args[]){
     44                Configuration conf = new Configuration();
     45                String src = "/home/hadooper/input";
     46                String dst = "/user/hadooper/program_put_input";
     47                boolean status = putToHdfs(src, dst, conf);
     48                System.err.println("create? :" + status);
     49               
     50        }
     51}
     52
     53}}}
     54
     55 * 執行後請檢查 hdfs 是否有該資料夾: /user/hadooper/program_put_input
     56