|
额,本来还雄心壮志的打算做一名量产的高达呢,但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死活点不开的...) |
|
|