设为首页 收藏本站
查看: 681|回复: 0

[经验分享] Hadoop伪分布式搭建以及入手小例子——面向纯新手(下)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-7-13 10:23:41 | 显示全部楼层 |阅读模式
  额,本来还雄心壮志的打算做一名量产的高达呢,但Boss不遂人愿,一封邮件让我回到解放前...
  邮件的主旨就是四个大字 “ 下个月加班不设上限!”,看来我朝9晚9的幸福生活要结束了 T T  先让我哭会... 哎,真不知道在坚持什么...
  但是,既然决定要开始,就应该坚持下去,上一篇还有童鞋提了建议了呢,作为一名光荣的团员,不能让团组织失望!
  继续上篇,伪环境已经搭建起来了,然后就可以举个栗子了...我也不能上WordCount啊... 就来个类似权威指南里 算气温的吧...
  统计气温有很多API,最早用的是Google的,后来不知道怎么的(难道这也河蟹?)打不开了,就转战Yahoo的了。Yahoo 也挺好的,提供从今天起的5天气温,但是貌似没啥图,没有 国家气象局的 好看,并且是xml格式的,各有优势吧~
  首先先收集数据,部分代码:



1 File file = null;
2 RandomAccessFile file1 = null;
3
4 file = new File("D:/temperature");
5 file.createNewFile();
6 file1 = new RandomAccessFile(file,"rw");
7
8 String proxyIP = "000.000.000.000";    //公司代理网址
9 int proxyPort = 0000;    //公司代理端口
10 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIP, proxyPort));
11
12 final String username = "Rex";    //用户名
13 final String password = "Rex";    //密码
14 Authenticator auth = new Authenticator(){
15     private PasswordAuthentication pa = new PasswordAuthentication(username, password.toCharArray());
16     protected PasswordAuthentication getPasswordAuthentication(){
17         return pa;
18     }
19 };
20
21 Authenticator.setDefault(auth); //在公司上网还需要配代理...哎...
22
23 URL url = new URL("http://weather.yahooapis.com/forecastrss?p=CHXX0019&u=c"); //0019是大连在Yahoo DB里的号
24 URLConnection urlConn = url.openConnection(proxy);
25
26 InputStreamReader isr = new InputStreamReader(urlConn.getInputStream(), "utf-8");
27 BufferedReader br = new BufferedReader(isr);
28
29 String line = "";
30 while((line=br.readLine())!=null){
31     if(line.indexOf("yweather:forecast")!=-1){
32         file1.writeBytes(line);
33         file1.writeBytes("\r\n");
34     }
35 }
  得到的文件大概是这样:








  之后把生成的这个文件上传到CentOS里,我弄到了 /home/rex/Documents,之后要记得把文件上传到HDFS中:



hadoop dfs -copyFromLocal temperature temperature
  好了,东西都准备齐全了,开始写Hadoop Demo了。
  打开熟悉的Eclipse 新建一个Maven Project(最简单的java项目就行),添加依赖:




org.apache.hadoop
hadoop-core
1.2.1
   
  新建一个Class:WeatherTask 咱们的主程序,先写mapper,在写reducer,最后就可以定义job了...具体为什么以后再看书,先让他跑起来...
  部分代码:



1 package com.RexSoft.Demo;
2
3 import java.io.IOException;
4
5 import org.apache.hadoop.fs.Path;
6 import org.apache.hadoop.io.IntWritable;
7 import org.apache.hadoop.io.LongWritable;
8 import org.apache.hadoop.io.Text;
9 import org.apache.hadoop.mapreduce.Job;
10 import org.apache.hadoop.mapreduce.Mapper;
11 import org.apache.hadoop.mapreduce.Reducer;
12 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
13 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
14
15 /**
16  * @ClassName: WeatherTask
17  * @Description: 近五天大连天气 最高气温
18  * @author Rex
19  * @date 2014年3月27日 下午1:54:39
20  */
21 public class WeatherTask {
22
23     public static void main(String[] args) {
24         
25         try {
26             Job job = new Job();
27             job.setJarByClass(WeatherTask.class);
28            
29             FileInputFormat.addInputPath(job, new Path(args[0]));
30             FileOutputFormat.setOutputPath(job, new Path(args[1]));
31            
32             job.setMapperClass(MyMapper.class);
33             job.setReducerClass(MyReducer.class);
34             job.setOutputKeyClass(Text.class);
35             job.setOutputValueClass(IntWritable.class);
36            
37             System.exit( job.waitForCompletion(true) ? 0 : 1);
38            
39         } catch (IOException e) {
40             e.printStackTrace();
41         } catch (InterruptedException e) {
42             e.printStackTrace();
43         } catch (ClassNotFoundException e) {
44             e.printStackTrace();
45         }
46     }
47     
48     /**
49      * @ClassName: MyMapper
50      * @Description: Mapper
51      */
52     static class MyMapper extends Mapper{
53         
54         public void map(LongWritable key, Text value , Context context){
55            
56             String line = value.toString();
57             int highTemp = Integer.parseInt(line.split("high=\"")[1].split("\"")[0]);
58             try {
59                 context.write(new Text("大连本周最高气温"), new IntWritable(highTemp));
60             } catch (IOException e) {
61                 e.printStackTrace();
62             } catch (InterruptedException e) {
63                 e.printStackTrace();
64             }
65            
66         }
67         
68     }
69
70     /**
71      * @ClassName: MyReducer
72      * @Description: Reducer
73      */
74     static class MyReducer extends Reducer{
75         
76         public void reduce(Text key, Iterable values, Context context){
77            
78             int high = 0;
79             for(IntWritable v : values){
80                 high = Math.max(v.get(),high);
81             }
82             try {
83                 context.write(key, new IntWritable(high));
84             } catch (IOException e) {
85                 e.printStackTrace();
86             } catch (InterruptedException e) {
87                 e.printStackTrace();
88             }
89         }
90         
91     }
92 }
  好了,打jar包,传到CentOS里
执行:



hadoop jar Demo.jar com.RexSoft.Demo.WeatherTask temperature out
  如果没报错的好就可以查看结果了~



hadoop dfs -cat /user/root/out/part-r-00000
  结果:



[iyunv@localhost logs]# hadoop dfs -cat /user/root/out/part-r-00000
Warning: $HADOOP_HOME is deprecated.
大连本周最高气温        14
  也可以使用Web监控页查看:

  好了,至此第一个Hadoop小例子算是跑起来了,之后就可以开始进入这个大坑了 - -!
  上一篇有童鞋留言说,太简陋了,的确啊,确实非常简陋,但也非常容易,所以才面向新手的,旨在建立自信...
  PS:源码可从Github上pull https://github.com/Rex---/HadoopDemo (在这里问下大家,有没有收到Atom的invite,但是点link死活点不开的...)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-86199-1-1.html 上篇帖子: Hadoop系列:在Linux下部署hadoop 0.20.1 下篇帖子: Hadoop安装、Hadoop环境搭建(Apache)版本
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表