前言:今天刚开始看到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. ARM USB 通信(转)

    ARM USB 通信 采用ZLG的动态链接库,动态装载. ARM是Context-M3-1343. 在C++ Builder 6 中开发的上位机通信软件. USB通信代码如下: //--------- ...

  2. 阿里云服务器 nginx 公网 IP 无法访问 浏览器

    配置完成 nginx 后, 在浏览器输入:http://ip,正常的话,会有页面,welcome to nginx但是浏览器显示访问失败 主要从两个方面找原因,一个是阿里云的安全组和服务器的防火墙是否 ...

  3. Android Studio打包没有Generate signed apk选项 解决方法

    原文地址:https://www.jianshu.com/p/9e02e55f0ba8 1.点击build栏目-并没有Generate signed apk选项 2.点击file,选中如下图所示Syn ...

  4. 泡泡一分钟:BLVD: Building A Large-scale 5D Semantics Benchmark for Autonomous Driving

    BLVD: Building A Large-scale 5D Semantics Benchmark for Autonomous Driving BLVD:构建自主驾驶的大规模5D语义基准 Jia ...

  5. python web开发——django学习(二)第一个django网站运行成功

    1.写message_form.html <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  6. vue 框架安装系列问题

    npm install --global vue-cli 错误提示:vue-cli-service' 不是内部或外部命令,也不是可运行的程序或批处理文件解决:如果是npm安装的 执行 npm -g b ...

  7. mysql使用yum源安装各个版本的mysql数据库

    每次想用yum安装旧版本的mysql时,发现都找不到mysql的yum安装源,在官网只能找到最新版本mysql的yum源.后来才知道,原来最新的mysql的yum源也是包含了旧版本的mysql的yum ...

  8. Jenkins运行python脚本出现 configparser.NoSectionError: No section: 'XXXXXX'

    原来的代码如下: def get_test_config(tag, key, config="config.ini"): cf = configparser.ConfigParse ...

  9. Go 协程

    Go 协程 协程与传统的系统级线程和进程相比,协程的优势在于其"轻量级",可以轻松创建上百万个协程而不会导致系统资源衰竭,所以协程也叫做轻量级线程. 在Go中goroutine就是 ...

  10. Shiro集成SSM基于动态URL权限管理(二)

    这个案例基于上一个demo扩展而来.所以数据库表,在Shiro集成SSM基于URL权限管理(一)开篇的一致.如果上个demo操作的建议重新导入一次,避免出现问题. 而这次都不是通过固定写在方法上的注解 ...