MapReduce编程快速入门

MapReduce编程快速入门MapReduce编程规范用户编写的程序分成三个部分:Mapper,Reducer,Driver(提交运行mr程序的客户端)Mapper阶段继承Mapper类(1)用户自定义的Mapper要继承自己的父类(2)Mapper的输入数据是KV对的形式(KV的类型可自定义)(3)Mapper中的业务逻辑写在map()方法中(4)Mapper的输出数据是KV对的形式(KV的类型可自定义)(5)map()方法(maptask进程)对每一个<K,V>调用一次Reducer阶段继承Reduce

大家好,又见面了,我是你们的朋友全栈君。

MapReduce编程规范

用户编写的程序分成三个部分:Mapper,Reducer,Driver(提交运行mr程序的客户端)

Mapper阶段继承Mapper类

(1)用户自定义的Mapper要继承自己的父类
(2)Mapper的输入数据是KV对的形式(KV的类型可自定义)
(3)Mapper中的业务逻辑写在map()方法中
(4)Mapper的输出数据是KV对的形式(KV的类型可自定义)
(5)map()方法(maptask进程)对每一个<K,V>调用一次

Reducer阶段继承Reducer类

(1)用户自定义的Reducer要继承自己的父类
(2)Reducer的输入数据类型对应Mapper的输出数据类型,也是KV
(3)Reducer的业务逻辑写在reduce()方法中
(4)Reducetask进程对每一组相同k的<k,v>组调用一次reduce()方法

Driver阶段使用Driver模板

整个程序需要一个Drvier来进行提交,提交的是一个描述了各种必要信息的job对象

案例实操

1.需求分析

在给定的文本文件中统计输出每一个单词出现的总次数
(1)输入数据 hello.txt

dev1 dev1
ss ss
cls cls
jiao
banzhang
xue
hadoop

(2)期望输出数据

dev1     2
banzhang    1
cls    2
hadoop    1
jiao    1
ss    2
xue    1

2.开发步骤

按照MapReduce编程规范,分别编写Mapper,Reducer,Driver

》》1输入数据

hadoop hdfs 
mr mr

》》2输出数据

hadoop 1
hdfs 1
mr 2

》》3 Mapper
3.1 将MapTask传给我们的文本内容先转换成String
3.2 根据空格将这一行切分成单词
3.3 将单词输出为<单词,1>
》》4 Reducer
4.1 汇总各个key的个数
4.2 输出该key的总次数
》》5 Driver
5.1 获取配置信息,获取job对象实例
5.2 指定本程序的jar所在的路径
5.3 关联Mapper/Reducer的业务类
5.4 指定Mapper输出数据的kv类型
5.5 指定最终输出的数据的kv类型
5.6 指定job的输入原始文本所在目录
5.7 指定job的输出结果所在目录
5.8 提交作业

3 项目搭建

(1)Idea 创建maven工程
在这里插入图片描述

(2)在pom.xml文件中添加如下依赖

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.7.2</version>
        </dependency>
</dependencies>

(2)在项目的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

4.编写程序

(1)编写Mapper类

package com.dev1.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);
    
    @Override
    protected 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) { 
   
            Text k = new Text();
            k.set(word);
            context.write(k, v);
        }
    }
}

(2)编写Reducer类

package com.dev1.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();

    @Override
    protected 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.dev1.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 获取配置信息以及封装任务
Configuration configuration = new Configuration();
Job job = Job.getInstance(configuration);
// 2 设置jar加载路径
job.setJarByClass(WordcountDriver.class);
// 3 设置map和reduce类
job.setMapperClass(WordcountMapper.class);
job.setReducerClass(WordcountReducer.class);
// 4 设置map输出
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 提交
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1);
}
}

5.本地测试
在这里插入图片描述

(1 )如果电脑系统是win7的就将win7的hadoop jar包解压
如果电脑系统是win10的就将win10的hadoop jar包解压

注意: 1 win8电脑和win10家庭版操作系统可能有问题,需要重新编译源码或者更改操作系统。 2 非中文,无空格路径

(2)在Eclipse/Idea上运行程序
运行前必须设置参数
在这里插入图片描述
在图中给定两个路径
在这里插入图片描述

6.集群上测试
(0)用maven打jar包,需要添加的打包插件依赖
注意:标记红颜色的部分需要替换为自己工程主类

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</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>
<archive>
<manifest>
<mainClass>com.dev1.wordcout.WordcountDriver</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

(1)将程序打成jar包。
在这里插入图片描述

修改不带依赖的jar包名称为wc.jar,并拷贝该jar包到Hadoop集群。
(2)启动Hadoop集群
在hadoop102上

start dfs.sh

在hadoop103上

start-yarn.sh

(3)上传文本文件到 input文件夹

hdfs dfs -mkdir -p /user/dev1/input
cd /opt/module/hadoop-2.7.2
hdfs dfs -put   ./words.txt  /user/dev1/input

input文件夹下只能有文本文件
(4)执行WordCount程序

hadoop jar  wc.jar com.dev1.wordcount.WordcountDriver /user/dev1/input /user/dev1/output
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/149234.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • 变异系数法之matlab

    变异系数法之matlab1.简介2.算法原理2.1指标正向化2.2数据标准化2.3计算变异系数2.4计算权重以及得分3.实例分析3.1读取数据3.2指标正向化3.3数据标准化3.4计算变异系数3.5计算权重3.6计算得分完整代码

  • MQTT学习笔记(6)搭建本地MQTT服务器

    MQTT学习笔记(6)搭建本地MQTT服务器搭建EMQTT下载地址下载emqttd-windows10-v2.3.11.zip解压后cd到bin目录,执行emqttdconsole执行成功会弹出下面窗口,不成功就关掉cmd重新试下打开浏览器输入http://127.0.0.1:18083默认用户名admin默认密码public进入如下界面框起来的点进去看看就知道了。注意如果你用w…

  • origin画图记录[通俗易懂]

    origin画图记录[通俗易懂]origin画图记录折线图折线图origin存放数据的Book其实和excel的sheet很相似,画图的操作也有一定的相似性,只是origin比excel的功能更加强大。首先打开安装好的origin软件,其界面如下图所示:画线状图时,直接选中需要画图的数据,然后选择plot—Line—Line,即可画出对应的折线图,但是此时画出的折线图巨丑,重点时后续对它的美化。对绘制图形的美化以及一些常用功能:这个手掌的图形主要是用于移动、缩放图形中白色画板,效果如下:对坐标轴(刻度、

  • Qt Quick之TableView的使用

    本博只是简单的展示TableView的基本使用(TableView、style:TableViewStyle、headerDelegate、rowDelegate、itemDelegate、Table

    2021年12月29日
  • python定义函数求和_Python定义函数实现累计求和操作

    python定义函数求和_Python定义函数实现累计求和操作一、使用三种方法实现0-n累加求和定义函数分别使用while循环、for循环、递归函数实现对0-n的累加求和1、使用while循环定义一个累加求和函数sum1(n),函数代码如下:2、使用for循环定义一个累加求和函数sum2(n),函数代码如下:3、使用递归函数定义一个累加求和函数sum3(n),函数代码如下:二、使用了三种实现累加求和的方法,分别定义了三个函数。1、对0-100实现累加求和,…

    2022年10月29日
  • VirtualBox安装Debian6的方法和步骤(详细)

    VirtualBox安装Debian6的方法和步骤(详细)下面是用VirtualBox安装Debian6的方法和步骤l新建一个文件夹,用于存放虚拟硬盘,如Debianl打开VirtualBox,点击新建l输入虚拟机名称,Debian_6l给虚拟机分配

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号