MapReduce 简单开发
先给出 maven 依赖配置
<properties>
<hadoop.version>2.6.0</hadoop.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
</dependencies>
打包的话可以参考之前的 build 配置 把所有的 jar 打包成一个
执行命令
hadoop jar dst.jar cc.stdpain.XXX $INPUT_PATH/ $OUTPUT_PATH/
MapReduce 包括Map 和 Reduce 两个过程
我们可以使用继承的方式开发
public static class StdMapper extends Mapper<Object, Text, Text, Text> {
@Override
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
/**
编写一些解析方法
*/
context.write(new Text(key.toString()), new Text(line));
}
}
Reduce 开发
public static class StdReducer extends Reducer<Text, Text, NullWritable, Text> {
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws Exception {
//相同的key会放在一起 进行一些Reduce 操作
for (Text text : values) {
//如果不需要Reduce操作也不需要key,就可以使用 NullWritable
context.write(NullWritable.get(), text);
}
}
}
Main
Configuration conf = new Configuration();
//获取参数 args 为main中的 args
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length < 2) {
System.out.println("Error param");
System.exit(-1);
}
设置任务
Job job = new Job(conf, "XXXJOB");
job.setJarByClass(XXXMain.class);//main的class
job.setMapperClass(XXXMain.StdMapper.class);//Mapper的class
job.setReducerClass(XXXMain.StdReducer.class);//Reducer的class
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
设置输入输出路径
String inputPath = otherArgs[(otherArgs.length - 2)];
FileSystem fs = FileSystem.get(conf);
Path path = new Path(inputPath);
FileStatus[] fileStatuses = fs.listStatus(path);
for (FileStatus fileStatus : fileStatuses) {
if (fileStatus.getPath().getName().equals("_C_SUCCESS")) {
continue;
}
FileInputFormat.addInputPath(job, fileStatus.getPath());
}
FileOutputFormat.setOutputPath(job, new Path(otherArgs[(otherArgs.length - 1)]));
//压缩 不需要可以注释
//FileOutputFormat.setCompressOutput(job, true);
FileOutputFormat.setCompressOutput(job, false);
System.exit(job.waitForCompletion(true) ? 0 : 1);
MapReduce 简单开发的更多相关文章
- Hadoop权威指南:MapReduce应用开发
Hadoop权威指南:MapReduce应用开发 [TOC] 一般流程 编写map函数和reduce函数 编写驱动程序运行作业 用于配置的API Hadoop中的组件是通过Hadoop自己的配置API ...
- MapReduce教程(一)基于MapReduce框架开发<转>
1 MapReduce编程 1.1 MapReduce简介 MapReduce是一种编程模型,用于大规模数据集(大于1TB)的并行运算,用于解决海量数据的计算问题. MapReduce分成了两个部分: ...
- 微信公众号PHP简单开发流程
原文:微信公众号PHP简单开发流程 微信公众号开发分傻瓜模式和开发者模式两种,前者不要考虑调用某些接口,只要根据后台提示傻瓜式操作即可,适用于非专业开发人员. 开发模式当然就是懂程序开发的人员使用的. ...
- Fluent Nhibernate之旅(五)--利用AutoMapping进行简单开发
Fluent Nhibernate(以下简称FN)发展到如今,已经相当成熟了,在Nhibernate的书中也相应的推荐了使用FN来进行映射配置,之前写的FN之旅至今还有很多人会来私信我问题,说来惭愧, ...
- C语言 动态库简单开发
动态库项目 //简单的动态库开发----报文发送 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib. ...
- MapReduce 简单的全文搜索2
上一个全文搜索实现了模糊查找,这个主要实现了精确查找,就是比如你查找mapreduce is simple那么他就只查找有这个句子的文章,而不是查找有这三个单词的文章. 这个版本需要重写反向索引,因为 ...
- 简单开发Apple Watch的步骤
好久没写博客了,自己这两年自从孩子出世,也慢慢懈怠了.实在有点对不住了,换了个新公司,也有点时间可以写写东西了. 前几天苹果刚刚发布Apple Watch,Xcode6也更新了watchKit,正好 ...
- 基于HBase Hadoop 分布式集群环境下的MapReduce程序开发
HBase分布式集群环境搭建成功后,连续4.5天实验客户端Map/Reduce程序开发,这方面的代码网上多得是,写个测试代码非常容易,可是真正运行起来可说是历经挫折.下面就是我最终调通并让程序在集群上 ...
- webapp 的简单开发
web app 的技术平台很多,如adobe phonegap.sencha touch.appcan(国产).dcloud(国产)平台.我选择了dcloud平台,原因:简单,容易上手. web ap ...
随机推荐
- PowerShell常用命令及美化(现代化的CMD)
PowerShell可谓现代终端,是微软用来替代古老的CMD的. PowerShell拥有面向对象的思想,非常方便. 常用命令 下载文件(此处以install.ps1文件为例) $client = n ...
- Spring Boot 知识笔记(整合Redis)
一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- Export failed for github.com/hashicorp/consul: Unable to export source: exit status 128
背景 go项目,使用glide install命令去下载安装依赖,依赖中有个github.com/hashicorp/consul 问题描述 一直无法下载安装依赖成功,报错如下: [ERROR] Ex ...
- 《TP5.0学习笔记---模板变量输出、替换和赋值篇》
原文地址:http://blog.csdn.net/self_realian/article/details/75214922 模板变量输出.替换和赋值 我们看一下文件编译的结果,我们知道我们现在写的 ...
- C# HTTP系列11 以普通文件流方式上传文件远程服务器
系列目录 [已更新最新开发文章,点击查看详细] 应用程序中上传附件是最常使用的操作之一,ASP.NET客户端一般通过上传控件实现, <input type="file" ...
- sql server 批量备份数据库及删除N天前的备份数据
很多时候,我们都需要将数据库进行备份,当服务器上数据库较多时,不可能一个数据库创建一个定时任务进行备份,这时,就需要进行批量的数据库备份操作,好了,废话不多说,具体实现语句如下: 1 2 3 4 5 ...
- select2的简单使用
静态下拉列表 修改 type_template.html 引入JS <!-- slect2插件--> <link rel="stylesheet" href=& ...
- Vagrant 安装Oracle19c RAC测试环境的简单学习
1. 学习自网站: https://xiaoyu.blog.csdn.net/article/details/103135158 简单学习了下 能够将oracle RAC开起来了 但是 对后期的维护和 ...
- UML类图记忆口诀
UML类图在设计模式书籍中用的比较多,经常忘记,口诀挺重要的,比如我们从小到大,除了乘法口诀.元素周期表等口诀形式的知识,其它的知识都基本忘记了, 所以编写口诀如下 1.三级石 2.见关一 3.零足迹 ...
- IntelliJ idea 撤回(已经commit未push的)操作
VSC => Git => reset head => 退回到上次commit => 退回到第2次提交之前 => 退回到指定commit版本