[Hadoop in Action] 第4章 编写MapReduce基础程序
- 基于hadoop的专利数据处理示例
- MapReduce程序框架
- 用于计数统计的MapReduce基础程序
- 支持用脚本语言编写MapReduce程序的hadoop流式API
- 用于提升性能的Combiner
- import java.io.IOException;
- import java.util.Iterator;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.conf.Configured;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapred.FileInputFormat;
- import org.apache.hadoop.mapred.FileOutputFormat;
- import org.apache.hadoop.mapred.JobClient;
- import org.apache.hadoop.mapred.JobConf;
- import org.apache.hadoop.mapred.KeyValueTextInputFormat;
- import org.apache.hadoop.mapred.MapReduceBase;
- import org.apache.hadoop.mapred.Mapper;
- import org.apache.hadoop.mapred.OutputCollector;
- import org.apache.hadoop.mapred.Reducer;
- import org.apache.hadoop.mapred.Reporter;
- import org.apache.hadoop.mapred.TextOutputFormat;
- import org.apache.hadoop.util.Tool;
- import org.apache.hadoop.util.ToolRunner;
- public class MyJob extends Configured implements Tool {
- public static class MapClass extends MapReduceBase
- implements Mapper<Text, Text, Text, Text> {
- public void map(Text key, Text value,
- OutputCollector<Text, Text> output,
- Reporter reporter) throws IOException {
- output.collect(value, key);
- }
- }
- public static class Reduce extends MapReduceBase
- implements Reducer<Text, Text, Text, Text> {
- public void reduce(Text key, Iterator<Text> values,
- OutputCollector<Text, Text> output,
- Reporter reporter) throws IOException {
- String csv = "";
- while (values.hasNext()) {
- if (csv.length() > 0) csv += ",";
- csv += values.next().toString();
- }
- output.collect(key, new Text(csv));
- }
- }
- public int run(String[] args) throws Exception {
- Configuration conf = getConf();
- JobConf job = new JobConf(conf, MyJob.class);
- Path in = new Path(args[0]);
- Path out = new Path(args[1]);
- FileInputFormat.setInputPaths(job, in);
- FileOutputFormat.setOutputPath(job, out);
- job.setJobName("MyJob");
- job.setMapperClass(MapClass.class);
- job.setReducerClass(Reduce.class);
- job.setInputFormat(KeyValueTextInputFormat.class);
- job.setOutputFormat(TextOutputFormat.class);
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(Text.class);
- job.set("key.value.separator.in.input.line", ",");
- JobClient.runJob(job);
- return 0;
- }
- public static void main(String[] args) throws Exception {
- int res = ToolRunner.run(new Configuration(), new MyJob(), args);
- System.exit(res);
- }
- }
选项
|
描述
|
-conf <configuration file> | 指定一个配置文件 |
-D <property=value> | 给JobConf属性赋值 |
-fs <local | namenode:port> | 指定一个NameNode,可以是“local” |
-jt <local | jobtracker:port> | 指定一个JobTracker |
-files <list of files> | 指定一个以逗号分隔的文件列表,用于MapReduce作业。这些文件自动地分布到所有节点,使之可从本地获取 |
-libjars <list of jars> | 指定一个以逗号分隔的jar文件列表,使之包含在所有任务JVM的classpath中 |
-archives <list of archives> | 指定一个以逗号分隔的存档文件列表,使之可以在所有任务节点上打开 |
- 编写MapReduce程序的第一步是了解数据流;
- 基于对数据流的理解,可以为输入、中间结果、输出的键/值对k1、v1、k2、v2、k3和v3设定类型;
- 根据数据流河数据类型,很容易能够理解程序代码。
- import java.io.IOException;
- import java.util.Iterator;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.conf.Configured;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapred.FileInputFormat;
- import org.apache.hadoop.mapred.FileOutputFormat;
- import org.apache.hadoop.mapred.JobClient;
- import org.apache.hadoop.mapred.JobConf;
- import org.apache.hadoop.mapred.KeyValueTextInputFormat;
- import org.apache.hadoop.mapred.MapReduceBase;
- import org.apache.hadoop.mapred.Mapper;
- import org.apache.hadoop.mapred.OutputCollector;
- import org.apache.hadoop.mapred.Reducer;
- import org.apache.hadoop.mapred.Reporter;
- import org.apache.hadoop.mapred.TextOutputFormat;
- import org.apache.hadoop.util.Tool;
- import org.apache.hadoop.util.ToolRunner;
- public class CitationHistogram extends Configured implements Tool {
- public static class MapClass extends MapReduceBase
- implements Mapper<Text, Text, IntWritable, IntWritable> {
- private final static IntWritable uno = new IntWritable(1);
- private IntWritable citationCount = new IntWritable();
- public void map(Text key, Text value,
- OutputCollector<IntWritable, IntWritable> output,
- Reporter reporter) throws IOException {
- citationCount.set(Integer.parseInt(value.toString()));
- output.collect(citationCount, uno);
- }
- }
- public static class Reduce extends MapReduceBase
- implements Reducer<IntWritable,IntWritable,IntWritable,IntWritable>
- {
- public void reduce(IntWritable key, Iterator<IntWritable> values,
- OutputCollector<IntWritable, IntWritable>output,
- Reporter reporter) throws IOException {
- int count = 0;
- while (values.hasNext()) {
- count += values.next().get();
- }
- output.collect(key, new IntWritable(count));
- }
- }
- public int run(String[] args) throws Exception {
- Configuration conf = getConf();
- JobConf job = new JobConf(conf, CitationHistogram.class);
- Path in = new Path(args[0]);
- Path out = new Path(args[1]);
- FileInputFormat.setInputPaths(job, in);
- FileOutputFormat.setOutputPath(job, out);
- job.setJobName("CitationHistogram");
- job.setMapperClass(MapClass.class);
- job.setReducerClass(Reduce.class);
- job.setInputFormat(KeyValueTextInputFormat.class);
- job.setOutputFormat(TextOutputFormat.class);
- job.setOutputKeyClass(IntWritable.class);
- job.setOutputValueClass(IntWritable.class);
- JobClient.runJob(job);
- return 0;
- }
- public static void main(String[] args) throws Exception {
- int res = ToolRunner.run(new Configuration(),
- new CitationHistogram(),
- args);
- System.exit(res);
- }
- }
- import java.io.IOException;
- import java.util.Iterable;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.conf.Configured;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.io.LongWritable;
- 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.input.TextInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
- import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
- import org.apache.hadoop.util.Tool;
- import org.apache.hadoop.util.ToolRunner;
- public class MyJob extends Configured implements Tool {
- public static class MapClass extends Mapper<LongWritable, Text, Text, Text> {
- public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
- String[] citation = value.toString().split(",");
- context.write(new Text(citation[1]), new Text(citation[0]));
- }
- }
- public static class Reduce extends Reducer<Text, Text, Text, Text> {
- public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
- String csv = "";
- for (Text val:values) { //Iterable类型允许foreach循环
- if (csv.length() > 0) csv += ",";
- csv += val.toString();
- }
- context.write(key, new Text(csv));
- }
- }
- public int run(String[] args) throws Exception {
- Configuration conf = getConf();
- Job job = new Job(conf, "MyJob");
- job.setJarByClass(MyJob.class);
- Path in = new Path(args[0]);
- Path out = new Path(args[1]);
- FileInputFormat.setInputPaths(job, in);
- FileOutputFormat.setOutputPath(job, out);
- job.setMapperClass(MapClass.class);
- job.setReducerClass(Reduce.class);
- job.setInputFormatClass(TextInputFormat.class); //兼容的InputFormat类
- job.setOutputFormatClass(TextOutputFormat.class);
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(Text.class);
- System.exit(job.waitForCompletion(true)?0:1);
- return 0;
- }
- public static void main(String[] args) throws Exception {
- int res = ToolRunner.run(new Configuration(), new MyJob(), args);
- System.exit(res);
- }
- }
- 通过Unix命令使用Streaming
- 通过脚本使用Streaming
- 用Streaming处理键/值对
- 通过Aggregate包使用Streaming
[Hadoop in Action] 第4章 编写MapReduce基础程序的更多相关文章
- 第 3 章 编写 PAM 应用程序和服务
Solaris 开发者安全性指南 Previous: 第 2 章 开发特权应用程序 Next: 第 4 章 编写使用 GSS-API 的应用程序 第 3 章 编写 PAM 应用程序和服务 可插拔验证模 ...
- [Hadoop in Action] 第5章 高阶MapReduce
链接多个MapReduce作业 执行多个数据集的联结 生成Bloom filter 1.链接MapReduce作业 [顺序链接MapReduce作业] mapreduce-1 | mapr ...
- [Hadoop in Action] 第7章 细则手册
向任务传递定制参数 获取任务待定的信息 生成多个输出 与关系数据库交互 让输出做全局排序 1.向任务传递作业定制的参数 在编写Mapper和Reducer时,通常会想让一些地方可以配 ...
- [Hadoop in Action] 第1章 Hadoop简介
编写可扩展.分布式的数据密集型程序和基础知识 理解Hadoop和MapReduce 编写和运行一个基本的MapReduce程序 1.什么是Hadoop Hadoop是一个开源的框架,可编写和运 ...
- [Hadoop in Action] 第6章 编程实践
Hadoop程序开发的独门绝技 在本地,伪分布和全分布模式下调试程序 程序输出的完整性检查和回归测试 日志和监控 性能调优 1.开发MapReduce程序 [本地模式] 本地模式 ...
- [hadoop in Action] 第3章 Hadoop组件
管理HDFS中的文件 分析MapReduce框架中的组件 读写输入输出数据 1.HDFS文件操作 [命令行方式] Hadoop的文件命令采取的形式为: hadoop fs -cmd < ...
- [Hadoop in Action] 第2章 初识Hadoop
Hadoop的结构组成 安装Hadoop及其3种工作模式:单机.伪分布和全分布 用于监控Hadoop安装的Web工具 1.Hadoop的构造模块 (1)NameNode(名字节点) ...
- 《Hadoop权威》学习笔记五:MapReduce应用程序
一.API的配置---Configuration类 API的配置:Hadoop提供了专门的API对资源进行配置,Configuration类的实例(在org.apache.hadoop.conf包)包 ...
- 编写mapreduce的程序的套路
https://blog.csdn.net/qq_42881421/article/details/83543926 给出下面6个经典案例: http://www.cnblogs.com/xia520 ...
随机推荐
- HTML中的拖拉框----在路上(29)
拖拽框---当鼠标按在指定的区域才可进行拖拽 思想:只有当鼠标是按在一个大div里的小div才可拖拽,其他不可:拖拽框有多种方法,这里以其中一种分享:下面使我自己写的demo,简单有效. <!D ...
- Entity Framework中使用IEnumerable<T>、IQueryable<T>及IList<T>的区别
1. IEnumerable<T> IEnumerable<T> :对于在内存中集合上运行的方法,返回的可枚举对象将捕获传递到方法的参数.在枚举该对象时,将使用查询运算符的逻辑 ...
- Eclipse 实用技巧
代码智能提示 Java智能提示 Window -> Preferences -> Java -> Editor -> Content Assist -> Auto Act ...
- 微信企业号开发(1)WebAPI在回调模式中的URL验证
微信回调模式的官方文档. 开发语言:C#(微信相关功能代码可以从官网下载) 首先,必须要明确几个参数,这几个参数在微信企业号中,每次调用都会使用到. 1.msg_signature:签名(已加密,加密 ...
- C#中级-开机自动启动程序
一.前言 关于C#开机自动启动程序的方法,网上出现比较多的是修改注册表: 1. HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion ...
- 自己动手写计算器v1.0
今天突发奇想,想着看了还几个设计模式了,倒不如写点东西来实践它们.发现计算器这种就比较合适,打算随着设计模式的学习,会对计算器不断的做改进. 包括功能的增加和算法的改进.初学者难免犯错,希望大家不吝指 ...
- nginx添加 nginx_heath模块
原因?为什么会使用nginx_heath 这个模块,主要是如nginx+tomcat部署的时,tomcat挂了之后nginx->upstream 轮询是可以踢掉挂掉的tomcat服务的,如果部署 ...
- 企业级应用架构模式N-Tier多层架构
先来看经典的3层架构,看下图: 涉及到平台可以是: Ruby on Rails, Java EE, ASP.NET, PHP, ColdFusion, Perl, Python 层 ...
- Struts2入门(四)——数据输入验证
一.前言 1.1.什么是输入验证?为什么需要输入验证? 在上一篇文章中,我们学习了数据类型转换,我们提到了表示层数据处理的两个方法,也提到了用户输入数据需要进行类型转换才能得到我们想要的数据,那么,我 ...
- NodeJs端口被占用的情况
在NodeJs运行的时候,我们往往会遇到一个问题:“端口被占用”,这个问题,我们的处理办法有哪些呢? 这里我只介绍一下linux下的方法: 1.使用nodeme(忘记是不是这个啦,回去查下,这个要安装 ...