Hadoop之Mapreduce 程序
package com.gylhaut.hadoop.senior.mapreduce; 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.LongWritable;
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; /**
* Shift +Alt +S 快捷键用法
*
*/
public class WordCount {
// step 1:Map Class
public static class WordCountMapper extends
Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text(); @Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
} // step 2: Reduce Class
public static class WordCountReducer extends
Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable(); @Override
public void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException { int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
} // step 3: Driver, component job
public int run(String[] args) throws Exception {
// 1.get configuration
Configuration configuration = new Configuration();
// 2:create job
Job job = Job.getInstance(configuration, this.getClass()
.getSimpleName());
// run jar
job.setJarByClass(this.getClass());
// 3.set job
// input ->map ->reduce->output
// 3.1 input
Path inPath = new Path(args[0]);
FileInputFormat.addInputPath(job, inPath);
// 3.2 map
job.setMapperClass(WordCountMapper.class);
// 设置map 输出类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
// 3.3 reduce
job.setReducerClass(WordCountReducer.class);
// 设置reduce 输出类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// 3.4 output
Path outPath = new Path(args[1]);
FileOutputFormat.setOutputPath(job, outPath);
// 4.submit job
boolean isSuccess = job.waitForCompletion(true); return isSuccess ? 0 : 1; } public static void main(String[] args) throws Exception {
int status = new WordCount().run(args);
System.exit(status);
}
}
Hadoop之Mapreduce 程序的更多相关文章
- 用PHP编写Hadoop的MapReduce程序
用PHP编写Hadoop的MapReduce程序 Hadoop流 虽然Hadoop是用Java写的,但是Hadoop提供了Hadoop流,Hadoop流提供一个API, 允许用户使用任何语言编 ...
- Hadoop之MapReduce程序应用三
摘要:MapReduce程序进行数据去重. 关键词:MapReduce 数据去重 数据源:人工构造日志数据集log-file1.txt和log-file2.txt. log-file1.txt内容 ...
- 如何在Windows下面运行hadoop的MapReduce程序
在Windows下面运行hadoop的MapReduce程序的方法: 1.下载hadoop的安装包,这里使用的是"hadoop-2.6.4.tar.gz": 2.将安装包直接解压到 ...
- 如何在Hadoop的MapReduce程序中处理JSON文件
简介: 最近在写MapReduce程序处理日志时,需要解析JSON配置文件,简化Java程序和处理逻辑.但是Hadoop本身似乎没有内置对JSON文件的解析功能,我们不得不求助于第三方JSON工具包. ...
- HADOOP之MAPREDUCE程序应用二
摘要:MapReduce程序进行单词计数. 关键词:MapReduce程序 单词计数 数据源:人工构造英文文档file1.txt,file2.txt. file1.txt 内容 Hello Ha ...
- hadoop开发MapReduce程序
准备工作: 1.设置HADOOP_HOME,指向hadoop安装目录 2.在window下,需要把hadoop/bin那个目录替换下,在网上搜一个对应版本的 3.如果还报org.apache.hado ...
- 在window下远程虚拟机(centos)hadoop运行mapreduce程序
(注:虽然连接成功但是还是执行不了.以后有时间再解决吧 看到的人别参考仅作个人笔记)先mark下 1.首先在window下载好一个eclipse.和拷贝好linux里面hadoop版本对应的插件(我是 ...
- hadoop-初学者写map-reduce程序中容易出现的问题 3
1.写hadoop的map-reduce程序之前所必须知道的基础知识: 1)hadoop map-reduce的自带的数据类型: Hadoop提供了如下内容的数据类型,这些数据类型都实现了Writab ...
- 对于Hadoop的MapReduce编程makefile
根据近期需要hadoop的MapReduce程序集成到一个大的应用C/C++书面框架.在需求make当自己主动MapReduce编译和打包的应用. 在这里,一个简单的WordCount1一个例子详细的 ...
随机推荐
- 计算当前日期n天后的日期
//计算180天后的日期//180*24*60*60*1000//更具时间戳计算n天前的日期 $(function () { var timestamp =Date.parse(new Date()) ...
- NS前缀
NS来自于NeXTStep的一个软件 NeXT Software OC中不支持命名空间(namespace) NS是为了避免命名冲突而给的前缀 看到NS前缀就知道是Cocoa中的系统类的名称
- iOS 即使通讯第三方SDK 资料
第三方即时通讯SDK,下面是一些主流的第三方的即时通讯SDK,尽管不能查看里面的源代码,但通过查看头文件,能为实现自己的即使通讯SDK提供很好的思路.(备用) 容云 容联.云通讯 IMSDK - 轻松 ...
- LVS负载均衡群集部署——DR模式
LVS负载均衡群集部署--DR模式 1.LVS-DR概述 2.部署实验 1.LVS-DR概述: LVS-DR(Linux Virtual Server Director Server)工作模式,是生产 ...
- 2022寒假集训day6
day6上午还是做四道题T1区域[上机练习]1.编程计算由"*"号围成的下列图形的面积.面积计算方法是统计*号所围成的闭合曲线中水平线和垂直线交点的数目.如下图所示,在 10*10 ...
- JDK、JRE 和 JVM 有什么用,它们是怎样运行的
JDK如何运作? JDK 功能 以下是JDK的重要组件: JDK 和 JRE:程序员通过使用JDK 创建由 JRE 运行的 Java 程序,其中包括 JVM 和类库. 类库:是一组可动态加载的库,Ja ...
- Java的Future接口
Java的Future接口 Java 中的 Future 接口和其实现类 FutureTask,代表了异步计算的结果. 1. Future接口简介 Future 是异步计算结果的容器接口,它提供了下面 ...
- vscode打开多个文件
vscode短时间内打开多个文件会覆盖原先打开的文件,在右方编辑区只显示一个.若想每次打开,都新创建一个编辑,可以用以下2个简单的方法: 方法一:直接在右侧打开的文件上,Ctrl + S,保存一次,再 ...
- 微信小程序蓝牙开发
微信小程序蓝牙控制方案: 蓝牙模块如何快速改名并绑定用户手机?这样即使多台蓝牙设备在同一个地方使用也可以互不干扰,燧星科技给出解决方案. 长按控制板5秒进入待绑定下状态,点击"添加蓝牙设备& ...
- S32Kxxx bootloader之CAN bootloader
了解更多关于bootloader 的C语言实现,请加我Q扣: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 最近完成了S32Kxx ...