尚硅谷大数据hadoop教程_mapReduce

news/2024/10/25 10:28:36/

p67 课程介绍

在这里插入图片描述

p68概述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

p69 mapreduce核心思想

在这里插入图片描述

p70 wordcount源码 序列化类型

mapReduce三类进程
在这里插入图片描述

在这里插入图片描述

p71 编程规范

用户编写的程序分成三个部分:Mapper、Reducer和Driver。
在这里插入图片描述
在这里插入图片描述

P72 wordcount需求案例分析

在这里插入图片描述

p 73 -78 案例环境准备

(1)创建maven工程,MapReduceDemo
(2)在pom.xml文件中添加如下依赖

<dependencies><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>3.1.3</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.30</version></dependency>
</dependencies>

(2)在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在文件中填入。

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

(3)创建包名:com.atguigu.mapreduce.wordcount
4)编写程序
(1)编写Mapper类

package com.atguigu.mapreduce.wordcount;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{Text k = new Text();IntWritable v = new IntWritable(1);@Overrideprotected void map(LongWritable key, Text value, Context context)	throws IOException, InterruptedException {// 1 获取一行String line = value.toString();// 2 切割String[] words = line.split(" ");// 3 输出for (String word : words) {k.set(word);context.write(k, v);}}
}

(2)编写Reducer类

package com.atguigu.mapreduce.wordcount;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{int sum;
IntWritable v = new IntWritable();@Overrideprotected void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {// 1 累加求和sum = 0;for (IntWritable count : values) {sum += count.get();}// 2 输出v.set(sum);context.write(key,v);}
}

(3)编写Driver驱动类

package com.atguigu.mapreduce.wordcount;
import java.io.IOException;
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.output.FileOutputFormat;public class WordCountDriver {public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {// 1 获取配置信息以及获取job对象Configuration conf = new Configuration();Job job = Job.getInstance(conf);// 2 关联本Driver程序的jarjob.setJarByClass(WordCountDriver.class);// 3 关联Mapper和Reducer的jarjob.setMapperClass(WordCountMapper.class);job.setReducerClass(WordCountReducer.class);// 4 设置Mapper输出的kv类型job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);// 5 设置最终输出kv类型job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);// 6 设置输入和输出路径FileInputFormat.setInputPaths(job, new Path(args[0]));FileOutputFormat.setOutputPath(job, new Path(args[1]));// 7 提交jobboolean result = job.waitForCompletion(true);System.exit(result ? 0 : 1);}
}

本地测试

(1)需要首先配置好HADOOP_HOME变量以及Windows运行依赖
(2)在IDEA/Eclipse上运行程序

提交到集群测试

集群上测试
(1)用maven打jar包,需要添加的打包插件依赖

<build><plugins><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.6.1</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin><plugin><artifactId>maven-assembly-plugin</artifactId><configuration><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin></plugins>
</build>

(2)将程序打成jar包

(3)修改不带依赖的jar包名称为 wc.jar,并拷贝该jar包到Hadoop集群的 /opt/module/hadoop-3.1.3 路径。
(4)启动Hadoop集群
[atguigu@hadoop102 hadoop-3.1.3]sbin/start-dfs.sh
[atguigu@hadoop103 hadoop-3.1.3]$ sbin/start-yarn.sh
(5)执行WordCount程序
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop jar wc.jar
com.atguigu.mapreduce.wordcount.WordCountDriver /user/atguigu/input /user/atguigu/output


http://www.ppmy.cn/news/117895.html

相关文章

安卓系统的电视机_2020年电视机最强选购指南,含15款高性价比电视机集合大推荐(2020年7月份更新)...

通过本文你可以快速了解到&#xff1a; 一、买电视机需要强烈关注的选购因素 二、网购电视机需要注意的事项 三、10款高性价比电视机推荐和点评 前面两部分会涉及到电视机参数等科普内容&#xff0c;如果需求不大&#xff0c;想直接看电视机推荐部分的&#xff0c;建议直接下滑…

Windows和Linux服务器给图片添加水印两种方法,引用字体库和自带字体库

文章目录 一、效果展示二、前提说明三、Windows自带字体库实现方法四、引入字体库实现方法 一、效果展示 水印123如下图 1.可以自定义&#xff08;类型&#xff1a;汉字、数字都支持&#xff09; 2.可以定位指定位置 二、前提说明 实现添加水印功能中Windows服务器和Linux服…

springBoot-MyBatis-Plus-binlog日志监听BinaryLogClient

前言 &#xff1a;项目中如果想要实时监听Mysql 表中数据的实时状态&#xff08;插入&#xff0c;更新&#xff0c;删除&#xff09;&#xff0c;并根据不同的状态做出相应的动作&#xff0c;应该怎么办&#xff1b; 1 BinaryLogClient 介绍&#xff1a; BinaryLogClient是一…

项目管理:面对未知的挑战时,如何获取和使用信息?

一项实验展示了人们在面对未知的挑战时&#xff0c;对信息的获取和使用的影响。在下面的实验中&#xff0c;三组人被要求步行到十公里外的三个村庄。 第一组人没有任何信息&#xff0c;只跟着向导走。他们在走了短短的两三公里后就开始抱怨和情绪低落&#xff0c;同时感到疲惫…

stg游戏c语言,坦克大战改版

《坦克大战改》是一款国内玩家模仿FC经典游戏坦克大战所自制的小游戏&#xff0c;游戏中玩家将会控制坦克保卫基地&#xff0c;一旦基地失守关卡就会失败。游戏支持手柄震动 &#xff0c;同时包含有关卡编辑器&#xff0c;让玩家能够自由编辑关卡。 游戏信息 游戏名称&#xff…

c语言编程简单小游戏坦克大战,坦克大战1990(c语言文件版)游戏

坦克大战1990(c语言文件版)是一款很炫的坦克战争类游戏。游戏设计感很强。敌人千变万化&#xff0c;但是你可别被迷惑哦&#xff0c;将他们通通歼灭吧&#xff01; 作者的话 经过四五天的奋斗&#xff0c;第一次编的游戏终于完成了&#xff0c;好激动。 首先得感谢一下吧友&…

Python文章合集 | (入门到实战、游戏、Turtle、案例等)

&#x1f339;前言 为了大家能相应的找到自己喜欢哪方面的文章&#xff0c;这边小编进行了文章的汇总合集&#xff0c;也特别感谢我的 粉丝们&#xff01;我会加油滴~文末公众号可见免费资料哈&#xff01; &#x1f44d;正文 每日更新&#xff1a; 项目0.1 30个精心整理的…

Python—2023 |已有文章汇总 | 持续更新,直接看这篇就够了

&#x1f381;导语 现将之前写过的文章进行分类整理&#xff0c;方便大家观看找到自己喜欢的文章类型哦~ 所有文章源码素材都在文末公众hao自取哈&#xff01;&#x1f447;&#x1f447;&#x1f447;&#x1f447;&#x1f447;&#x1f447; &#x1f496;每日更新&#xf…