前言:今天刚开始看到map和reduce类里面的内容时,说实话一片迷茫,who are you?,最后实在没办法,上B站看别人的解说视频,再加上自己去网上查java的包的解释,终于把WordCount例子看懂,准备后面自己写一遍!实话说,现在实在肝不动了,每天只有晚上有点时间来学习,代码贴上来,睡觉!

正文:实在不想写太多,解释都在代码的注释里面,饶了我吧!

贴一个讲的比较好的网址:https://www.cnblogs.com/houji/p/7161468.html

代码如下:

/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hadoop; import java.io.IOException;
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.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;
import org.apache.hadoop.util.GenericOptionsParser; public class WordCount {//WordCount是类名,要用public class进行修饰,java程序由类(class)组成,一个源文件可以包含多个类 public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
/***
Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT>
行偏移量 输入值 输出key 输出值
***/ private final static IntWritable one = new IntWritable(1);
private Text word = new Text(); public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());//value.toString()获取输入值,并用StringTokenizer进行分隔(默认空格)
while (itr.hasMoreTokens()) { //判断itr是否还有字符串,返回true或false
word.set(itr.nextToken()); //set方法给word赋值,nextToken()返回下一个标记
context.write(word, one);//输出<'word',1>
}
}
} public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, //values 里面存储着map输出数据,格式为 'word list<1,1,1,1,1>'
Context context
) throws IOException, InterruptedException { int sum = 0;//自定义一个计数器
for (IntWritable val : values) { //循环list里面的值
sum += val.get();//求和
}
result.set(sum);//赋值给result
context.write(key, result);
}
} public static void main(String[] args) throws Exception { //1、Java程序的入口,public static void main(String[] args){}是固定用法,public static void都是关键字。2、throws:声明一个异常可能被抛出
/***
Create a new Job
***/
Configuration conf = new Configuration(); //实例化Configuration,读取Hadoop配置信息
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); //读取Hadoop的argument填入地址信息
if (otherArgs.length < 2) {//若填入的地址小于2,报错并输出"Usage: wordcount <in> [<in>...] <out>"
System.err.println("Usage: wordcount <in> [<in>...] <out>");
System.exit(2);
}
Job job = Job.getInstance(conf, "word count");//单例模式getInstance(),在主函数开始时调用,返回一个实例化对象,此对象是static的,在内存中保留着它的引用
job.setJarByClass(WordCount.class);
//设置Job处理的Map(拆分)、Combiner(中间结果合并)以及Reduce(合并)的相关处理类
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
//设置job输出结果<key,value>的中key和value数据类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
/***
调用addInputPath()和setOutputPath()设置输入输出路径置
InputFormat()方法是用来生成可供map处理的<key,value>对的
***/
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); //运行job
}
}

【hadoop】看懂WordCount例子的更多相关文章

  1. hadoop安装与WordCount例子

    1.JDK安装 下载网址: http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u29-download-513648.html  ...

  2. 三.hadoop mapreduce之WordCount例子

    目录: 目录见文章1 这个案列完成对单词的计数,重写map,与reduce方法,完成对mapreduce的理解. Mapreduce初析 Mapreduce是一个计算框架,既然是做计算的框架,那么表现 ...

  3. RedHat 安装Hadoop并运行wordcount例子

    1.安装 Red Hat 环境 2.安装JDK 3.下载hadoop2.8.0 http://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/had ...

  4. 看懂c/c++ 函数、指针、数组定义

    读懂 函数 + 指针 + 数组 c语言运算符机器优先级,看这里 结合运算符优先级,我们试着读懂函数和指针 优先级简单看 表达式提升():一级优先 函数():二级优先 数组[]:二级优先 指针定义*:三 ...

  5. 一文看懂大数据的技术生态圈,Hadoop,hive,spark都有了

    一文看懂大数据的技术生态圈,Hadoop,hive,spark都有了 转载: 大数据本身是个很宽泛的概念,Hadoop生态圈(或者泛生态圈)基本上都是为了处理超过单机尺度的数据处理而诞生的.你可以把它 ...

  6. [Linux][Hadoop] 运行WordCount例子

    紧接上篇,完成Hadoop的安装并跑起来之后,是该运行相关例子的时候了,而最简单最直接的例子就是HelloWorld式的WordCount例子.   参照博客进行运行:http://xiejiangl ...

  7. hadoop的wordcount例子运行

    可以通过一个简单的例子来说明MapReduce到底是什么: 我们要统计一个大文件中的各个单词出现的次数.由于文件太大.我们把这个文件切分成如果小文件,然后安排多个人去统计.这个过程就是”Map”.然后 ...

  8. 一图看懂hadoop分布式文件存储系统HDFS工作原理

    一图看懂hadoop分布式文件存储系统HDFS工作原理

  9. (二)Hadoop例子——运行example中的wordCount例子

    Hadoop例子——运行example中的wordCount例子 一.   需求说明 单词计数是最简单也是最能体现MapReduce思想的程序之一,可以称为 MapReduce版"Hello ...

随机推荐

  1. thinkphp5---join联合查询

    使用thinkphp3.2进行联合查询,join联合查询: $list = M('document as d') ->join('tp_admin_column as c on d.cid = ...

  2. db2常用操作命令

    1. 打开命令行窗口 #db2cmd 2. 打开控制中心 # db2cmd db2cc 3. 打开命令编辑器 db2cmd db2ce =====操作数据库命令===== 4. 启动数据库实例 #db ...

  3. Js限制Input框只能输入数字

    <input type="text" onkeyup="value=value.replace(/[^\d]/g,'')" /> <input ...

  4. [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  5. Go语言中的值类型和引用类型

    一.值类型和引用类型值类型:int.float.bool和string这些类型都属于值类型,使用这些类型的变量直接指向存在内存中的值,值类型的变量的值存储在栈中.当使用等号=将一个变量的值赋给另一个变 ...

  6. Lambda的延迟执行

    在兼顾面向对象特性的基础上,Java语言通过Lambda表达式与方法引用等,为开发者打开了函数式编程的大门. 下面我们做一个初探. Lambda的延迟执行 有些场景的代码执行后,结果不一定会被使用,从 ...

  7. Centos7挂载新硬盘

    1.查看系统是否检测到新的硬盘设备 ls /dev/ |grep sd linux 中所有外设都会在这个目录下,对应一个文件,其中第一块硬盘是sda,第二块硬盘是sdb,第三块硬盘是sdc.其中sda ...

  8. Java开发笔记(一百三十一)Swing的列表框

    前面介绍了选择框的用法,当时为了方便用户勾勾点点,无论是复选框还是单选按钮,统统把所有选项都摆在界面上.倘若只有两三个选项还好办,要是选项数量变多比如超过五个,这么多的选择框一齐在界面罗列,不光程序员 ...

  9. mysql网文收录

    1.分布式事务 1)  聊聊分布式事务,再说说解决方案 https://www.cnblogs.com/savorboard/p/distributed-system-transaction-cons ...

  10. golan切片