package com.yw.hadoop273;

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; /**
* @Auther: YW
* @Date: 2019/9/18 20:58
* @Description:
*/
public class WCWordCount extends Mapper<LongWritable, Text, Text, IntWritable> {
/*
* Mapper
  * 把单词分割出来
* */
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
Text keyOut = new Text();
IntWritable valueOut = new IntWritable();
String[] arr = value.toString().split("");
for (String s : arr) {
keyOut.set(s);
valueOut.set();
context.write(keyOut,valueOut);
}
}
}

package com.yw.hadoop273;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; /**
* @Auther: YW
* @Date: 2019/9/18 21:20
* @Description:
*/
public class WCReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
/***
* 聚合
*/ @Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int count=0;
for (IntWritable value : values) {
count = count + value.get();
}
context.write(key,new IntWritable(count));
} }
package com.yw.hadoop273;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import java.io.IOException; /**
* @Auther: YW
* @Date: 2019/9/16 21:20
* @Description:
*/
public class WCApp {
public static void main(String[] args) throws IOException {
Configuration conf = new Configuration();
// 删除已有的目录
if (args.length>1){
FileSystem.get(conf).delete(new Path(args[1]));
} Job job = Job.getInstance(conf);
// 设置job属性
job.setJobName("WCApp"); // 设置作业名称
job.setJarByClass(WCApp.class); // 设置搜索类
job.setInputFormatClass(TextInputFormat.class);// 设置输入格式 FileInputFormat.addInputPath(job,new Path(args[0])); // 输入路径
FileOutputFormat.setOutputPath(job,new Path(args[1]));// 输出路径
job.setMapperClass(WCWordCount.class); // 设置mapper 类
job.setReducerClass(WCReducer.class); // 设置reducer类
job.setNumReduceTasks(1); // reducer个数
job.setMapOutputKeyClass(Text.class);
job.setMapOutputKeyClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputKeyClass(IntWritable.class);
} }

Hadoop 求单词count数的更多相关文章

  1. 分页过滤SQL求总条数SQL正则

    public static void main(String[] args) throws Exception { String queryForScanUsers_SQL = "selec ...

  2. Mac下hadoop运行word count的坑

    Mac下hadoop运行word count的坑 Word count体现了Map Reduce的经典思想,是分布式计算中中的hello world.然而博主很幸运地遇到了Mac下特有的问题Mkdir ...

  3. c语言求回文数的三种算法的描述

    c语言求回文数的三种算法的描述 题目描述 注意:(这些回文数都没有前导0) 1位的回文数有0,1,2,3,4,5,6,7,8,9 共10个: 2位的回文数有11,22,33,44,55,66,77,8 ...

  4. 评playerc网友的"求比指定数大且最小的“不重复数”问题"

    问题见:对Alexia(minmin)网友代码的评论及对“求比指定数大且最小的‘不重复数’问题”代码的改进 .算法:求比指定数大且最小的“不重复数”问题的高效实现 . playerc网友的代码如下(求 ...

  5. 对Alexia(minmin)网友代码的评论及对“求比指定数大且最小的‘不重复数’问题”代码的改进

    应Alexia(minmin)网友之邀,到她的博客上看了一下她的关于“求比指定数大且最小的‘不重复数’问题”的代码(百度2014研发类校园招聘笔试题解答),并在评论中粗略地发表了点意见. 由于感觉有些 ...

  6. sum_series() 求一列数的指定个数的数和(5个数字的和)

    #include <stdio.h> #include <stdarg.h> /*用sum_series() 求一列数的指定个数的数和(5个数字的和)*/ double sum ...

  7. H面试程序(29):求最大递增数

    要求:求最大递增数 如:1231123451 输出12345 #include<stdio.h> #include<assert.h> void find(char *s) { ...

  8. codeforces 429 On the Bench dp+排列组合 限制相邻元素,求合法序列数。

    限制相邻元素,求合法序列数. /** 题目:On the Bench 链接:http://codeforces.com/problemset/problem/840/C 题意:求相邻的元素相乘不为平方 ...

  9. P1474 货币系统 Money Systems(完全背包求填充方案数)

    题目链接:https://www.luogu.org/problemnew/show/1474 题目大意:有V种货币,求用V种货币凑出面值N有多少种方案. 解题思路:就是完全背包问题,只是将求最大价值 ...

随机推荐

  1. embeding 是什么

    要搞清楚embeding先要弄明白他和one hot encoding的区别,以及他解决了什么one hot encoding不能解决的问题,带着这两个问题去思考,在看一个简单的计算例子 以下引用 Y ...

  2. Workstation 14 Returns EFI Network Start PXE over IPv4 When Installing New Win10 Guest O/S

    当启动出现“Click any key to boot from DVD”时候,需要键盘点击一下,即可解决. 参考:https://communities.vmware.com/thread/5730 ...

  3. UK Biobank专题

    这个时代的生信,统计遗传,不懂或不会用这个数据库就说不过去了. 看看10年GWAS里是如何定位和评价UK biobank的: For the near future, the UK Biobank i ...

  4. elasticsearch 的入门

    参考文档 1.全文搜索引擎 Elasticsearch 入门教程(http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html) 2.elasti ...

  5. nice -n 10 bash 和 chrt 10 bash 和 echo -17 > /proc/PID/oom_score_adj

    进程优先级起作用的方式从发明以来基本没有什么变化,无论是只有一个cpu的时代,还是多核cpu时代,都是通过控制进程占用cpu时间的长短来实现的. 就是说在同一个调度周期中,优先级高的进程占用的时间长些 ...

  6. [转]在CentOS安装CMake (CentOS7 64位适用)

    一.环境描述 1.系统:CentOS 6.4 i386 (min) 2.登录用户:root 3.版本:CMake 2.8.10.2 4.虚拟机:Oracle VM VirtualBox 二.安装步骤 ...

  7. 123457123456#0#-----com.cym.shuXue02--前拼后广--开心学数学

    com.cym.shuXue02--前拼后广--开心学数学

  8. pca数学原理(转)

    PCA的数学原理 前言 数据的向量表示及降维问题 向量的表示及基变换 内积与投影 基 基变换的矩阵表示 协方差矩阵及优化目标 方差 协方差 协方差矩阵 协方差矩阵对角化 算法及实例 PCA算法 实例 ...

  9. MySQL修改表名示例

    首先,我们新建一个名为test_table的表: drop table if exists test_table; create table test_table select TABLE_SCHEM ...

  10. HTML布局排版2 div的和图片平铺方便管理

    在HTML里,由于浏览器显示器等差异,浏览器的宽度也会有变化,为了适应不同的宽度,需要用到平铺.例如页面前面的固定的条等,如果是纯色,可以用背景色,如果不是纯色,是渐变等,可以用条状图平铺.常见的布局 ...