MapReduce编写wordcount程序代码实现
发表于:2025-02-24 作者:千家信息网编辑
千家信息网最后更新 2025年02月24日,MapReduce经典案例代码(wordcount)以经典的wordcount为例,通过自定义的mapper和reducer来实现单词计数package com.fwmagic.mapreduce;i
千家信息网最后更新 2025年02月24日MapReduce编写wordcount程序代码实现
MapReduce经典案例代码(wordcount)
以经典的wordcount为例,通过自定义的mapper和reducer来实现单词计数
package com.fwmagic.mapreduce;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;import java.io.IOException;/** * MapReduce单词统计 */public class WordCountDemo { /** * 自定义Mapper继承:org.apache.hadoop.mapreduce.Mapper,实现map方法 */ public static class WordCountMapper extends Mapper { @Override protected void map(LongWritable key, Text value, Mapper.Context context) throws IOException, InterruptedException { String[] words = value.toString().split(" "); for (String word : words) { context.write(new Text(word), new IntWritable(1)); } } } /** * 自定义Reducer继承:org.apache.hadoop.mapreduce.Reducer,实现reduce方法 */ public static class WordCountReducer extends Reducer { @Override protected void reduce(Text key, Iterable values, Reducer.Context context) throws IOException, InterruptedException { int count = 0; for (IntWritable writable : values) { count += writable.get(); } context.write(key, new IntWritable(count)); } } /** * job启动类,设置参数并集群中提交job * @param args * @throws Exception */ public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf); job.setJarByClass(WordCountDemo.class); job.setMapperClass(WordCountMapper.class); job.setReducerClass(WordCountReducer.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.setInputPaths(job, new Path("/wordcount/input")); FileOutputFormat.setOutputPath(job, new Path("/wordcount/output")); boolean b = job.waitForCompletion(true); System.exit(b ? 0 : 1); }}
集群中/wordcount/input目录下数据内容
打包项目,执行job
hadoop jar fwmagic-wordcount.jar
执行输出结果
单词
方法
经典
集群
代码
内容
参数
数据
案例
目录
经典案例
结果
项目
统计
输出
程序
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络技术是什么课程
手机软件开发时间
共筑网络安全守护美好家园ppt
网络安全进校园班会体会
红四方网络安全宣传周
上海网络安全准入控制系统供应商
新基建服务器特质
中南大学张涛网络技术怎么样
服务器遇到问题是什么
嵌入式应用软件开发
山东华云网络技术有限公司
怀技网络技术有限公司靠谱吗
数据库修改字段长度过程分析
数据库表格数据量太大怎么处理
长沙骏业软件开发
安卓数据库操作频率
小学生网络安全培训课
甘德罗服务器的功能作用
服务器最多能装多大的内存
广州游戏软件开发培训学校
实惠的应用软件开发方案
工业企业网络安全应急预案
苏州大数据软件开发预算
计算机围棋软件开发
云服务器安装多系统
软件开发与实施哪个好
在传统模式中孤岛网络技术
虚拟机FTP服务器配置
数据库表的记录有效性规则
小软件开发公司的优势