[b0011] windows 下 eclipse 开发 hdfs程序样例 (三)

目的:

学习windows 开发hadoop程序的配置。

 

[b0007] windows 下 eclipse 开发 hdfs程序样例 

太麻烦

[b0010] windows 下 eclipse 开发 hdfs程序样例 (二)

输出日志变化,而且配置似乎很麻烦。

 

环境:

windows 7 64下 eclipse

 

说明:

该实践是在[0008] Windows 7 下 hadoop 2.6.4 eclipse 本地开发调试配置 中设置后进行的,

在这里面进行了一些环境变量设置、插件安装。 

如果按照以下步骤进行,还有报错的话,可以参考这篇文章,以及问题[0009] 解决

 

1.新建项目

新建项目、导入hadoop开发包

详细参考

[0007] windows 下 eclipse 开发 hdfs程序样例  1 新建项目

 

2.新建类,编写如下代码  

 1 package hdfs;
 2 
 3 import java.io.FileNotFoundException;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 
 7 import org.apache.commons.compress.utils.IOUtils;
 8 import org.apache.hadoop.conf.Configuration;
 9 import org.apache.hadoop.fs.FSDataInputStream;
10 import org.apache.hadoop.fs.FileSystem;
11 import org.apache.hadoop.fs.Path;
12 
13 /**
14  *  功能:      将 hdfs://ssmaster:9000/data/paper.txt下载到Windows下c:\paper.txt
15  *  调用方式:hadoop jar 打包包名.jar  
16  */
17 
18 public class Down_Load {
19 
20     public static void main(String[] args) {
21         
22     Configuration conf =new Configuration();
23     conf.set("fs.defaultFS", "hdfs://ssmaster:9000/");
24     
25     FileSystem fs = null;
26     Path src = null;
27     FSDataInputStream in = null;
28     FileOutputStream out = null;
29       
30     src = new Path("hdfs://ssmaster:9000/data/paper.txt" );
31     
32     try {
33         
34       fs = FileSystem.get(conf) ;
35       in = fs.open(src);
36 
37        } catch (IOException e) {
38         e.printStackTrace(); 
39     }
40     
41     try {
42         out = new FileOutputStream ("c:\\paper.txt"); //等效  c:/paper.txt
43     } catch (FileNotFoundException e) {
44         e.printStackTrace();
45     }
46     
47     try {
48         IOUtils.copy(in, out);
49     } catch (IOException e) {
50         e.printStackTrace();
51     }
52 
53 }
54 }
View Code

备注: 在[0007] 第2步的代码上 增加 conf.set("fs.defaultFS", "hdfs://ssmaster:9000/");

 

3 执行

右键->run java application

 

总结:

HDFS下的windows开发环境基本搭建好。

 

posted @ 2016-10-25 11:21  sunzebo  阅读(421)  评论(0编辑  收藏  举报