第一次写MapReduce之WordCount实例
2016-08-04 07:37
423 查看
步骤如下:
1. 安装JDK,目前我的版本是1.7
2. 安装eclipse
3. 创建项目firstWordCountInstance
4. 导入开发需要的jar包
这步需要下载hadoop源码,然后在share目录下可以找到很多相关jar包,我的目录是hadoop-2.6.0-x64\hadoop-2.6.0\share\hadoop,有如下文件夹,为保险起见,全部导入到项目中去:
如下是导入后的jar包:
5.配置HADOOP_HOME
5.1.下载winutils的windows版本
GitHub上,有人提供了winutils的windows的版本,项目地址是:https://github.com/srccodes/hadoop-common-2.2.0-bin 直接下载此项目的zip包,下载后是文件名是hadoop-common-2.2.0-bin-master.zip,随便解压到一个目录
5.2.配置环境变量
增加用户变量HADOOP_HOME,值是下载的zip包解压的目录,然后在系统变量path里增加%HADOOP_HOME%\bin 即可。
6. 编写WordCount程序
7. 打jar包并上传到linux服务器
如下图:
8. 把readme.txt文件上传到hadoop文件目录下
参考命令如下:
9. 运行jar包
至此,运行结束
1. 安装JDK,目前我的版本是1.7
2. 安装eclipse
3. 创建项目firstWordCountInstance
4. 导入开发需要的jar包
这步需要下载hadoop源码,然后在share目录下可以找到很多相关jar包,我的目录是hadoop-2.6.0-x64\hadoop-2.6.0\share\hadoop,有如下文件夹,为保险起见,全部导入到项目中去:
如下是导入后的jar包:
5.配置HADOOP_HOME
5.1.下载winutils的windows版本
GitHub上,有人提供了winutils的windows的版本,项目地址是:https://github.com/srccodes/hadoop-common-2.2.0-bin 直接下载此项目的zip包,下载后是文件名是hadoop-common-2.2.0-bin-master.zip,随便解压到一个目录
5.2.配置环境变量
增加用户变量HADOOP_HOME,值是下载的zip包解压的目录,然后在系统变量path里增加%HADOOP_HOME%\bin 即可。
6. 编写WordCount程序
package com.lyh; import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.*; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser; public class WordCount { //嵌套类 Mapper //Mapper<keyin,valuein,keyout,valueout> public static class WordCountMapper extends Mapper<Object, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); @Override protected void map(Object key, Text value, Context context) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); while(itr.hasMoreTokens()){ word.set(itr.nextToken()); context.write(word, one);//Context机制 } } } //嵌套类Reducer //Reduce<keyin,valuein,keyout,valueout> //Reducer的valuein类型要和Mapper的va lueout类型一致,Reducer的valuein是Mapper的valueout经过shuffle之后的值 public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{ private IntWritable result = new IntWritable(); @Override protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for(IntWritable i:values){ sum += i.get(); } result.set(sum); context.write(key,result);//Context机制 } } public static void main(String[] args) throws Exception{ Configuration conf = new Configuration();//获得Configuration配置 Configuration: core-default.xml, core-site.xml String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();//获得输入参数 [hdfs://localhost:9000/user/dat/input, hdfs://localhost:9000/user/dat/output] if(otherArgs.length != 2){//判断输入参数个数,不为两个异常退出 System.err.println("Usage:wordcount <in> <out>"); System.exit(2); } ////设置Job属性 Job job = new Job(conf,"word count"); job.setJarByClass(WordCount.class); job.setMapperClass(WordCountMapper.class); job.setCombinerClass(WordCountReducer.class);//将结果进行局部合并 job.setReducerClass(WordCountReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//传入input path FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//传入output path,输出路径应该为空,否则报错org.apache.hadoop.mapred.FileAlreadyExistsException。 System.exit(job.waitForCompletion(true)?0:1);//是否正常退出 } }正常运行
7. 打jar包并上传到linux服务器
如下图:
8. 把readme.txt文件上传到hadoop文件目录下
参考命令如下:
hadoop fs -put /home/mart_cmo/task/lyh/readme.txt hdfs://ns1/user/mart_cmo/test其中readme.txt文件内容如下:
configuration options from Spyder versions previous to They way did
9. 运行jar包
hadoop jar /home/mart_cmo/task/lyh/wordcount.jar com.lyh.WordCount hdfs://ns1/user/mart_cmo/test/readme.txt hdfs://ns1/user/mart_cmo/test/output2并从output2中查看结果:
至此,运行结束
相关文章推荐
- C#入门3.4——数据类型之引用类型
- 昨天晚上同样算加班,7点菜回来
- 订餐系统:MUI开发手机端前台,页面加载后number-box增减按钮不可用
- c++运算符重载总结
- Yii 2.0 权威指南 (4) 使用 Gii 生成代码
- usbip 试用记
- 使用AccessibilityService进行APK自动安装
- 【LEETCODE】375- Guess Number Higher or Lower II [Python]
- maven学习(下)利用Profile构建不同环境的部署包
- C++拷贝构造函数(深拷贝,浅拷贝)
- 1087 麦森数
- 【ES6】函数的扩展(1)
- react native use map function
- Python xml 解析百度糯米信息
- 在.NET开发面向Oracle数据库的应用程序
- 【★】致全球第一批全帧3D游戏!
- 【★】致全球第一批全帧3D游戏!
- 【★】致全球第一批全帧3D游戏!
- iOS 10推送通知开发
- Windows 10周年更新正式版