liunx Hadoop 集群运行Hadoo.jar代码中主类名为WordCountMain的代码格式

时间: 2024-05-14 15:14:35 浏览: 5
WordCountMain的代码格式与Java编程规范无异,以下是一个示例: ```java 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; public class WordCountMain { 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(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCountMain.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(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 在这个示例代码中,我们定义了两个类:TokenizerMapper和IntSumReducer。TokenizerMapper是一个Mapper类,用于将输入文件中的数据拆分成单词,并将单词作为key,将数字1作为value输出。IntSumReducer是一个Reducer类,用于将相同的单词合并,并将它们的数量相加,输出最终结果。 在main函数中,我们设置了MapReduce作业的各种参数,包括输入路径、输出路径、Mapper和Reducer类等。最后,我们使用System.exit来退出程序,并将MapReduce作业的状态作为参数传递给它。如果作业成功完成,返回0;否则,返回1。

相关推荐

最新推荐

recommend-type

red head liunx安装 Oracle SOA集群中文文档

1.1 检查软件环境需求的程序包(在所有节点上执行) 2 1.2 创建Oracle帐户(在所有节点上执行) 3 1.3 安装 SOA Suite10.1.3.1.0软件 4
recommend-type

linux中批量修改文件名的脚本代码

linux中批量修改文件名的脚本代码,主要是使用了rename,结合shell,喜欢的朋友可以参考下
recommend-type

mobaxterm无法连接到liunx的解决办法.doc

适用于之前安装过linux系列的系统,重装linux系统,导致无法使用mobaxterm连接的情况
recommend-type

liunx 安装oracle SOA集群 配置中文手册

1.1 安装独立 OC4J 实例 3 1.2安装一个集成的 Web 服务器和 OC4J 中间层 3 1.3安装 SOA 管理实例 4 1.4使用 SOA 套件安装 J2EE 服务器 5 1.5 安装 J2EE 服务器、 Web 服务器和 SOA 套件 5 1.6 J2EE 服务器和 SOA ...
recommend-type

torch-1.7.1+cu110-cp37-cp37m-linux_x86_64.whl离线安装包linux系统x86_64

torch-1.7.1+cu110-cp37-cp37m-linux_x86_64.whl torchvision-0.8.2+cu110-cp37-cp37m-linux_x86_64.whl 由于超过1G无法上传,给的是百度云链接!!!!!需自行下载
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。