目录

  • WordCount案例
  • 需求
  • 环境准备
  • 本地测试
  • 提交到集群测试
  • 集群测试
  • 源码程序
  • 1.WordCountMapper类
  • 2.WordCountReducer类
  • 3.WordCountDriver类


WordCount案例

需求

: 统计一堆文件中单词出现的个数。

1.输入数据
hello hello
hi hi
haha
map
reduce

2.期望输出数据
hello 2
hi 2
haha 1
map 1
reduce 1

需求分析:按照MapReduce编程规范,分别编写Mapper、Reducer、Driver。

3.Mapper
1). 将MapTask传给我们的文本内容转换成String:
hello hello

2). 根据空格将这一行切分成单词:
hello
hello

3). 将单词输出为<单词,1>
hello, 1
hello, 1

4.Reducer
1). 汇总各个key的个数
hello, 1
hello, 1
2). 输出该key的总次数
hello, 2

5.Driver
1). 获取配置信息,获取job对象实例;
2). 制定本程序的jar包所在的本地路径;
3). 关联Mapper/Reducer业务类;
4). 指定Mapper输出数据的KV类型;
5). 指定最终输出的数据的KV类型;
6). 指定job的输入原始文件所在目录;
7). 指定job的输出结果所在目录;
8).提交作业。

环境准备

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

<dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.2.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.30</version>
        </dependency>

    </dependencies>

3.在项目的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

4.创建包名:com.xiaobai.mapreduce.wordcount;
分别编写Mapper、Reducer、Driver类。

本地测试

源码Driver部分:

//6.设置输入路径和输出路径
FileInputFormat.setInputPaths(job,new Path("/Users/jane/Desktop/test/"));
FileOutputFormat.setOutputPath(job,new Path("/Users/jane/Desktop/hadoop/output"));

在“/Users/jane/Desktop/test/”目录下新建一份hello.xml,内容如下:

hadoop统计单词数 hadoop统计某个单词次数_hadoop

输出结果:

hadoop统计单词数 hadoop统计某个单词次数_hadoop_02

提交到集群测试

集群测试

1.用maven打jar包,在pom.xml文件中添加如下依赖:

<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.打包maven jar包。

hadoop统计单词数 hadoop统计某个单词次数_big data_03


(ps.我太难了,这张图是拼接的,截了好几次图,一直缺东缺西的,不太完美 = = )

3.使用命令启动集群:

[xiaobai@hadoop102 ~]$ myhadoop.sh start

4.使用命令查看进程,确保集群已经正常启动:

[xiaobai@hadoop102 ~]$ jpsall

hadoop统计单词数 hadoop统计某个单词次数_big data_04

5.将jar包复制一份到桌面并命名为wc.jar,上传打包好的jar包到/opt/module/hadoop3.2.2:

hadoop统计单词数 hadoop统计某个单词次数_hadoop_05

6.右击WordCountDriver–>copy/paste Special–>copy reference拷贝全类名。

com.xiaobai.mapreduce.wordcount.WordCountDriver

hadoop统计单词数 hadoop统计某个单词次数_hadoop_06


7.在/opt/module/hadoop3.2.2目录下创建WordSum.txt并输入以下内容:

[xiaobai@hadoop102 hadoop-3.2.2]$ vim WordSum.txt

hadoop统计单词数 hadoop统计某个单词次数_apache_07

8.如图,切换hdfs用户并创建一个input文件夹:

[xiaobai@hadoop102 hadoop-3.2.2]$ hdfs dfs -mkdir /input

hadoop统计单词数 hadoop统计某个单词次数_apache_08

9.如图,将本地文件WordSum.txt上传到HDFS:

[xiaobai@hadoop102 hadoop-3.2.2]$ hdfs dfs -put /opt/module/hadoop-3.2.2/WordSum.txt /input

hadoop统计单词数 hadoop统计某个单词次数_big data_09

10.如图,运行wc.jar:

[xiaobai@hadoop102 hadoop-3.2.2]$ hadoop jar wc.jar com.xiaobai.mapreduce.wordcount.WordCountDriver /input /output

hadoop统计单词数 hadoop统计某个单词次数_hadoop_10


tips: 空格计数1是因为我多打了一行,并没有写入内容。

源码程序

1.WordCountMapper类

package com.xiaobai.mapreduce.wordcount;

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 java.io.IOException;

/*
KEYIN, map阶段输入的key的类型:LongWritable
VALUEIN, map阶段输入value类型:Text
KEYOUT, map阶段输出的key类型:Text
VALUEOUT,map阶段输出的value类型:IntWritable
 */
public class WordCountMapper extends Mapper<LongWritable, Text,Text, IntWritable> {
    private Text outK = new Text();
    private IntWritable outV = new IntWritable(1);

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        //1.获取一行
        //hello hello
        String line = value.toString();

        //2.切割
        //hello
        //hello
        String[] words = line.split(" ");

        //3.循环写出
        for (String s : words) {

            //封装outK
            outK.set(s);

            //写出
            context.write(outK,outV);
        }
    }
}

2.WordCountReducer类

package com.xiaobai.mapreduce.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/*
KEYIN, reduce阶段输入的key的类型:Text
VALUEIN, reduce阶段输入value类型:IntWritable
KEYOUT, reduce阶段输出的key类型:Text
VALUEOUT,reduce阶段输出的value类型:IntWritable
 */
public class WordCountReducer extends Reducer<Text, IntWritable,Text,IntWritable> {
    private IntWritable outV = new IntWritable();
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

        int sum = 0;
        //hello,(1,1)
        //累加
        for (IntWritable value: values) {
            sum += value.get();

        }

        outV.set(sum);

        //写出
        context.write(key,outV);
        
    }
}

3.WordCountDriver类

package com.xiaobai.mapreduce.wordcount;

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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/*
KEYIN, map阶段输入的key的类型:LongWritable
VALUEIN, map阶段输入value类型:Text
KEYOUT, map阶段输出的key类型:Text
VALUEOUT,map阶段输出的value类型:IntWritable
 */
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.设置jar包路径
        job.setJarByClass(WordCountDriver.class);

        //3.关联mapper和reducer
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);

        //4.设置map输出的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.提交job
        boolean result = job.waitForCompletion(true);

        System.exit(result?0:1);
    }

}