hadoop-mapreduce-(1)-统计单词数量
编写map程序
package com.cvicse.ump.hadoop.mapreduce.map; import java.io.IOException; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; public class WordCountMap extends Mapper<LongWritable, Text, Text, IntWritable> { @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){
context.write(new Text(word), new IntWritable(1));
} } }
编写reduce程序
package com.cvicse.ump.hadoop.mapreduce.reduce; import java.io.IOException; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class WordCountReduce extends
Reducer<Text, IntWritable, Text, IntWritable> { @Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException { Integer count = 0;
for(IntWritable value:values){
count+=value.get();
} context.write(key, new IntWritable(count)); } }
编写main函数
package com.cvicse.ump.hadoop.mapreduce; 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 com.cvicse.ump.hadoop.mapreduce.map.WordCountMap;
import com.cvicse.ump.hadoop.mapreduce.reduce.WordCountReduce; public class WordCount { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf,"wordCount");
job.setJarByClass(WordCount.class);
job.setMapperClass(WordCountMap.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(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); boolean bb = job.waitForCompletion(true);
if(!bb){
System.out.println("wrodcount task fail!");
}else{
System.out.println("wordcount task success!");
} } }
把wordcount.txt放在hdfs的/dyh/data/input/目录下
执行:hadoop jar hdfs.jar com.cvicse.ump.hadoop.mapreduce.WordCount /dyh/data/input/wordcount.txt /dyh/data/output/1
hadoop-mapreduce-(1)-统计单词数量的更多相关文章
- hadoop MapReduce —— 输出每个单词所对应的文件
下面是四个文件及其内容. 代码实现: Mapper: package cn.tedu.invert; import java.io.IOException; import org.apache.had ...
- go语言小练习——给定英语文章统计单词数量
给定一篇英语文章,要求统计出所有单词的个数,并按一定次序输出.思路是利用go语言的map类型,以每个单词作为关键字存储数量信息,代码实现如下: package main import ( " ...
- Hadoop MapReduce 操作 统计词频
1.准备文件并设置编码格式为UTF-8并上传Linux 2.新建一个Java Project 3.导入jar 4.编写Map()和Reduce() 5.将代码输出成jar 6.在linux中启动hdf ...
- Storm监控文件夹变化 统计文件单词数量
监控指定文件夹,读取文件(新文件动态读取)里的内容,统计单词的数量. FileSpout.java,监控文件夹,读取新文件内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- 023_数量类型练习——Hadoop MapReduce手机流量统计
1) 分析业务需求:用户使用手机上网,存在流量的消耗.流量包括两部分:其一是上行流量(发送消息流量),其二是下行流量(接收消息的流量).每种流量在网络传输过程中,有两种形式说明:包的大小,流量的大小. ...
- Hadoop实战3:MapReduce编程-WordCount统计单词个数-eclipse-java-ubuntu环境
之前习惯用hadoop streaming环境编写python程序,下面总结编辑java的eclipse环境配置总结,及一个WordCount例子运行. 一 下载eclipse安装包及hadoop插件 ...
- 分析MapReduce执行过程+统计单词数例子
MapReduce 运行的时候,会通过 Mapper 运行的任务读取 HDFS 中的数据文件,然后调用自己的方法,处理数据,最后输出.Reducer 任务会接收 Mapper 任务输出的数据,作为自己 ...
- Hadoop Mapreduce 案例 wordcount+统计手机流量使用情况
mapreduce设计思想 概念:它是一个分布式并行计算的应用框架它提供相应简单的api模型,我们只需按照这些模型规则编写程序,即可实现"分布式并行计算"的功能. 案例一:word ...
- 【Cloud Computing】Hadoop环境安装、基本命令及MapReduce字数统计程序
[Cloud Computing]Hadoop环境安装.基本命令及MapReduce字数统计程序 1.虚拟机准备 1.1 模板机器配置 1.1.1 主机配置 IP地址:在学校校园网Wifi下连接下 V ...
随机推荐
- WampServer 安装使用详解
WampServer集成环境的搭建.安装.使用.配置 什么是WampServer WampServer是一款由法国人开发的Apache Web服务器.PHP解释器以及MySQL数据库的整合软件包.免去 ...
- domain or business logic
Here are a few of the questions you should ask when writing business logic: ¡Do you fully understand ...
- 【Ansible 文档】【译文】Playbooks 变量
Variables 变量 自动化的存在使得重复的做事情变得很容易,但是我们的系统不可能完全一样. 在某些系统中,你可能想要设置一些与其他系统不一样的行为和配置. 同样地,远程系统的行为和状态也可以影响 ...
- 基于jquery的从一个页面跳转到另一个页面的指定位置的实现代码
比如 想跳到 mao.aspx 的页面 的div id="s" 的位置 那么 只用<a href="mao.aspx#s"> 就可实现跳转到指定位置 ...
- ST_Geometry效率的测试与分析
测试环境 数据库:Oracle11g R1(11.1.0.6) 64Bit 中间件:ArcSDE10 (64Bit) 数据情况:点数据(point,231772条记录),面数据(poly,12条记录) ...
- swift 该死的派发机制--待完成
swift 该死的派发机制 final static oc类型 多态类型 静态类型 动态函数 静态函数 nsobject: 1.缺省不再使用oc的动态派发机制: 2.可以使用nsobject暴露出来 ...
- Idea设置快捷键以及修改Eclipse的debug快捷键
Idea强大不多说了,用久了都可以习惯,但是感觉Idea的debug真是不如eclipse好用,Idea的快捷键都是组合键,用着繁琐.两种方法可以设置eclipse的快捷键: 1:直接全局都使用ecl ...
- 椭圆曲线签名算法的v的定义
在之前的个个与签名相关的地方我都对v的定义感到十分困惑,知道查看了黄皮书以后才对它的作用有了一定地了解,如下: (v is the recovery id: a 1 byte value specif ...
- SpringMVC——消息转换器HttpMessageConverter(转)
文章转自http://blog.csdn.net/cq1982/article/details/44101293 概述 在SpringMVC中,可以使用@RequestBody和@ResponseBo ...
- B-Tree外存数据结构 _(外存储器—磁盘)第一部分
1.外存储器—磁盘 计算机存储设备一般分为两种:内存储器(main memory)和外存储器(external memory).内存存取速度快,但容量小,价格昂贵,而且不能长期保存数据(在不通电情况下 ...