用户统计文件中的单词出现的个数

注意各个文件的导包,job的封装步骤

WordCountMapper.java

package top.wintp.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; /**
* @description: description:
* <p>
* @author: upuptop
* <p>
* @qq: 337081267
* <p>
* @CSDN: http://blog.csdn.net/pyfysf
* <p>
* @cnblogs: http://www.cnblogs.com/upuptop
* <p>
* @blog: http://wintp.top
* <p>
* @email: pyfysf@163.com
* <p>
* @time: 2019/05/2019/5/21
* <p>
*/
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
private Text K = new Text();
private IntWritable V = new IntWritable(1); @Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString();
String[] words = line.split(" "); for (String word : words) {
K.set(word);
context.write(K, V);
} }
}

WordCountReduce

package top.wintp.mapreduce.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; /**
* @description: description:
* <p>
* @author: upuptop
* <p>
* @qq: 337081267
* <p>
* @CSDN: http://blog.csdn.net/pyfysf
* <p>
* @cnblogs: http://www.cnblogs.com/upuptop
* <p>
* @blog: http://wintp.top
* <p>
* @email: pyfysf@163.com
* <p>
* @time: 2019/05/2019/5/21
* <p>
*/
public class WordCountReduce extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable V = 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();
} V.set(sum);
context.write(key, V);
}
}

WordCountRunner

package top.wintp.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.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; /**
* @description: description:
* <p>
* @author: upuptop
* <p>
* @qq: 337081267
* <p>
* @CSDN: http://blog.csdn.net/pyfysf
* <p>
* @cnblogs: http://www.cnblogs.com/upuptop
* <p>
* @blog: http://wintp.top
* <p>
* @email: pyfysf@163.com
* <p>
* @time: 2019/05/2019/5/21
* <p>
*/
public class WordCountRunner implements Tool {
private Configuration conf; public int run(String[] strings) throws Exception {
//封装job
Job job = Job.getInstance(this.conf);
job.setJarByClass(WordCountRunner.class); job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReduce.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); FileInputFormat.setInputPaths(job, new Path("E:/input/wordcount/"));
FileOutputFormat.setOutputPath(job, new Path("E:/output/wordcount/" + System.currentTimeMillis())); //提交任务
int result = job.waitForCompletion(true) ? 0 : 1; return result;
} public void setConf(Configuration configuration) {
this.conf = configuration;
} public Configuration getConf() {
return this.conf;
} public static void main(String[] args) throws Exception {
int status = ToolRunner.run(new WordCountRunner(), args);
System.exit(status);
}
}

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

MapReduce之WordCount的更多相关文章

  1. Java编程MapReduce实现WordCount

    Java编程MapReduce实现WordCount 1.编写Mapper package net.toocruel.yarn.mapreduce.wordcount; import org.apac ...

  2. eclipse运行mapreduce的wordcount

    1,eclipse安装hadoop插件 插件下载地址:链接: https://pan.baidu.com/s/1U4_6kLFNiKeLsGfO7ahXew 提取码: as9e 下载hadoop-ec ...

  3. MapReduce实现WordCount

    package algorithm; import java.io.IOException; import java.util.StringTokenizer; import org.apache.h ...

  4. Hadoop实战5:MapReduce编程-WordCount统计单词个数-eclipse-java-windows环境

    Hadoop研发在java环境的拓展 一 背景 由于一直使用hadoop streaming形式编写mapreduce程序,所以目前的hadoop程序局限于python语言.下面为了拓展java语言研 ...

  5. Hadoop实战3:MapReduce编程-WordCount统计单词个数-eclipse-java-ubuntu环境

    之前习惯用hadoop streaming环境编写python程序,下面总结编辑java的eclipse环境配置总结,及一个WordCount例子运行. 一 下载eclipse安装包及hadoop插件 ...

  6. Hadoop 6、第一个mapreduce程序 WordCount

    1.程序代码 Map: import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.h ...

  7. Hadoop Mapreduce中wordcount 过程解析

    将文件split 文件1:                                                                   分割结果: hello  world   ...

  8. 三.hadoop mapreduce之WordCount例子

    目录: 目录见文章1 这个案列完成对单词的计数,重写map,与reduce方法,完成对mapreduce的理解. Mapreduce初析 Mapreduce是一个计算框架,既然是做计算的框架,那么表现 ...

  9. 大数据技术 - 通俗理解MapReduce之WordCount(三)

    上一章我们编写了简单的 MapReduce 程序,掌握这些就能编写大多数数据处理的代码.但是 MapReduce 框架提供给用户的能力并不止如此,本章我们仍然以上一章 word count 为例,继续 ...

  10. 大数据技术 - 通俗理解MapReduce之WordCount(二)

    上一章我们搭建了分布式的 Hadoop 集群.本章我们介绍 Hadoop 框架中的一个核心模块 - MapReduce.MapReduce 是并行计算模块,顾名思义,它包含两个主要的阶段,map 阶段 ...

随机推荐

  1. 一篇文章搞定JS类型转换

    啥要说这个东西?一道面试题就给我去说它的动机.题如下: var bool = new Boolean(false); if (bool) { alert('true'); } else { alert ...

  2. 使用VS2010再装VS2013不用再烦恼不兼容

    某些同事有时在开发过程中出现这么个问题,在使用js直接异步调用类库时,弹出错误类库不存在或者没有定义等,类似问题,这个时候可能你正在绞尽脑汁的去解决问题,明明问题不大,为什么安装VS2013后就不能打 ...

  3. java泛型方法返回泛型结果

    public class Test { static HashMap<String, String> sMap = new HashMap<String, String>(); ...

  4. Spring Boot的学习之路(03):基础环境搭建,做好学习前的准备工作

    1. 前言 <论语·魏灵公>:"工欲善其事,必先利其器.居是邦也,事其大夫之贤者,友其士之仁者." 工欲善其事必先利其器.我们在熟悉一个陌生项目的时候,首先会大概去看一 ...

  5. Spring Boot2(一):使用Spring Boot2集成Mybatis基础搭建

    Mybatis 初期使用比较麻烦,需要各种配置文件.实体类.Dao 层映射关联.还有一大推其它配置.mybatis-spring-boot-starter 就是 Spring Boot+ Mybati ...

  6. Hive 学习之路(七)—— Hive 常用DML操作

    一.加载文件数据到表 1.1 语法 LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (p ...

  7. bower工具

    1.安装bower npm install bower -g 2.安装软件 borwer install jquery 3.安装指定版本 borwer install jquery#1.7 4.卸载软 ...

  8. 解决IE8placeholder属性问题

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. Android 上传开源项目到 jcenter 实战踩坑之路

    本文微信公众号「AndroidTraveler」首发. 背景 其实 Android 上传开源项目到 jcenter 并不是一件新鲜事,网上也有很多文章. 包括我本人在将开源项目上传到 jcenter ...

  10. Postman接口测试_基本功能

    一.   安装与更新 1.安装的方式 方式1:chrome插件版本:chrome--->设置--->扩展程序: 方式2:native版本(具有更好的扩展性,推荐使用):https://ww ...