【hadoop】看懂WordCount例子
前言:今天刚开始看到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例子的更多相关文章
- hadoop安装与WordCount例子
1.JDK安装 下载网址: http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u29-download-513648.html ...
- 三.hadoop mapreduce之WordCount例子
目录: 目录见文章1 这个案列完成对单词的计数,重写map,与reduce方法,完成对mapreduce的理解. Mapreduce初析 Mapreduce是一个计算框架,既然是做计算的框架,那么表现 ...
- RedHat 安装Hadoop并运行wordcount例子
1.安装 Red Hat 环境 2.安装JDK 3.下载hadoop2.8.0 http://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/had ...
- 看懂c/c++ 函数、指针、数组定义
读懂 函数 + 指针 + 数组 c语言运算符机器优先级,看这里 结合运算符优先级,我们试着读懂函数和指针 优先级简单看 表达式提升():一级优先 函数():二级优先 数组[]:二级优先 指针定义*:三 ...
- 一文看懂大数据的技术生态圈,Hadoop,hive,spark都有了
一文看懂大数据的技术生态圈,Hadoop,hive,spark都有了 转载: 大数据本身是个很宽泛的概念,Hadoop生态圈(或者泛生态圈)基本上都是为了处理超过单机尺度的数据处理而诞生的.你可以把它 ...
- [Linux][Hadoop] 运行WordCount例子
紧接上篇,完成Hadoop的安装并跑起来之后,是该运行相关例子的时候了,而最简单最直接的例子就是HelloWorld式的WordCount例子. 参照博客进行运行:http://xiejiangl ...
- hadoop的wordcount例子运行
可以通过一个简单的例子来说明MapReduce到底是什么: 我们要统计一个大文件中的各个单词出现的次数.由于文件太大.我们把这个文件切分成如果小文件,然后安排多个人去统计.这个过程就是”Map”.然后 ...
- 一图看懂hadoop分布式文件存储系统HDFS工作原理
一图看懂hadoop分布式文件存储系统HDFS工作原理
- (二)Hadoop例子——运行example中的wordCount例子
Hadoop例子——运行example中的wordCount例子 一. 需求说明 单词计数是最简单也是最能体现MapReduce思想的程序之一,可以称为 MapReduce版"Hello ...
随机推荐
- java8新特性一图整理
可以右键在新选项卡打开查看大图 原图地址:https://www.processon.com/view/5abb31abe4b027675e42cebc#map
- 使用create-react-app遇到问题解决方案汇总
使用create-react-app时遇到Module not found问题 转 https://blog.csdn.net/wkq_1212/article/details/90291558 本来 ...
- WebGL学习笔记(五):变换库
在WebGL开始绘制之前,我们需要通过自己对3D空间进行矩阵和向量的运算,使用网上已经成熟的转换库,可以避免自己去实现这些复杂的数学运算. 我们这里选择的是gl-matrix库,下载地址:https: ...
- (转)IIS windows认证
转自 https://my.oschina.net/u/2551141/blog/2878673 IIS配置Windows认证方式: 1.IIS->>要设置的网站->>身份验证 ...
- OpenShift 4.1 基本问题探索
因为在OpenShift 4.1环境中不建议直接登录集群主机操作,因此很多操作可能需要在外部的Client VM上完成.当然用rhel的worker node的同事也可以和原来习惯保持一致. 这里记录 ...
- sigaction和实时信号sigqueue
sigaction函数sigaction函数的功能是用于改变进程接收到特定信号后的行为.int sigaction(int signum, const struct sigaction *act,st ...
- [LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] 529. Minesweeper 扫雷
Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...
- c++基础(三)——容器
1. 顺序容器 vector和string将元素保存在连续的内存空间中.由于元素是连续存储的,由元素的下标来计算其地址是非常快速的.但是在这两种容器的中间位置添加或删除元素就非常耗时 list和for ...
- SAS学习笔记57 template的管理
template查询 首先点击SAS Windows左上方查询框,输入“odst”或者“odstemplates”,如下所示: 然后点击enter键,进入查询的template文件夹,如下所示: 这里 ...