利用hadoop计算文件中的最大值
2016-04-25 20:43
363 查看
本文主要是找出文件中的最大值假设文件的类型为:
A 66
B 72
C 91
A 43
C 62
C 85
B 35
A 23
B 39
找出文件中的每个对象的最大值:
A 66
B 72
C 91
操作代码解析如下:
A 66
B 72
C 91
A 43
C 62
C 85
B 35
A 23
B 39
找出文件中的每个对象的最大值:
A 66
B 72
C 91
操作代码解析如下:
import java.io.IOException; import java.net.URI; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.fs.FileSystem; 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.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; public class MaxNumMR extends Configured implements Tool{ @Override public int run(String[] args) throws Exception { //获取文件系统的配置文件 Configuration conf = new Configuration() ; URI uri = new URI("hdfs://hadoop:9000") ; //声明文件系统的对象 FileSystem fs = FileSystem.get(uri, conf) ; //结果的输出目录,如果该目录存在则删除 Path path = new Path("hdfs://hadoop:9000/ming/out2") ; if(fs.isDirectory(path)){ fs.delete(path, true) ; } //声明作业对象 Job job = new Job(conf); job.setJarByClass(MaxNumMR.class); //配置Mapper的类和输出的格式 job.setMapperClass(MaxMapper.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(Text.class); //配置Reducer的类和输出的格式 job.setReducerClass(MaxReducer.class) ; job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); //配置文件的输入和输出文件 FileInputFormat.addInputPath(job, new Path("hdfs://hadoop:9000/ming/math.txt")); FileOutputFormat.setOutputPath(job, path); return job.waitForCompletion(true)?0:1; } public static void main(String[] args) throws Exception { //主方法,代码的入口点 int e = ToolRunner.run(new Configuration(), new MaxNumMR(), args); System.exit(e); } public static class MaxMapper extends Mapper<LongWritable,Text,Text,Text>{ @Override protected void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException{ String line = value.toString() ; //对数据进行切分,然后存入输出的文件中 String[] param = line.split("\t") ; context.write(new Text(param[0]),new Text(param[1])) ; } } public static class MaxReducer extends Reducer<Text,Text,Text,Text>{ @Override protected void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException{ long max = 0L ; for(Text t:values){ //判断数据的大小 if(Long.parseLong(t.toString()) > max){ max = Long.parseLong(t.toString()) ; } } context.write(key, new Text(String.valueOf(max))); } } }
相关文章推荐
- 高焕堂Android架构技术全集
- 高焕堂Android架构技术全集
- OpenCV —— AR实现思路
- 每天一个linux命令:chgrp
- Docker容器与容器云
- 关于linux硬盘分区的简单方法
- docker容器网络通信原理分析
- scanf那些事
- 如何使用Linux的Crontab定时执行PHP脚本的方法
- 每天一个linux命令:chown
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) B. Little Artem and Grasshopper 模拟题
- 有趣的linux文件bb
- linux shell
- 【Linux】UNDERSTANDING LINUX CPU LOAD - WHEN SHOULD YOU BE WORRIED?
- 【Linux C 王者归来】【第四章】【指针与字符串】
- CentOS7安装vncserver(启动失败及连接黑屏解决办法)
- CentOS下安装setuptools、pip和virtualenv
- CentOS 6.5环境使用ansible剧本自动化部署Corosync + pacemaker环境及corosync常用配置详解
- Linux 大于2T 解决方法
- CentOS 6.5环境使用ansible剧本自动化部署Corosync + pacemaker环境及corosync常用配置详解