在AWS里用Elastic Map Reduce 开一个Cluster

然后登陆master node并编译下面程序:

  1. import java.io.IOException;
  2. import java.util.StringTokenizer;
  3.  
  4. import org.apache.hadoop.conf.Configuration;
  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. public class WordCount {
  16.  
  17. public static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
  18.  
  19. private final IntWritable one = new IntWritable(1);
  20. private Text word = new Text();
  21.  
  22. @Override
  23. protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  24. String line = value.toString();
  25. StringTokenizer tokenizer = new StringTokenizer(line);
  26. while(tokenizer.hasMoreTokens()) {
  27. word.set(tokenizer.nextToken());
  28. context.write(word, one);
  29. }
  30. }
  31.  
  32. }
  33.  
  34. public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
  35.  
  36. @Override
  37. protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
  38. int sum = 0;
  39. for(IntWritable value : values) {
  40. sum += value.get();
  41. }
  42. context.write(key, new IntWritable(sum));
  43. }
  44.  
  45. }
  46.  
  47. public static void main(String[] args) throws Exception {
  48. Configuration conf = new Configuration();
  49. Job job = new Job(conf, "Word Count hadoop-0.20");
  50.  
  51. //setting the class names
  52. job.setJarByClass(WordCount.class);
  53. job.setMapperClass(WordCountMapper.class);
  54. job.setReducerClass(WordCountReducer.class);
  55.  
  56. //setting the output data type classes
  57. job.setOutputKeyClass(Text.class);
  58. job.setOutputValueClass(IntWritable.class);
  59.  
  60. //to accept the hdfs input and outpur dir at run time
  61. FileInputFormat.addInputPath(job, new Path(args[0]));
  62. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  63.  
  64. System.exit(job.waitForCompletion(true) ?
  65.  
  66. 0 : 1);
  67. }
  68.  
  69. }

设置:

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 样例的更多相关文章

  1. Mac下hadoop运行word count的坑

    Mac下hadoop运行word count的坑 Word count体现了Map Reduce的经典思想,是分布式计算中中的hello world.然而博主很幸运地遇到了Mac下特有的问题Mkdir ...

  2. Hadoop简单源码样例

    1.WordCount策略比较简单 import java.io.IOException; import java.util.StringTokenizer; import org.apache.ha ...

  3. eclipse 配置执行hadoop 2.7 程序样例參考步骤

    前提:你搭建好了hadoop 2.x的linux环境,并可以成功执行.还有就是window可以訪问到集群.over 1. hfds-site.xml 添加属性:关闭集群的权限校验.windows的用户 ...

  4. hadoop学习;block数据块;mapreduce实现样例;UnsupportedClassVersionError异常;关联项目源代码

    对于开源的东东,尤其是刚出来不久,我认为最好的学习方式就是能够看源代码和doc,測试它的样例 为了方便查看源代码,关联导入源代码的项目 先前的项目导入源代码是关联了源代码文件 block数据块,在配置 ...

  5. Hadoop Word Count程序

    Hadoop Word Count程序 pom.xml文件: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns ...

  6. [hadoop系列]Pig的安装和简单演示样例

    inkfish原创,请勿商业性质转载,转载请注明来源(http://blog.csdn.net/inkfish ).(来源:http://blog.csdn.net/inkfish) Pig是Yaho ...

  7. hadoop得知;block数据块;mapreduce实现样例;UnsupportedClassVersionError变态;该项目的源代码相关联

    对于开源的东西.特别是刚出来不久.我认为最好的学习方法是能够看到源代码,doc,样品测试 为了方便查看源代码,导入与项目相关的源代码 watermark/2/text/aHR0cDovL2Jsb2cu ...

  8. 用python + hadoop streaming 编写分布式程序(一) -- 原理介绍,样例程序与本地调试

    相关随笔: Hadoop-1.0.4集群搭建笔记 用python + hadoop streaming 编写分布式程序(二) -- 在集群上运行与监控 用python + hadoop streami ...

  9. Java --本地提交MapReduce作业至集群☞实现 Word Count

    还是那句话,看别人写的的总是觉得心累,代码一贴,一打包,扔到Hadoop上跑一遍就完事了????写个测试样例程序(MapReduce中的Hello World)还要这么麻烦!!!?,还本地打Jar包, ...

随机推荐

  1. SQL传入时间获取到时间的周一和周日

    declare @time datetime declare @timeMonday datetime set @time='2013-11-07' ) ,@time) select @timeMon ...

  2. Knockout应用开发指南(完整版) 目录索引(转)

    使用Knockout有一段时间了(确切的说从MIX11大会宣传该JavaScript类库以来,我们就在使用,目前已经在正式的asp.net MVC项目中使用),Knockout使用js代码达到双向绑定 ...

  3. 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 ...

  4. [ Nowcoder Contest 167 #C ] 部分和

    \(\\\) \(Description\) 给出一个长度为\(N\)的数组\(A[i]\),保证\(N\)为 \(2\) 的整次幂. 对于每个 \(i\ (i\in [0,N))\)求所有满足\(( ...

  5. oracle添加联合主键

    1 alter table tablename add constraint unionkeyname primary key (column1,column2); 上面语句中: tablename为 ...

  6. redhat之数据挖掘R语言软件及rstudio-server服务的安装

    安装时间:2015年8月25日 22:55:35 作者:luomg 软件:R.Rstudio-server 环境:redhat6.2 联系:luomgf@163.com 声明:如果你有遇到安装中的问题 ...

  7. HDU_1789_doing homework again_贪心

    Doing Homework again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. python3.7实现九九乘法表

    for i in range(1,10): for j in range(1,i+1): print("%d*%d=%d" % (i,j,i*j),end=" " ...

  9. zip相关知识梳理(一)

    zip相关知识梳理(一) 经过对zip文件的长时间研究,对zip文件进行相关知识进行梳理,虽然网上很多牛人对其做了相关基础解析,但是对于特殊情况没有进行说明,比如超过4G的zip文件该以什么格式进行编 ...

  10. UVA - 10410 Tree Reconstruction(栈处理递归)

    题目: 给出一棵树的BFS和DFS序列,输出这棵树中每个结点的子结点列表.BFS和DFS序列在生成的时候,当一个结点被扩展时,其所有子结点应该按照编号从小 到大的顺序访问. 思路: 一开始是想根据BF ...