简单的计算最值的MapReduce程序
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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;
public class max_number {
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
String line=value.toString();
StringTokenizer itr = new StringTokenizer(line,"\n");
while (itr.hasMoreTokens()) {
StringTokenizer tkl=new StringTokenizer(itr.nextToken());
String sKey=tkl.nextToken();
String sNum=tkl.nextToken();
Text key=new Text(sKey);
int numInt=Integer.parseInt(sScore);
context.write(key, new IntWritable(numInt));
}
}
}
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int max = -219876543;
Iterator<IntWritable>iterator=values.iterator();
while(iterator.hasNext())
{
int max1= iterator.next().get();
if(max1>=max){max=max1;}
} context.write(key, new IntWritable(max));
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "max number");
job.setJarByClass(max_number.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
运行需要的设置
导入hadoop的class和java_home路径
export JAVA_HOME=/usr/java/default
export PATH=${JAVA_HOME}/bin:${PATH}
export HADOOP_CLASSPATH=${JAVA_HOME}/lib/tools.jar
--编译写好的MapReduce java程序
$ $Hadoop_Home/bin/hadoop com.sun.tools.javac.Main max_number.java
$ jar cf maxnumber.jar max_number*.class
--上传文件到hdfs
$Hadoop_Home/bin/hadoop fs --copyFromLocal /home/hadoop/hadoop/random.txt /test/random
--执行
$Hadoop_Home/bin/hadoop jar maxnumber.jar max_number /test/random /output11/minnumber2 //练习执行的脚本
查询输出Output:
$ $Hadoop_Home/bin/hadoop fs -cat /output11/minnumber2/part-r-00000`
简单的计算最值的MapReduce程序的更多相关文章
- 计算均值mean的MapReduce程序Computing mean with MapReduce
In this post we'll see how to compute the mean of the max temperatures of every month for the city o ...
- 简单的java Hadoop MapReduce程序(计算平均成绩)从打包到提交及运行
[TOC] 简单的java Hadoop MapReduce程序(计算平均成绩)从打包到提交及运行 程序源码 import java.io.IOException; import java.util. ...
- 编写简单的Mapreduce程序并部署在Hadoop2.2.0上运行
今天主要来说说怎么在Hadoop2.2.0分布式上面运行写好的 Mapreduce 程序. 可以在eclipse写好程序,export或用fatjar打包成jar文件. 先给出这个程序所依赖的Mave ...
- [python]使用python实现Hadoop MapReduce程序:计算一组数据的均值和方差
这是参照<机器学习实战>中第15章“大数据与MapReduce”的内容,因为作者写作时hadoop版本和现在的版本相差很大,所以在Hadoop上运行python写的MapReduce程序时 ...
- OpenJudge计算概论-简单算术表达式求值
/*===================================== 简单算术表达式求值 总时间限制: 1000ms 内存限制: 65536kB 描述 2位正整数的简单算术运算(只考虑整数运 ...
- 从零开始学习Hadoop--第2章 第一个MapReduce程序
1.Hadoop从头说 1.1 Google是一家做搜索的公司 做搜索是技术难度很高的活.首先要存储很多的数据,要把全球的大部分网页都抓下来,可想而知存储量有多大.然后,要能快速检索网页,用户输入几个 ...
- Hadoop(十三)分析MapReduce程序
前言 刚才发生了悲伤的一幕,本来这篇博客马上就要写好的,花了我一晚上的时间.但是刚才电脑没有插电源就没有了.很难受!想哭,但是没有办法继续站起来. 前面的一篇博文中介绍了什么是MapReduce,这一 ...
- 第一个MapReduce程序——WordCount
通常我们在学习一门语言的时候,写的第一个程序就是Hello World.而在学习Hadoop时,我们要写的第一个程序就是词频统计WordCount程序. 一.MapReduce简介 1.1 MapRe ...
- hive--构建于hadoop之上、让你像写SQL一样编写MapReduce程序
hive介绍 什么是hive? hive:由Facebook开源用于解决海量结构化日志的数据统计 hive是基于hadoop的一个数据仓库工具,可以将结构化的数据映射为数据库的一张表,并提供类SQL查 ...
随机推荐
- Python 文件编码(文件乱码)
IndentationError: unindent does not match any outer indentation level 文件未对齐,在记事本打开. 乱码原因:源码文件的编码格式为u ...
- Linux学习笔记---用户管理---组group
组管理: (1)/etc/group 格式: 组名:密码:GID:组员
- Rearrange a string so that all same characters become d distance away
Given a string and a positive integer d. Some characters may be repeated in the given string. Rearra ...
- 【iCore3 双核心板】例程二十一:LAN_TCPS实验——以太网数据传输
实验指导书及代码包下载: http://pan.baidu.com/s/1ntTjWpV iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- 领导者/追随者(Leader/Followers)模型和半同步/半异步(half-sync/half-async)模型都是常用的客户-服务器编程模型
领导者-追随者(Leader/Followers)模型的比喻 半同步/半异步模型和领导者/追随者模型的区别: 半同步/半异步模型拥有一个显式的待处理事件队列,而领导者-追随者模型没有一个显式的队列(很 ...
- ios-滚动视图滚动取消键盘
_scroll.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;_SearchTable.keyboardDismissMode ...
- ASP.NET MVC 5 中 使用富文本编辑器 Ueditor
一.Ueditor插件下载自:http://ueditor.baidu.com/website/ 二.将解压文件目录ueditor复制到项目根目录后, 修改以下几个文件配置: 1.访问路径配置:ued ...
- sublimetext ruby 插件
写ruby的编辑器推荐 俗话说磨刀不误砍柴工,好的编辑器可以写的更舒服,更快. 完全初学者建议用RubyMine,这个目前估计最强的写Ruby的IDE. 不过我没有使用它,因为它速度太慢了.如果你能 ...
- Python中的函数修饰符@
首先,什么是函数修饰符?函数修饰符就是对原有函数做一层包装.比如有以下两个函数: 复制代码 def func1(): print 'I am function func1' def func2(): ...
- Java maven安装GDAL
1. 使用编译好的安装jdal http://www.gisinternals.com/release.phpgdal-111-1800-x64-core.msi下载地址:http://downloa ...