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

[经验分享] 第六篇:Eclipse上运行第一个Hadoop实例

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-12-18 13:22:22 | 显示全部楼层 |阅读模式
1 /**  
  2  *  Licensed under the Apache License, Version 2.0 (the "License");
  
  3  *  you may not use this file except in compliance with the License.
  
  4  *  You may obtain a copy of the License at
  
  5  *
  
  6  *      http://www.apache.org/licenses/LICENSE-2.0
  
  7  *
  
  8  *  Unless required by applicable law or agreed to in writing, software
  
  9  *  distributed under the License is distributed on an "AS IS" BASIS,
  
10  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  
11  *  See the License for the specific language governing permissions and
  
12  *  limitations under the License.
  
13  */
  
14
  
15
  
16 package org.apache.hadoop.examples;
  
17
  
18 import java.io.IOException;
  
19 import java.util.StringTokenizer;
  
20
  
21 //导入各种Hadoop包
  
22 import org.apache.hadoop.conf.Configuration;
  
23 import org.apache.hadoop.fs.Path;
  
24 import org.apache.hadoop.io.IntWritable;
  
25 import org.apache.hadoop.io.Text;
  
26 import org.apache.hadoop.mapreduce.Job;
  
27 import org.apache.hadoop.mapreduce.Mapper;
  
28 import org.apache.hadoop.mapreduce.Reducer;
  
29 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  
30 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  
31 import org.apache.hadoop.util.GenericOptionsParser;
  
32
  
33 // 主类

  
34 public>  
35         
  
36     // Mapper类

  
37     public static>  
38         
  
39         // new一个值为1的整数对象
  
40         private final static IntWritable one = new IntWritable(1);
  
41         // new一个空的Text对象
  
42         private Text word = new Text();
  
43      
  
44         // 实现map函数
  
45         public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
  
46            
  
47             // 创建value的字符串迭代器
  
48             StringTokenizer itr = new StringTokenizer(value.toString());
  
49         
  
50             // 对数据进行再次分割并输出map结果。初始格式为<字节偏移量,单词> 目标格式为<单词,频率>
  
51             while (itr.hasMoreTokens()) {
  
52                     word.set(itr.nextToken());
  
53                     context.write(word, one);
  
54             }
  
55         }
  
56     }
  
57         
  
58     // Reducer类

  
59     public static>  
60     
  
61         // new一个值为空的整数对象
  
62         private IntWritable result = new IntWritable();
  
63
  
64         // 实现reduce函数
  
65         public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
  
66                 
  
67             int sum = 0;
  
68             for (IntWritable val : values) {
  
69                 sum += val.get();
  
70             }
  
71                 
  
72             // 得到本次计算的单词的频数
  
73             result.set(sum);
  
74                        
  
75             // 输出reduce结果
  
76             context.write(key, result);
  
77         }
  
78     }
  
79
  
80     // 主函数
  
81     public static void main(String[] args) throws Exception {
  
82     
  
83         // 获取配置参数
  
84         Configuration conf = new Configuration();
  
85         String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
  
86                 
  
87         // 检查命令语法
  
88         if (otherArgs.length != 2) {
  
89                 System.err.println("Usage: wordcount <in> <out>");
  
90                 System.exit(2);
  
91         }
  
92                 
  
93         // 定义作业对象
  
94         Job job = new Job(conf, "word count");
  
95         // 注册分布式类
  
96         job.setJarByClass(WordCount.class);
  
97         // 注册Mapper类
  
98         job.setMapperClass(TokenizerMapper.class);
  
99         // 注册合并类
  
100         job.setCombinerClass(IntSumReducer.class);
  
101         // 注册Reducer类
  
102         job.setReducerClass(IntSumReducer.class);
  
103         // 注册输出格式类
  
104         job.setOutputKeyClass(Text.class);
  
105         job.setOutputValueClass(IntWritable.class);
  
106         // 设置输入输出路径
  
107         FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  
108         FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
  
109                 
  
110         // 运行程序
  
111         System.exit(job.waitForCompletion(true) ? 0 : 1);
  
112     }
  
113 }

运维网声明 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-425379-1-1.html 上篇帖子: 零基础学习hadoop到上手工作线路指导(编程篇) 下篇帖子: Hadoop HDFS编程 API入门系列之简单综合版本1(四)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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