首页 > 编程语言 >MapReduce程序编写(举例:统计单词个数)

MapReduce程序编写(举例:统计单词个数)

时间:2022-08-28 19:01:02浏览次数:63  
标签:LongWritable map Text MapReduce 单词 job 举例 new class

public class WordCount {
//map读取数据的key类型定死是LongWritable,代表的是行号,从0开始,value是一行数据,Text
static class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException {
//写map处理逻辑
//context代表的是hadoop的上下文,将来可以使用它将数据写出map
//对每一行数据进行分割
//将hadoop的类型转成java的类型
String row = value.toString();
String[] words = row.split(" ");
//遍历数据,得到每一个单词
for (String word : words) {
//将String-->Text
Text key2 = new Text(word);
//对每一个单词进行封装,利用context写出map
context.write(key2, new LongWritable(1L));
}


}
}

static class MyReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
@Override
protected void reduce(Text key, Iterable<LongWritable> values, Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
//迭代values,进行求和
long sum = 0L;

for (LongWritable value : values) {
long l = value.get();
sum = sum + l;
}
context.write(key, new LongWritable(sum));

}
}


public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
//获取hadoop相关的配置
Configuration conf = new Configuration();
//创建作业Job
Job job = Job.getInstance(conf);
//给任务起一个名字,这个名字将来会在yarn中能看到
job.setJobName("word count");
//设置reduce的个数,默认1个
// job.setNumReduceTasks(1);
//设置该任务的运行的主类
job.setJarByClass(WordCount.class);

//设置该任务将来的的map类
job.setMapperClass(MyMapper.class);
//设置该任务将来的的reduce类
job.setReducerClass(MyReducer.class);

//设置map阶段k-v输出的数据类型
//hadoop中字符串的类型对应的是叫做Text
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);

//设置reduce阶段k-v输出的数据类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);

//设置HDFS的输入路径和输出路径
FileInputFormat.addInputPath(job, new Path(args[0]));
//注意,这里设置的是输出的目录
FileOutputFormat.setOutputPath(job, new Path(args[1]));


//启动mr任务
job.waitForCompletion(true);


}
}

标签:LongWritable,map,Text,MapReduce,单词,job,举例,new,class
From: https://www.cnblogs.com/xiaoxiao-/p/16633385.html

相关文章

  • leetcode139:单词拆分
    packagecom.mxnet;importjava.util.HashSet;importjava.util.List;publicclassSolution139{publicstaticvoidmain(String[]args){}/**......
  • MapReduce计算流程
    MapReduce的计算流程1.1原始数据FileThebookschronicletheadventuresoftheadolescentwizardHarryPotterandhisbestfriendsRonWeasleyandHermioneGra......
  • # autoit 自动翻译并附注 pdf中不认识的单词
    autoit自动翻译并附注pdf中不认识的单词学印尼语遇到一个问题,即需要大量复制pdf文本中的个别单词到谷歌翻译中,这个过程需要重复点击和按键,及其耗费时间,为此写了一个脚本......
  • MapReduce-day1
    MapReducehadoop-ha问题dfs.ha.fencing.methods表示:alistofscriptsorJavaclasseswhichwillbeusedtofencetheActiveNameNodeduringafailover而配置......
  • MapReduce核心原理(下)
    MapReduce中的排序MapTask和ReduceTask都会对数据按key进行排序。该操作是Hadoop的默认行为,任何应用程序不管需不需要都会被排序。默认排序是字典顺序排序,排序方法......
  • 79. 单词搜索
    79.单词搜索给定一个 mxn二维字符网格 board和一个字符串单词 word。如果 word存在于网格中,返回true;否则,返回false。单词必须按照字母顺序,通过相邻的单元......
  • 程序员最容易读错的单词,听到status我炸了
    最近在跟同事讨论问题的时候,他突然对我说。。。这个死太丢死不太对,需要改一下。。。我当时应该是愣住了,然后想了一下,你说的是status吗???看着他疑惑不解的眼神,我当时的表......
  • 1455. 检查单词是否为句中其他单词的前缀
    1455.检查单词是否为句中其他单词的前缀给你一个字符串sentence作为句子并指定检索词为searchWord,其中句子由若干用单个空格分隔的单词组成。请你检查检索词se......
  • SAS 用cards/datalines读入原始数据举例
    SAS用cards/datalines读入原始数据:input作用:1)当数据没有这个变量时生成新变量2)读取cards或外部数据。语法:inputinformat.  在input设定的输入格式并不存储在创......
  • 总结~音节=>单词---如何划分音节及音节的类型
    参考:https://baijiahao.baidu.com/s?id=1667812287459301608&wfr=spider&for=pc   大家好,今天我们一起来学习下音节的相关内容,本文涉及到了音节的定义,如何划分......