先给出 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 简单开发的更多相关文章

  1. Hadoop权威指南:MapReduce应用开发

    Hadoop权威指南:MapReduce应用开发 [TOC] 一般流程 编写map函数和reduce函数 编写驱动程序运行作业 用于配置的API Hadoop中的组件是通过Hadoop自己的配置API ...

  2. MapReduce教程(一)基于MapReduce框架开发<转>

    1 MapReduce编程 1.1 MapReduce简介 MapReduce是一种编程模型,用于大规模数据集(大于1TB)的并行运算,用于解决海量数据的计算问题. MapReduce分成了两个部分: ...

  3. 微信公众号PHP简单开发流程

    原文:微信公众号PHP简单开发流程 微信公众号开发分傻瓜模式和开发者模式两种,前者不要考虑调用某些接口,只要根据后台提示傻瓜式操作即可,适用于非专业开发人员. 开发模式当然就是懂程序开发的人员使用的. ...

  4. Fluent Nhibernate之旅(五)--利用AutoMapping进行简单开发

    Fluent Nhibernate(以下简称FN)发展到如今,已经相当成熟了,在Nhibernate的书中也相应的推荐了使用FN来进行映射配置,之前写的FN之旅至今还有很多人会来私信我问题,说来惭愧, ...

  5. C语言 动态库简单开发

    动态库项目 //简单的动态库开发----报文发送 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib. ...

  6. MapReduce 简单的全文搜索2

    上一个全文搜索实现了模糊查找,这个主要实现了精确查找,就是比如你查找mapreduce is simple那么他就只查找有这个句子的文章,而不是查找有这三个单词的文章. 这个版本需要重写反向索引,因为 ...

  7. 简单开发Apple Watch的步骤

    好久没写博客了,自己这两年自从孩子出世,也慢慢懈怠了.实在有点对不住了,换了个新公司,也有点时间可以写写东西了.  前几天苹果刚刚发布Apple Watch,Xcode6也更新了watchKit,正好 ...

  8. 基于HBase Hadoop 分布式集群环境下的MapReduce程序开发

    HBase分布式集群环境搭建成功后,连续4.5天实验客户端Map/Reduce程序开发,这方面的代码网上多得是,写个测试代码非常容易,可是真正运行起来可说是历经挫折.下面就是我最终调通并让程序在集群上 ...

  9. webapp 的简单开发

    web app 的技术平台很多,如adobe phonegap.sencha touch.appcan(国产).dcloud(国产)平台.我选择了dcloud平台,原因:简单,容易上手. web ap ...

随机推荐

  1. 第02组 Beta冲刺(4/4)

    队名:十一个憨批 组长博客 作业博客 组长黄智 过去两天完成的任务:了解整个游戏的流程 GitHub签入记录 接下来的计划:继续完成游戏 还剩下哪些任务:完成游戏 燃尽图 遇到的困难:没有美术比较好的 ...

  2. [LeetCode] 39. Combination Sum 组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  3. [最新].NET Core ORM 开源项目一览,持续更新

    截至2019-05-08共收集27个 .NET Core ORM 开源项目,38个 .NET ORM 开源项目. .NET Core ORM 开源项目收集地址:https://github.com/o ...

  4. Notepad++显示内容自动换行

  5. Qt Quick 组件与动态对象

    博客24## 一.Components(组件) Component 是由 Qt 框架或开发者封装好的.只暴露了必要接口的 QML 类型,可以重复利用.一个 QML 组件就像一个黑盒子,它通过属性.信号 ...

  6. Java8 新特性 Steam() 中间有状态操作

    中间有状态操作 Java8 新特性 Stream 练习实例   中间操作,就是把数据处理成自己想要的类型,并且有状态操作,是在所有的数据基础上进行操作的.比如dictinct(去重),sorted(排 ...

  7. css设置手型光标

    因为现在主流浏览器是chrome,所以要尽量使用 cursor:pointer,不要使用 cursor:hand chrome下支持的鼠标样式 default 默认光标(通常是一个箭头) auto 默 ...

  8. apache poi操作office文档----java在线预览txt、word、ppt、execel,pdf代码

    在页面上显示各种文档中的内容.在servlet中的逻辑 word: BufferedInputStream bis = null;  URL url = null;  HttpURLConnectio ...

  9. .NET Standard和.NET Core是什么关系(转载)

    .NET Standard vs .NET Core 问: I have read about the difference between .NET Standard and .NET Core, ...

  10. 玩透二叉树(Binary-Tree)及前序(先序)、中序、后序【递归和非递归】遍历

    基础预热: 结点的度(Degree):结点的子树个数:树的度:树的所有结点中最大的度数:叶结点(Leaf):度为0的结点:父结点(Parent):有子树的结点是其子树的根节点的父结点:子结点/孩子结点 ...