MapReduce编程:单词去重
编程实现单词去重要用到NullWritable类型。
NullWritable:
NullWritable 是一种特殊的Writable 类型,由于它的序列化是零长度的,所以没有字节被写入流或从流中读出,可以用作占位符。比如,在MapReduce 中,在不需要这个位置的时候,键或值能够被声明为NullWritable,从而有效存储一个不变的空值。
通过调用NullWritable.get() 方法来检索。
单词去重我们最后要输出的形式是<单词>,所以值可以声明为NullWritable。
代码如下:
package org.apache.hadoop.examples;
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class DistinctWord{
public DistinctWord() {
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
//String[] otherArgs = (new GenericOptionsParser(conf, args)).getRemainingArgs();
String[] otherArgs = new String[]{"input","output"}; //设置输入和输出
if(otherArgs.length < 2) {
System.err.println("Usage: wordcount <in> [<in>...] <out>");
System.exit(2);
}
Job job = Job.getInstance(conf, "distinct word");
job.setJarByClass(DistinctWord.class); //设置jar包所在路径
//指定Mapper和Reducer类
job.setMapperClass(DistinctWord.DistinctWordMapper.class);
job.setCombinerClass(DistinctWord.DistinctWordReducer.class);
job.setReducerClass(DistinctWord.DistinctWordReducer.class);
//指定MapTask的输出类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NullWritable.class);
//指定ReduceTask的输出类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
//指定数据输入路径
for(int i = 0; i < otherArgs.length - 1; ++i) {
FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
}
//指定数据输出路径
FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));
//提交任务
System.exit(job.waitForCompletion(true)?0:1);
}
//输出类型定义为NullWritable
public static class DistinctWordMapper extends Mapper<Object, Text, Text, NullWritable> {
private Text word = new Text();
public DistinctWordMapper() {
}
public void map(Object key, Text value, Mapper<Object, Text, Text, NullWritable>.Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString()); //分词器
while(itr.hasMoreTokens()) {
this.word.set(itr.nextToken());
context.write(this.word, NullWritable.get());
}
}
}
public static class DistinctWordReducer extends Reducer<Text, NullWritable, Text, NullWritable> {
public DistinctWordReducer() {
}
//reduce方法每调用一次,就接收到一组相同的单词,所以直接输出一次key即可。
public void reduce(Text key, Iterable<NullWritable> values, Reducer<Text, NullWritable, Text, NullWritable>.Context context) throws IOException, InterruptedException {
context.write(key, NullWritable.get());
}
}
}
MapReduce编程:单词去重的更多相关文章
- Hadoop MapReduce编程学习
一直在搞spark,也没时间弄hadoop,不过Hadoop基本的编程我觉得我还是要会吧,看到一篇不错的文章,不过应该应用于hadoop2.0以前,因为代码中有 conf.set("map ...
- hadoop2.2编程:使用MapReduce编程实例(转)
原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...
- 三、MapReduce编程实例
前文 一.CentOS7 hadoop3.3.1安装(单机分布式.伪分布式.分布式 二.JAVA API实现HDFS MapReduce编程实例 @ 目录 前文 MapReduce编程实例 前言 注意 ...
- [Hadoop入门] - 1 Ubuntu系统 Hadoop介绍 MapReduce编程思想
Ubuntu系统 (我用到版本号是140.4) ubuntu系统是一个以桌面应用为主的Linux操作系统,Ubuntu基于Debian发行版和GNOME桌面环境.Ubuntu的目标在于为一般用户提供一 ...
- mapreduce编程模型你知道多少?
上次新霸哥给大家介绍了一些hadoop的相关知识,发现大家对hadoop有了一定的了解,但是还有很多的朋友对mapreduce很模糊,下面新霸哥将带你共同学习mapreduce编程模型. mapred ...
- 《Data-Intensive Text Processing with mapReduce》读书笔记之二:mapreduce编程、框架及运行
搜狐视频的屌丝男士第二季大结局了,惊现波多野老师,怀揣着无比鸡冻的心情啊,可惜随着剧情的推进发展,并没有出现期待中的屌丝奇遇,大鹏还是没敢冲破尺度的界线.想百度些种子吧,又不想让电脑留下污点证据,要知 ...
- MapReduce 编程模型
一.简单介绍 1.MapReduce 应用广泛的原因之中的一个在于它的易用性.它提供了一个因高度抽象化而变得异常简单的编程模型. 2.从MapReduce 自身的命名特点能够看出,MapReduce ...
- MapReduce编程模型详解(基于Windows平台Eclipse)
本文基于Windows平台Eclipse,以使用MapReduce编程模型统计文本文件中相同单词的个数来详述了整个编程流程及需要注意的地方.不当之处还请留言指出. 前期准备 hadoop集群的搭建 编 ...
- 实训任务04 MapReduce编程入门
实训任务04 MapReduce编程入门 1.实训1:画图mapReduce处理过程 使用有短句“A friend in need is a friend in deed”,画出使用MapReduce ...
随机推荐
- 16-1 ECMA5与ECMA6的函数定义
ECMA5: function Drag(id){ this.ele = document.getElementById(id); var that = this; this.ele.onmoused ...
- java基础学习总结——流
一.JAVA流式输入/输出原理
- iptables共享上网
1.1 流程大概如下: 1.环境准备 内部服务器B 内网172.16.1.12 ifdown eth0 #首先关闭外网网卡 route add default gw 172.16.1.11 #把上图中 ...
- 找不到main
用eclipse写代码的时候,写了一个简单的程序,编译的时候突然出现“错误: 在类 com.test.demo 中找不到 main 方法, 请将 main 方法定义为: public static v ...
- vue构造器的内容
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- servlet 执行顺序
public class TestServelt { public static void main(String[] args) { ChildServlet childServlet = new ...
- Linux Input子系统
先贴代码: //input.c int input_register_handler(struct input_handler *handler) { //此处省略很多代码 list_for_each ...
- JAVA可检测异常和非检测异常
Java的可检测异常和非检测异常泾渭分明.可检测异常经编译器验证,对于声明抛出异常的任何方法,编译器将强制执行处理或声明规则. 非检测异常不遵循处理或声明规则.在产生此类异常时,不一定非要采取任何适当 ...
- 遇到NotificationService: Suppressing notification from package com.example.dell.servicebestpractice by u错误
一般来说是手机设置没有允许通知
- 钉钉调试应用Inspect不显示或显示空白的解决方法
首先必须使用钉钉开发版,并确保已经通过此链接打开了调试功能: https://open-doc.dingtalk.com/docs/doc.htm?spm=5176.10694750.0.0.3tPH ...