mapreduce实例WordCount

需求:统计一个文件中每一个单词出现的总次数。
案例数据:
一段文章
使用 idea 创建 maven 工程,添加依赖如下:

pom.xml

<?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.kgc</groupId>
<artifactId>hadoop_day1208</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>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.6.0-cdh5.14.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>2.6.0-cdh5.14.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-hdfs</artifactId>
        <version>2.6.0-cdh5.14.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-mapreduce-client-core</artifactId>
        <version>2.6.0-cdh5.14.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-mapreduce-client-jobclient</artifactId>
        <version>2.6.0-cdh5.14.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-auth</artifactId>
        <version>2.6.0-cdh5.14.2</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>

</build>

WCMApper

public class WCMapper extends Mapper<LongWritable, Text,Text, IntWritable> {
    private  Text word=new Text();
    private IntWritable count =new IntWritable(1);
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] words = value.toString().trim()
                .replaceAll(",|\\.|!|\\?|:|\"", "").split(" ");
        for (String word : words) {
            this.word.set(word);
            context.write(this.word,count);
        }
    }
}

WCReader

public class WCReducer extends Reducer <Text, IntWritable,Text,IntWritable>{
    private IntWritable count=new IntWritable();


    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int sum=0;
        for (IntWritable value : values) {
           sum+=value.get();
        }
        count.set(sum);
        context.write(key,count);
    }
}

WCPartitioner

public class WCPartitioner extends Partitioner<Text, IntWritable> {
    @Override
    public int getPartition(Text text, IntWritable intWritable, int numberReduceTasks) {
        return text.toString().length()%numberReduceTasks;
    }
}

WCDriver

public class WCDriver {
    public static void main(String[] args) throws Exception {
        //创建hdfs访问路径配置信息
        final String INPUT=args[0],OUTPUT=args[1];
        Configuration  config=new Configuration();
        config.set("fs.defaultFS","hdfs://192.168.184.200:9000");
        //如果输出路径已存在则删除
        FileSystem fs=FileSystem.get(config);
        fs.deleteOnExit(new Path(OUTPUT));
        fs.close();
        //创建mapreduce计算任务
        Job job=Job.getInstance(config,"wordCount");
        //设置主类,定位外部jar资源
        job.setJarByClass(WCDriver.class);
        //设置任务数:和文件分片数和所存储的DN数有关
        job.setNumReduceTasks(2);
        //设置分区器Partitioner
            job.setPartitionerClass(WCPartitioner.class);
        //设置Combiner
        job.setCombinerClass(WCReducer.class);
        //设置Maper
        job.setMapperClass(WCMapper.class);
        //设置Reducer
        job.setReducerClass(WCReducer.class);
        //设置Mapper的输出键值类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        //设置Reducer的输出键值类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);


        FileInputFormat.setInputPaths(job,new Path(INPUT));
        FileOutputFormat.setOutputPath(job,new Path(OUTPUT));
        //等待所有job步骤完成
        System.out.println(job.waitForCompletion(true));
    }
}

打成jar包在虚拟机执行

在这里插入图片描述

找到jar包,传输到虚拟机中

在这里插入图片描述

在虚拟机中执行jar包

在这里插入图片描述

cn.kgc.WordCount.WCDriver为要运行的类名

结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值