hadoop mapreduce 简单例子
本例子统计 用空格分开的单词出现数量( 这个Main.mian 启动方式是hadoop 2.0 的写法。1.0 不一样 )
目录结构:
使用的 maven : 下面是maven 依赖。
- <dependency>
- <groupId>org.apache.hadoop</groupId>
- <artifactId>hadoop-client</artifactId>
- <version>2.8.5</version>
- </dependency>
Main.java:
- package com.zyk.test;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.LongWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Job;
- import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
- import org.apache.hadoop.util.GenericOptionsParser;
- public class Main {
- public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
- Configuration conf = new Configuration();
- GenericOptionsParser optionParser = new GenericOptionsParser(conf, args);
- String[] remainingArgs = optionParser.getRemainingArgs();
- if ((remainingArgs.length != 2) && (remainingArgs.length != 4)) {
- System.err.println("Usage: wordcount <in> <out> [-skip skipPatternFile]");
- System.exit(2);
- }
- Job job = Job.getInstance(conf, "word count");
- job.setJarByClass(Main.class);
- job.setMapperClass(WordMap.class);
- // job.setCombinerClass(IntSumReducer.class);
- job.setReducerClass(WordReduce.class);
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(LongWritable.class);
- //FileInputFormat.addInputPath(job, new Path("/wd/in"));
- //FileOutputFormat.setOutputPath(job, new Path("/wd/out"));
- List<String> otherArgs = new ArrayList<String>();
- for (int i = 0; i < remainingArgs.length; ++i) {
- if ("-skip".equals(remainingArgs[i])) {
- job.addCacheFile(new Path(remainingArgs[++i]).toUri());
- job.getConfiguration().setBoolean("wordcount.skip.patterns", true);
- } else {
- otherArgs.add(remainingArgs[i]);
- }
- }
- FileInputFormat.addInputPath(job, new Path(otherArgs.get(0)));
- FileOutputFormat.setOutputPath(job, new Path(otherArgs.get(1)));
- System.exit(job.waitForCompletion(true) ? 0 : 1);
- }
- }
WordMap.java
- package com.zyk.test;
- import java.io.IOException;
- import org.apache.hadoop.io.LongWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Mapper;
- public class WordMap extends Mapper<LongWritable, Text, Text, LongWritable> {
- @Override
- protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context)throws IOException, InterruptedException {
- String[] words = value.toString().split(" ");
- for(String word : words) {
- context.write (new Text( word ), new LongWritable( 1 ) );
- }
- }
- }
WordReduce.java
- package com.zyk.test;
- import java.io.IOException;
- import java.util.Iterator;
- import org.apache.hadoop.io.LongWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Reducer;
- public class WordReduce extends Reducer<Text, LongWritable, Text, LongWritable > {
- @Override
- protected void reduce(Text key, Iterable<LongWritable> arg1,Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
- Iterator<LongWritable> its = arg1.iterator();
- long sum = 0L;
- while( its.hasNext() ) {
- LongWritable it = its.next();
- sum += it.get();
- }
- context.write( key , new LongWritable( sum ) );
- }
- }
content.txt 是 要上传到hdfs 上作为输入参数目录的 ,内容我就不提提供了。随便找个页面复制一些文本就可以。
然后打成 jar 包。 发布到hadoop 上运行。( 后面 两个参数是 指定的 输入 和输出路径 )运行前应该吧 要统计的文件复制到 hdfs 的 /wd/in 目录里面。
- ./hadoop jar /tools/wd.jar com.zyk.test.Main /wd/in /wd/out4
运行结果:
hadoop mapreduce 简单例子的更多相关文章
- Hadoop RPC简单例子
jdk中已经提供了一个RPC框架-RMI,但是该PRC框架过于重量级并且可控之处比较少,所以Hadoop RPC实现了自定义的PRC框架. 同其他RPC框架一样,Hadoop RPC分为四个部分: ( ...
- hadoop学习第四天-Writable和WritableComparable序列化接口的使用&&MapReduce中传递javaBean的简单例子
一. 为什么javaBean要继承Writable和WritableComparable接口? 1. 如果一个javaBean想要作为MapReduce的key或者value,就一定要实现序列化,因为 ...
- Hadoop MapReduce执行过程详解(带hadoop例子)
https://my.oschina.net/itblog/blog/275294 摘要: 本文通过一个例子,详细介绍Hadoop 的 MapReduce过程. 分析MapReduce执行过程 Map ...
- Hadoop MapReduce例子-新版API多表连接Join之模仿订单配货
文章为作者原创,未经许可,禁止转载. -Sun Yat-sen University 冯兴伟 一. 项目简介: 电子商务的发展以及电商平台的多样化,类似于京东和天猫这种拥有过亿用户的在线购 ...
- 简单的java Hadoop MapReduce程序(计算平均成绩)从打包到提交及运行
[TOC] 简单的java Hadoop MapReduce程序(计算平均成绩)从打包到提交及运行 程序源码 import java.io.IOException; import java.util. ...
- 三.hadoop mapreduce之WordCount例子
目录: 目录见文章1 这个案列完成对单词的计数,重写map,与reduce方法,完成对mapreduce的理解. Mapreduce初析 Mapreduce是一个计算框架,既然是做计算的框架,那么表现 ...
- hadoop —— MapReduce例子 (数据去重)
参考:http://eric-gcm.iteye.com/blog/1807468 例子1: 概要:数据去重 描述:将file1.txt.file2.txt中的数据合并到一个文件中的同时去掉重复的内容 ...
- hadoop自带例子SecondarySort源码分析MapReduce原理
这里分析MapReduce原理并没用WordCount,目前没用过hadoop也没接触过大数据,感觉,只是感觉,在项目中,如果真的用到了MapReduce那待排序的肯定会更加实用. 先贴上源码 pac ...
- hadoop —— MapReduce例子 (求平均值)
参考:http://eric-gcm.iteye.com/blog/1807468 math.txt: 张三 88 李四 99 王五 66 赵六 77 china.txt: 张三 78 李四 89 王 ...
随机推荐
- 运行时常量池中的符号引用/String.intern() /ldc指令
运行时常量池,之前放在方法区(永久代)中,1.8之后被转移到元空间,放到了native memory中. 具体的数据结构是:(看对象的内存布局,句柄访问还是对象头中保存指向类的元数据的指针,这里以对象 ...
- <YARN><MRv2><Spark on YARN>
MRv1 VS MRv2 MRv1: - JobTracker: 资源管理 & 作业控制- 每个作业由一个JobInProgress控制,每个任务由一个TaskInProgress控制.由于每 ...
- 20165214 实验三 敏捷开发与XP实践
一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:朱文远 学号:20165214 指导教师:娄嘉鹏 实验日期:2018年4月28日 实验时间:15:35 - 17:15 实验序号:三 ...
- 《统计学习方法》笔记(3):k近邻
k近邻(KNN)是相对基本的机器学习方法,特点是不需要建立模型,而是直接根据训练样本的数据对测试样本进行分类. 1.k近邻的算法? 算法对测试样本进行分类的一般过程如下: 1)根据给定的k值,搜索与测 ...
- day12作业答案
2.1 # lst=['asdgg','as','drtysr'] # lst2=[i.upper() for i in lst if len(i) >3 ] # print(lst2) # 2 ...
- c++ 继承(二)
不能自动继承的成员函数 1.构造函数 2.析构函数 3.=运算符 继承与构造函数 1.基类的构造函数不被继承,派生类中需要声明自己的构造函数 2.声明构造函数时,只需要对本类中新增成员进行初始化,对继 ...
- shapefile 转 geojson 文件类型
http://mapshaper.org/ 从natural earth下载后,扔进去2个文件,就可以了.
- Linux内核info leak漏洞
1 Information Leak漏洞风险 从应用层软件,到hypervisor再到kernel代码,都存在Information Leak的风险.下面给出一些示例: 应用层软件:通常是应用敏感数 ...
- TensorRT caffemodel serialize
1.TensorRT的需要的文件 需要的基本文件(不是必须的) 1>网络结构文件(deploy.prototxt) 2>训练的权重模型(net.caffemodel) TensorRT 2 ...
- flask表单,orm,csrf
flask表单是flask中最基本的功能. 它是负责HTML页面中数据采集的部分,它由三部分组成:表单标签,表单域,表单按钮组成,通过表单用户输入的数据提交给服务器. flask表单封装了WTForm ...