当前位置: 首页 > news >正文

MapReduce编程:词频统计

首先在项目的src文件中需要加入以下文件,log4j的内容为:

log4j.rootLogger=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

 

代码如下:

 1 package org.apache.hadoop.examples;
 2      
 3     import java.io.IOException;
 4     import java.util.Iterator;
 5     import java.util.StringTokenizer;
 6     import org.apache.hadoop.conf.Configuration;
 7     import org.apache.hadoop.fs.Path;
 8     import org.apache.hadoop.io.IntWritable;
 9     import org.apache.hadoop.io.Text;
10     import org.apache.hadoop.mapreduce.Job;
11     import org.apache.hadoop.mapreduce.Mapper;
12     import org.apache.hadoop.mapreduce.Reducer;
13     import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
14     import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
15     import org.apache.hadoop.util.GenericOptionsParser;
16      
17     public class WordCount {
18         public WordCount() {
19         }
20          
21         //main函数,MapReduce程序运行的入口
22         public static void main(String[] args) throws Exception {
23             Configuration conf = new Configuration();   //指定HDFS相关的参数
24             
25             //String[] otherArgs = (new GenericOptionsParser(conf, args)).getRemainingArgs();
26             String[] otherArgs = new String[]{"input","output"};
27             if(otherArgs.length < 2) {
28                 System.err.println("Usage: wordcount <in> [<in>...] <out>");
29                 System.exit(2);
30             }
31          
32             //通过Job类设置Hadoop程序运行时的环境变量
33             Job job = Job.getInstance(conf, "word count");  //设置环境参数 
34             job.setJarByClass(WordCount.class);  //设置整个程序的类名
35             job.setMapperClass(WordCount.TokenizerMapper.class); //添加Mapper类
36             job.setCombinerClass(WordCount.IntSumReducer.class); 
37             job.setReducerClass(WordCount.IntSumReducer.class); //添加Reducer类
38             job.setOutputKeyClass(Text.class);  //设置输出类型,因为输出的形式是<单词,个数>,所以这里用Text,类似于Java的String,但还是有些区别
39             job.setOutputValueClass(IntWritable.class);  //设置输出类型,类似于Java的Int
40      
41             for(int i = 0; i < otherArgs.length - 1; ++i) {  
42                 FileInputFormat.addInputPath(job, new Path(otherArgs[i]));    //设置输入文件
43             }
44      
45             FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));   //设置输出文件
46             System.exit(job.waitForCompletion(true)?0:1);  //提交作业
47         }
48      
49         //Reduce处理逻辑
50         public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
51             private IntWritable result = new IntWritable();
52      
53             public IntSumReducer() {
54             }
55      
56             public void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
57                 int sum = 0;
58      
59                 IntWritable val;
60                 for(Iterator i$ = values.iterator(); i$.hasNext(); sum += val.get()) {
61                     val = (IntWritable)i$.next();
62                 }
63      
64                 this.result.set(sum);
65                 context.write(key, this.result);
66             }
67         }
68      
69         
70         //Map处理逻辑
71         public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
72             private static final IntWritable one = new IntWritable(1);
73             private Text word = new Text();
74      
75             public TokenizerMapper() {
76             }
77      
78             public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
79                 StringTokenizer itr = new StringTokenizer(value.toString());   //分词器
80      
81                 while(itr.hasMoreTokens()) {
82                     this.word.set(itr.nextToken());
83                     context.write(this.word, one);  //输出键值对
84                     //这里也可以直接写成context.write(new Text(word), new IntWritable(1));
85                 }
86      
87             }
88         }
89     }    

 

转载于:https://www.cnblogs.com/zyb993963526/p/10244721.html

相关文章:

  • Python基础之文件
  • 使用vuepress搭建文档管理/博客
  • 什么是分布式系统,如何学习分布式系统
  • Dynamics CRM - 如何解决 Microsoft Dynamics CRM has encountered an error 弹窗的问题
  • python队列Queue
  • [转]让Linux进入虚拟机 Virtualenv
  • C++学习二十C++中函数重载的理解
  • vuex存储和本地存储(localstorage、sessionstorage)的区别
  • Ubuntu下postgresql安装及常见错误处理
  • [BZOJ5125]小Q的书架(决策单调性+分治DP+树状数组)
  • IP 别名和辅助 IP 地址
  • python 使用多线程进行并发编程/互斥锁的使用
  • 树莓派Ubuntu 16.04 MATA系统 修改用户文件夹名后,提示configure it with blueman-service...
  • 基于websocket的单聊.群聊
  • Python(76)_装饰器进阶_带参数的装饰器
  • [js高手之路]搞清楚面向对象,必须要理解对象在创建过程中的内存表示
  • Brief introduction of how to 'Call, Apply and Bind'
  • ES6核心特性
  • export和import的用法总结
  • Netty 框架总结「ChannelHandler 及 EventLoop」
  • Vue ES6 Jade Scss Webpack Gulp
  • 从0搭建SpringBoot的HelloWorld -- Java版本
  • 翻译--Thinking in React
  • 给github项目添加CI badge
  • 深入 Nginx 之配置篇
  • 深入浅出webpack学习(1)--核心概念
  • 使用 @font-face
  • 首页查询功能的一次实现过程
  • 双管齐下,VMware的容器新战略
  • 一天一个设计模式之JS实现——适配器模式
  • 在Mac OS X上安装 Ruby运行环境
  • 正则表达式
  • 正则表达式小结
  • Mac 上flink的安装与启动
  • ​渐进式Web应用PWA的未来
  • (3)选择元素——(14)接触DOM元素(Accessing DOM elements)
  • (4)事件处理——(6)给.ready()回调函数传递一个参数(Passing an argument to the .ready() callback)...
  • (二)PySpark3:SparkSQL编程
  • (一)80c52学习之旅-起始篇
  • (转)Scala的“=”符号简介
  • (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一
  • (转)甲方乙方——赵民谈找工作
  • .gitignore文件设置了忽略但不生效
  • .NET Entity FrameWork 总结 ,在项目中用处个人感觉不大。适合初级用用,不涉及到与数据库通信。
  • .NET gRPC 和RESTful简单对比
  • .net 重复调用webservice_Java RMI 远程调用详解,优劣势说明
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地中转一个自定义的弱事件(可让任意 CLR 事件成为弱事件)
  • .net遍历html中全部的中文,ASP.NET中遍历页面的所有button控件
  • .NET值类型变量“活”在哪?
  • .net中应用SQL缓存(实例使用)
  • .one4-V-XXXXXXXX勒索病毒数据怎么处理|数据解密恢复
  • .php结尾的域名,【php】php正则截取url中域名后的内容
  • @configuration注解_2w字长文给你讲透了配置类为什么要添加 @Configuration注解
  • @Tag和@Operation标签失效问题。SpringDoc 2.2.0(OpenApi 3)和Spring Boot 3.1.1集成
  • [202209]mysql8.0 双主集群搭建 亲测可用