MapReduce计算每个单词出现的次数

文章目录

1. 准备工作

  • pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.itcast</groupId>
    <artifactId>mapreduce</artifactId>
    <version>1.0-SNAPSHOT</version>
    <repositories>
        <repository>
            <id>cloudera</id>
            <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.apache.Hadoop</groupId>
            <artifactId>Hadoop-client</artifactId>
            <version>2.6.0-mr1-cdh5.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.Hadoop</groupId>
            <artifactId>Hadoop-common</artifactId>
            <version>2.6.0-cdh5.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.Hadoop</groupId>
            <artifactId>Hadoop-hdfs</artifactId>
            <version>2.6.0-cdh5.14.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.Hadoop</groupId>
            <artifactId>Hadoop-mapreduce-client-core</artifactId>
            <version>2.6.0-cdh5.14.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <minimizeJar>true</minimizeJar>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
</project>

2. WordCount计算

计算每个单词出现的次数

2.1 原始数据

  zhangsan,lisi,wangwu
  zhaoliu,maqi
  zhangsan,zhaoliu,wangwu
  lisi,wangwu

2.2 期望的结果

  zhangsan 2
  lisi 2
  wangwu 3
  zhaoliu 2
  maqi 1

2.3 偏移量

每个字符移动到当前文档的最前面需要移动的字符个数。

2.4 WordCount-Map实现

注意:导包别导错了

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;

public class WordCountMapper  extends Mapper<LongWritable, Text, Text, LongWritable> { 
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { 
        //获取传入的每一行内容
        String line = value.toString();
        //按照数据分隔符切割
        String[] words = line.split(" ");
        //遍历单词数组,出现单词就标记为1
        for (String word : words) { 
            context.write(new Text(word), new LongWritable(1));
        }
    }
}

 1、实例一个class 继承Mapper<输入的key的数据类型,输入的value的数据类型,输出的key的数据类型,输出的value的数据类型> 
 
 2、重写map方法 map(LongWritable key, Text value, Context context)
    key: 行首字母的偏移量
    value: 一行数据
    context:上下文对象
    
 3、根据业务需求进行切分,然后逐一输出

2.5 WordCount-Reduce实现

注意:导包别导错了

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;

public class WordCountReducer extends Reducer<Text,LongWritable,Text,LongWritable> { 
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException { 
        //声明一个变量
        int sum = 0;
        for (LongWritable value: values) { 
            sum += value.get();
        }
        context.write(key, new LongWritable(sum));
    }
}

 1、实例一个class 继承Reducer<输入的key的数据类型,输入的value的数据类型,输出的key的数据类型,输出的value的数据类型> 
 
 2、重写reduce方法 reduce(Text key, Iterable values, Context context)
    key: 去重后单词
    values: 标记的1(好多个1,key出现几次就有几个1)
    context:上下文对象
    
 3、遍历values 进行汇总计算

2.6 WordCount-Driver实现

注意:导包别导错了

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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 WordCountRunner { 
    public static void main(String[] args) throws Exception { 
        // 创建本次mr程序的job实例
        Configuration conf = new Configuration();
        // conf.set("mapreduce.framework.name", "local");
        Job job = Job.getInstance(conf);
        // 指定本次job运行的主类
        job.setJarByClass(WordCountRunner.class);
        // 指定本次job的具体mapper reducer实现类
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);
        // 指定本次job map阶段的输出数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        // 指定本次job reduce阶段的输出数据类型 也就是整个mr任务的最终输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        // 指定本次job待处理数据的目录 和程序执行完输出结果存放的目录
        long startTime=System.currentTimeMillis();   //获取开始时间
        FileInputFormat.setInputPaths(job,"E:\\wordcount\\input\\words.txt");
        FileOutputFormat.setOutputPath(job, new Path("E:\\wordcount\\output"));
        // 提交本次job
        boolean b = job.waitForCompletion(true);
        long endTime=System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
        System.exit(b ? 0 : 1);
    }
}

2.7 最终结果

lisi	2
maqi	1
wangwu	3
zhangsan	2
zhaoliu	2
    原文作者:潘书鹏的BigData
    原文地址: https://blog.csdn.net/weixin_45749011/article/details/103042007
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞