[0010] windows 下 eclipse 開發 hdfs程式範例 (二)

來源:互聯網
上載者:User

標籤:環境   jar   ati   ica   ges   out   main   system   nbsp   

目的:

學習windows 開發hadoop程式的配置

相關:

[0007] windows 下 eclipse 開發 hdfs程式範例

環境:

基於以下環境配置好後。

[0008] Windows 7 下 hadoop 2.6.4 eclipse 本地開發調試配置

 

1. 建立HDFS下載檔案類

在已有mapreduce項目中建立類添加如下代碼,代碼從[0007]中取出小修改

功能:從hdfs下載檔案到windows本地

package hadoop.hdfs;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import org.apache.commons.compress.utils.IOUtils;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataInputStream;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;/** *  功能:      將 hdfs://ssmaster:9000/data/paper.txt下載到Windows下c:\paper.txt *  調用方式:hadoop jar 打包包名.jar   */public class Hdfs_Download {    public static void main(String[] args) {            Configuration conf =new Configuration();        FileSystem fs = null;    Path src = null;    FSDataInputStream in = null;    FileOutputStream out = null;      //    src = new Path("hdfs://ssmaster:9000/data/paper.txt" );        src = new Path("hdfs://ssmaster:9000/data/paper.txt" );        try {              fs = FileSystem.get(conf) ;      in = fs.open(src);       } catch (IOException e) {        e.printStackTrace();     }        try {        out = new FileOutputStream ("c:\\paper.txt");    } catch (FileNotFoundException e) {        e.printStackTrace();    }        try {        IOUtils.copy(in, out);    } catch (IOException e) {        e.printStackTrace();    }}}
View Code

 

2.複製hadoop配置好的core-site.xml,hdfs-site.xml,mapred-site.xml到src目錄下

 

3 運行

 右鍵類名,運行為 java application,正常會執行成功

 

異常

參考[0009]hadoop異常處理,一般都是設定檔沒有

 

總結:

比[0007]打包執行方便一些。

存在問題:

如果打包到linux下執行,需要修改路徑

如何在eclipse中,直接跑程式,下載hdfs到linxu本地

[0010] windows 下 eclipse 開發 hdfs程式範例 (二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.