Hadoop之自写 WordCount.class

2024-04-30 21:32

本文主要是介绍Hadoop之自写 WordCount.class,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

不是井里没有水,而是你挖的不够深。不是成功来得慢,而是你努力的不够多。

昨天我们用Hadoop自带的WordCount.class统计了一个文件每个单词出现了几次。
今天我们自己写MapReduce

第一步:写Map类
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;import java.io.IOException;/*** map程序逐行读取数据,将每一行数据的起始偏移量作为key,类型是LongWritable,* 将每一行数据的内容作为value(Text),传给我们写的map,* 经过map阶段的处理,输出的key是Text,value是IntWritable。** 调用机制::* 1,谁在调用:map在调用* 2,怎么调:是在当前mapreduce过程中,每读取一行数据,调用一次map*/
public class CountMap extends Mapper<LongWritable, Text, Text, IntWritable> {@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {//key:读进来时,数据的偏移量,一般没什么用//value:具体读进来的数据   helloString word = value.toString();String[] split = word.split(" ");for (String w:split) {//(hello ,1)(as ,1)(hello,1)context.write(new Text(w),new IntWritable(1));}}
}
第二步:写Reduce类
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;/*** 在map中每次只用相同的才会过来* (hello,1) (hello,1) (hello,1) (hello,1)........* 他会自己改写成(hello,(1,1,1,1,1,1....))** reduce的调用机制* 1,谁在调用:reduce阶段在调用* 2,怎么调用:reduce每收到一组key相同的数据,调用一次*/
public class CountReduce extends Reducer<Text, IntWritable,Text,IntWritable> {
//重写Reduce方法@Overrideprotected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {Integer count = 0;for (IntWritable v:values){count++;}context.write(key, new IntWritable(count));}
}
第三步:实现类
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;import java.io.IOException;public class CountDriver {public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {Configuration conf = new Configuration();conf.set("yarn.resorcemanager.hostname","192.168.72.110");conf.set("fs.deafutFS","hdfs://192.168.72.110:9000/");Job job = Job.getInstance(conf);job.setJarByClass(CountDriver.class);//设置本次job是使用map,reducejob.setMapperClass(CountMap.class);job.setReducerClass(CountReadius.class);//设置本次map和reduce的输出类job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);job.setOutputKeyClass(Text.class);job.setOutputKeyClass(IntWritable.class);//制定本次job读取源数据时需要用到的组件:我们的源文件在hdfs的文本文件中,用TextInputFormatjob.setInputFormatClass(TextInputFormat.class);//制定本次job输出数据需要的组件:我们要输出到hdfs文件中,用TextOutputFormatjob.setOutputFormatClass(TextOutputFormat.class);//设置输入路径FileInputFormat.setInputPaths(job,new Path(args[0]));FileOutputFormat.setOutputPath(job,new Path(args[1]));//提交任务,客户端返回job.submit();//核心代码:提交jar程序给yarn,客户端不退出,等待接收mapreduce的进度信息,//打印进度信息,并等待最终运行的结果//客户端true的含义:等着//result:返回true则跑完了,false,出错了boolean resule = job.waitForCompletion(true);System.exit(resule ? 0 : 1);}
}

写完主类,我们要打jar包。本人是idea
这是打包的网址。

https://blog.csdn.net/njxiaoxiao79/article/details/85679992

打包完后,将打包完的jar包上传到虚拟机。

[root@Tyler01 home]# rz

打开hadoop集群

[root@Tyler01 home]# start-all.sh

执行hadoop命令

[root@Tyler01 home]# hadoop jar a22.jar /wc/word.txt /wc/output

查看结果

hadoop fs -cat /wc/out/part-r-00000
as	3
daniu	2
hello	10
kopmgkomg	1
pp	2
tyler	1
tylerhjghjghjghjgjh	1

这篇关于Hadoop之自写 WordCount.class的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/949970

相关文章

VUE动态绑定class类的三种常用方式及适用场景详解

《VUE动态绑定class类的三种常用方式及适用场景详解》文章介绍了在实际开发中动态绑定class的三种常见情况及其解决方案,包括根据不同的返回值渲染不同的class样式、给模块添加基础样式以及根据设... 目录前言1.动态选择class样式(对象添加:情景一)2.动态添加一个class样式(字符串添加:情

提示:Decompiled.class file,bytecode version如何解决

《提示:Decompiled.classfile,bytecodeversion如何解决》在处理Decompiled.classfile和bytecodeversion问题时,通过修改Maven配... 目录问题原因总结问题1、提示:Decompiled .class file,China编程 bytecode

Hadoop企业开发案例调优场景

需求 (1)需求:从1G数据中,统计每个单词出现次数。服务器3台,每台配置4G内存,4核CPU,4线程。 (2)需求分析: 1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster 平均每个节点运行10个 / 3台 ≈ 3个任务(4    3    3) HDFS参数调优 (1)修改:hadoop-env.sh export HDFS_NAMENOD

Hadoop集群数据均衡之磁盘间数据均衡

生产环境,由于硬盘空间不足,往往需要增加一块硬盘。刚加载的硬盘没有数据时,可以执行磁盘数据均衡命令。(Hadoop3.x新特性) plan后面带的节点的名字必须是已经存在的,并且是需要均衡的节点。 如果节点不存在,会报如下错误: 如果节点只有一个硬盘的话,不会创建均衡计划: (1)生成均衡计划 hdfs diskbalancer -plan hadoop102 (2)执行均衡计划 hd

hadoop开启回收站配置

开启回收站功能,可以将删除的文件在不超时的情况下,恢复原数据,起到防止误删除、备份等作用。 开启回收站功能参数说明 (1)默认值fs.trash.interval = 0,0表示禁用回收站;其他值表示设置文件的存活时间。 (2)默认值fs.trash.checkpoint.interval = 0,检查回收站的间隔时间。如果该值为0,则该值设置和fs.trash.interval的参数值相等。

Hadoop数据压缩使用介绍

一、压缩原则 (1)运算密集型的Job,少用压缩 (2)IO密集型的Job,多用压缩 二、压缩算法比较 三、压缩位置选择 四、压缩参数配置 1)为了支持多种压缩/解压缩算法,Hadoop引入了编码/解码器 2)要在Hadoop中启用压缩,可以配置如下参数

类型信息:反射-Class

在说反射前提一个概念:RTTI(在运行时,识别一个对象的类型) public class Shapes {public static void main(String[] args) {List<Shape> shapes = Arrays.asList(new Circle(), new Square(), new Triangle());for (Shape shape : shapes

react笔记 8-17 属性绑定 class绑定 引入图片 循环遍历

1、绑定属性 constructor(){super()this.state={name:"张三",title:'我是一个title'}}render() {return (<div><div>aaaaaaa{this.state.name}<div title={this.state.title}>我是一个title</div></div></div>)} 绑定属性直接使用花括号{}   注

泛型参Class、Class、Class的对比区别

1.原文链接 泛型参Class、Class、Class的对比区别 https://blog.csdn.net/jitianxia68/article/details/73610606 <? extends T>和<? super T> https://www.cnblogs.com/drizzlewithwind/p/6100164.html   2.具体内容: 泛型参数Class、

【Hadoop|MapReduce篇】MapReduce概述

1. MapReduce定义 MapReduce是一个分布式运算程序的编程框架,是用户开发“基于Hadoop的数据分析应用”的核心框架。 MapReduce核心功能是将用户编写的业务逻辑代码和自带默认组件整合成一个完整的分布式运算程序,并发运行在一个Hadoop集群上。 2. MapReduce优缺点 2.1 优点 MapReduce易于编程 它简单的实现一些接口,就可以完成一个分布式