简单的计算最值的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查 ...
随机推荐
- BizTalk开发系列(十三) Schema设计之值约束
XML Schema 的作用是定义 XML 文档的合法构建模块.在开发过程中有时需要对XML文档做精确的约束.以保证XMl数据的准确性. 今天我们以一个班级Sample来讲探讨一下如何在开发BizTa ...
- Unity Svn(转)
先吐个槽.关于这个国内各种简单到家的文章让人搞不懂,而且场景合并,prefab合并等关键问题都说没法解决,其实本质就是因为它们都是二进制文件,所以SVN没法对其合并,但事实上Unity是支持把这些文件 ...
- J2EE用户CPU占用过大后的分析过程
1.找到最耗CPU的java线程ps命令 命令:ps -mp pid -o THREAD,tid,time 或者 ps -Lfp pid 结果展示: 2.可以获取到 ...
- INSTRUCTION CYCLE
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION • Fetch: Read the nex ...
- NSString(或者说是UILabel)加入 “行间距” 之后的 “高度”计算
一.首先,写一个工具类(NSString的分类,增加两个功能,计算高度宽度) #import "NSString+Extension.h" @implementation NSSt ...
- LeetCode Range Sum Query 2D - Mutable
原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...
- Matlab代码备忘
1.Matlab写入文件 set(hp1,'xdata',bbb(1,:),'ydata',bbb(2,:),'zdata',bbb(3,:)); M=size(bbb,2); name=strca ...
- 三维高斯模型 opencv实现
OnProbabilityModel() { int i; for(int x=0;x<workImg->height;x++) { for(int y=0;y<workImg-&g ...
- (九)串行口方式0 拓展并行输出端口 02 74LS164芯片
1.先讲解74LS164 移位芯片: 74HC164.74HCT164 是 8 位边沿触发式移位寄存器,串行输入数据,然后并行输出. 数据通过两个输入端(DSA 或 DSB)之一串行输入:任一输入端可 ...
- ASP.NET中的Image和ImageButton控件
Image 控件用来显示图形.Image 控件可以显示来自位图.图标或元文件的图形,也可以显示增强的元文件.JPEG 或 GIF文件. ImageButton 控件用于显示可点击的图像. Image ...