Hadoop AWS Word Count 样例
在AWS里用Elastic Map Reduce 开一个Cluster
然后登陆master node并编译下面程序:
- 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.LongWritable;
- 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;
- public class WordCount {
- public static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
- private final IntWritable one = new IntWritable(1);
- private Text word = new Text();
- @Override
- protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
- String line = value.toString();
- StringTokenizer tokenizer = new StringTokenizer(line);
- while(tokenizer.hasMoreTokens()) {
- word.set(tokenizer.nextToken());
- context.write(word, one);
- }
- }
- }
- public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
- @Override
- protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
- int sum = 0;
- for(IntWritable value : values) {
- sum += value.get();
- }
- context.write(key, new IntWritable(sum));
- }
- }
- public static void main(String[] args) throws Exception {
- Configuration conf = new Configuration();
- Job job = new Job(conf, "Word Count hadoop-0.20");
- //setting the class names
- job.setJarByClass(WordCount.class);
- job.setMapperClass(WordCountMapper.class);
- job.setReducerClass(WordCountReducer.class);
- //setting the output data type classes
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(IntWritable.class);
- //to accept the hdfs input and outpur dir at run time
- FileInputFormat.addInputPath(job, new Path(args[0]));
- FileOutputFormat.setOutputPath(job, new Path(args[1]));
- System.exit(job.waitForCompletion(true) ?
- 0 : 1);
- }
- }
设置:
export CLASSPATH=$CLASSPATH:/home/hadoop/*:/home/hadoop/lib/*:'.'
javac WordCount.java
jar cvf WordCount.jar *.class
hadoop jar WordCount.jar WordCount s3://15-319-s13/book-dataset/pg_00 /output
执行成功后,由于output目录在Hadoop FS下,所以能够这样查看:
hadoop fs -cat /output/part-r-00000 | less
主要參考:
http://kickstarthadoop.blogspot.com/2011/04/word-count-hadoop-map-reduce-example.html
http://kickstarthadoop.blogspot.com/2011/05/word-count-example-with-hadoop-020.html
Hadoop AWS Word Count 样例的更多相关文章
- Mac下hadoop运行word count的坑
Mac下hadoop运行word count的坑 Word count体现了Map Reduce的经典思想,是分布式计算中中的hello world.然而博主很幸运地遇到了Mac下特有的问题Mkdir ...
- Hadoop简单源码样例
1.WordCount策略比较简单 import java.io.IOException; import java.util.StringTokenizer; import org.apache.ha ...
- eclipse 配置执行hadoop 2.7 程序样例參考步骤
前提:你搭建好了hadoop 2.x的linux环境,并可以成功执行.还有就是window可以訪问到集群.over 1. hfds-site.xml 添加属性:关闭集群的权限校验.windows的用户 ...
- hadoop学习;block数据块;mapreduce实现样例;UnsupportedClassVersionError异常;关联项目源代码
对于开源的东东,尤其是刚出来不久,我认为最好的学习方式就是能够看源代码和doc,測试它的样例 为了方便查看源代码,关联导入源代码的项目 先前的项目导入源代码是关联了源代码文件 block数据块,在配置 ...
- Hadoop Word Count程序
Hadoop Word Count程序 pom.xml文件: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns ...
- [hadoop系列]Pig的安装和简单演示样例
inkfish原创,请勿商业性质转载,转载请注明来源(http://blog.csdn.net/inkfish ).(来源:http://blog.csdn.net/inkfish) Pig是Yaho ...
- hadoop得知;block数据块;mapreduce实现样例;UnsupportedClassVersionError变态;该项目的源代码相关联
对于开源的东西.特别是刚出来不久.我认为最好的学习方法是能够看到源代码,doc,样品测试 为了方便查看源代码,导入与项目相关的源代码 watermark/2/text/aHR0cDovL2Jsb2cu ...
- 用python + hadoop streaming 编写分布式程序(一) -- 原理介绍,样例程序与本地调试
相关随笔: Hadoop-1.0.4集群搭建笔记 用python + hadoop streaming 编写分布式程序(二) -- 在集群上运行与监控 用python + hadoop streami ...
- Java --本地提交MapReduce作业至集群☞实现 Word Count
还是那句话,看别人写的的总是觉得心累,代码一贴,一打包,扔到Hadoop上跑一遍就完事了????写个测试样例程序(MapReduce中的Hello World)还要这么麻烦!!!?,还本地打Jar包, ...
随机推荐
- SQL传入时间获取到时间的周一和周日
declare @time datetime declare @timeMonday datetime set @time='2013-11-07' ) ,@time) select @timeMon ...
- Knockout应用开发指南(完整版) 目录索引(转)
使用Knockout有一段时间了(确切的说从MIX11大会宣传该JavaScript类库以来,我们就在使用,目前已经在正式的asp.net MVC项目中使用),Knockout使用js代码达到双向绑定 ...
- Python,计算 ax^2 + bx + c = 0的根
1 #-*-coding : utf-8-*- 2 import math 3 4 def quadratic(a, b, c): 5 if not isinstance(a, (int, float ...
- [ Nowcoder Contest 167 #C ] 部分和
\(\\\) \(Description\) 给出一个长度为\(N\)的数组\(A[i]\),保证\(N\)为 \(2\) 的整次幂. 对于每个 \(i\ (i\in [0,N))\)求所有满足\(( ...
- oracle添加联合主键
1 alter table tablename add constraint unionkeyname primary key (column1,column2); 上面语句中: tablename为 ...
- redhat之数据挖掘R语言软件及rstudio-server服务的安装
安装时间:2015年8月25日 22:55:35 作者:luomg 软件:R.Rstudio-server 环境:redhat6.2 联系:luomgf@163.com 声明:如果你有遇到安装中的问题 ...
- HDU_1789_doing homework again_贪心
Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- python3.7实现九九乘法表
for i in range(1,10): for j in range(1,i+1): print("%d*%d=%d" % (i,j,i*j),end=" " ...
- zip相关知识梳理(一)
zip相关知识梳理(一) 经过对zip文件的长时间研究,对zip文件进行相关知识进行梳理,虽然网上很多牛人对其做了相关基础解析,但是对于特殊情况没有进行说明,比如超过4G的zip文件该以什么格式进行编 ...
- UVA - 10410 Tree Reconstruction(栈处理递归)
题目: 给出一棵树的BFS和DFS序列,输出这棵树中每个结点的子结点列表.BFS和DFS序列在生成的时候,当一个结点被扩展时,其所有子结点应该按照编号从小 到大的顺序访问. 思路: 一开始是想根据BF ...