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

注意各个文件的导包,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. 值得推荐的C/C++框架和库(深度好文)

    [本文系外部转贴,原文地址:http://www.cppblog.com/merlinfang/archive/2014/12/26/209311.html http://coolshell.info ...

  2. 编译 Qt 5.6(使QtWebEngine支持XP)

    说明 qt 5.6的编译进行了数十遍,才得出本文的可行方案,之所以花了这么多的时间,主要是qt引入了QtWebEngine模块后,导致编译难度直线上升,而且又有一些中国特色的问题(如360安全卫士)导 ...

  3. Controls 属性与继承 TShape 类的小练习(使用TShape可以解决很多图形问题)

    本例效果图: 代码文件: unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, ...

  4. python之mock模块基本使用

    mock简介 mock原来是python的第三方库 python3以后mock模块已经整合到了unittest测试框架中,不用再单独安装 Mock这个词在英语中有模拟的这个意思,因此我们可以猜测出这个 ...

  5. Nodejs操作MySQL - 增删改查

    先安装npm模块项目 npm init 安装mysql npm install mysql --save Nodejs 连接msyql // 导入mysql const mysql = require ...

  6. Linux基础及系统优化

    1 如何实现自动挂载操作(光驱自动挂载--fstab) 1.1 方法 第一种方法:编辑fstab文件 vi /etc/fstab /dev/cdrom /mnt iso9660 default 0 0 ...

  7. VUE单页面的应用优缺点

    1.优 分离前后端关注点,前端负责界面显示,后端负责数据存储和计算. 减轻服务器压力,服务器只用出数据就可以: 同一套后端程序代码,不用修改就可以用于多种设备客户端: 2019-06-19用户体验好. ...

  8. ASP.NET Core[源码分析篇] - 认证

    追本溯源,从使用开始 首先看一下我们的通常是如何使用微软自带的认证,一般在Startup里面配置我们所需的依赖认证服务,这里通过JWT的认证方式讲解 public void ConfigureServ ...

  9. JavaScript学习笔记(2)

    常用对象 Boolean Number String Array 数组 Date 日期 Math 数字 RegExp 正则 Global 全局 函数 var m = function(){} 事件 o ...

  10. node.js + mssql 简易封装操作

    时间吧,总是这么凑巧,在我学习[node.js]还没几天,我的 Microsoft SQL Server Management Studio 18 就歇菜了,至于怎么歇菜的吧....它可能的意思就是想 ...