【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 ...
随机推荐
- 如何连接到远程windows服务器
在学习和工作中,有些时候我们需要连接到远程服务器,这里,我来演示如何连接到windows服务器. 1. 首先利用快捷键win+r打开运行,或者在搜索框搜索“运行”,然后在运行框输入mstsc,点击确定 ...
- Flutter中通过普通的点击事件修改TextFormField的值
import 'package:flutter/material.dart'; import 'package:zhongfa_apps/widget/public/PublicWidget.dart ...
- 软件定义网络基础---REST API的设计规范
一:REST API的设计 REST API是基于HTTP协议进行设计的,由HTTP动词+URI组成 (一)HTTP动词 (二)资源的原型 文档(Document): 文档是资源的单一表现形式: 集合 ...
- python之psutil模块
简述 psutil是一个跨平台库(http://code.google.com/p/psutil/) ,能够轻松实现获取系统运行的进程和系统利用率(包括CPU.内存.磁盘.网络等)信息.它主要应用于系 ...
- Java8 Stream流方法
流是Java API的新成员,它允许以声明性方式处理数据集合(通过查询语句来表达,而不是临时编写一个实现).就现在来说,可以把它们看成遍历数据集的高级迭代器.此外,流还可以透明地并行处理,无需写任何多 ...
- [转自baidu]修正古人五行,《七行说》之提出
一.论原五行相生.相克关系不妥之处: 祖国医学原“五行说”即金.木.水.火.土.在五行学说中说相生规律为:金生水:水生木:木生火:火生土:土生金.相克规律为:火克金:金克木:木克土:土克水:水克火.这 ...
- jenkins部署集群
环境: 两台Centos7.3系统 master:172.16.1.227 slave:172.16.1.228 其中一台作为master,另一台为slave(slave服务器上无需安装jenkins ...
- redis内存分析工具rdbtools
当Redis的内存已经快满的时候,我们能做什么呢? 最直接的方法就是分析一下Redis内存的构成,看是哪些键比较大,或者比较多,然后考虑一下对应的功能能不能优化,例如减少超时时间,例如不必要的数据不用 ...
- python:单元测试框架pytest的一个简单例子
之前一般做自动化测试用的是unitest框架,发现pytest同样不错,写一个例子感受一下 test_sample.py import cx_Oracle import config from sen ...
- mysql索引数据结构
什么是索引?索引就是排好序的数据结构,可以帮助我们快速的查找到数据 推荐一个网站,可以演示各种数据结构:https://www.cs.usfca.edu/~galles/visualization/A ...