Mapreduce WordCount实例
2016-07-26 23:18
447 查看
用的版本是hadoop.1.2.1
1.在Eclipes上新建一个工程,新建一个类WordCount
下面是具体的代码
2.在工程下新建lib包,然后拷贝一下jar包到lib中,
右键构建到路径
3.导出为jar包
上传到linux节点上
4.运行
1.在Eclipes上新建一个工程,新建一个类WordCount
下面是具体的代码
package com.heima.hadoop; 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.Job; 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 { public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public 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); } } } public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context ) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); if (otherArgs.length != 2) { System.err.println("Usage: wordcount <in> <out>"); System.exit(2); } Job job = new Job(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }这个Mapreduce 实现了分割字符串,统计每个单词的数量的功能,Map类和Reduce用的是内部类
2.在工程下新建lib包,然后拷贝一下jar包到lib中,
右键构建到路径
3.导出为jar包
上传到linux节点上
4.运行
export HADOOP_CLASSPATH=mapreduce-example.jar hadoop org.heima.hadoop.WordCount input output
相关文章推荐
- 78.You are managing an Oracle Database 11g database. The database is open, and you plan to perform R
- c#基础入门(2)——语法结构、方法、关键字
- 进程虚拟内存
- 自写JQ控件-树状菜单控件[demo下载]
- 自写JQ控件-树状菜单控件[demo下载]
- 自写JQ控件-树状菜单控件[demo下载]
- 自写JQ控件-树状菜单控件[demo下载]
- selenium-webdriver
- 解决EXC_BAD_ACCESS错误的一种方法--NSZombieEnabled
- 学习技术,最忌讳的就是眼高手低!
- OpenJudge 百练 2016 ACM 暑期课练习题 滑雪
- 逆向技能+2 JNI介绍
- 转载--小甲鱼PE详解之区块描述、对齐值以及RVA详解(PE详解06)
- Javacard的内部认证和外部认证
- Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null obj
- uc高级编程之创建和删除目录
- 学习技术,最忌讳的就是眼高手低!
- 崔希凡-javaWeb-笔记day07-day09(2016年7月26日23:14:40)
- SmartAssembly .net混淆后,无法找到部分类型
- SQLSERVER:sqlserver2008r2安装好后,自动提示功能不可以使用