77范文网 - 专业文章范例文档资料分享平台

Hadoop那些事儿(四) - MapReduce编程实例(基础)(2)

来源:网络收集 时间:2019-01-10 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs(); if(otherArgs.length != 2){

System.err.println(\ System.exit(2); }

//配置作业名

Job job = new Job(conf,\ //配置作业各个类

job.setJarByClass(InvertedIndex.class); job.setMapperClass(Map.class); job.setReducerClass(Reduce.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class);

FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1);

} }

在map中,通过context.getInputSplit()获取到数据所在的文件,然后将读取的数据按句号分隔,并遍历,如果包含指定字符“月”,则将文件名作为key,该句作value写出。

在reduce中是一个简单的合并的过程。

2.最大值 最小值 平均数

问题描述

给定一批数字,获取其中的最大值 最小值 以及求得平均数

解决方案

这个问题也很简单,首先在map中读取数据并进行切割,定义一个递增的数字作key,切下来的数字作为value.在reduce中遍历value,计算数量并求和同时比较大小获取最大最小值,最后求其平均数

测试数据 输入

in1.txt

1 1 1 1 1 1 1 1 1 1 5 5 5 5 5 5 5 5 5 5

in2.txt

5 8 10 17 32 8 9 13 32 21

预期结果

平均数 11 最大值 32 最小值 1

看图说话

代码

package train;

import java.io.IOException; import java.util.StringTokenizer;

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; import org.apache.hadoop.util.GenericOptionsParser;

import test.WordCount;

/**

* 计算平均数 * @author hadoop * */

public class Average1 {

public static class Map extends Mapper{ private static IntWritable no = new IntWritable(1); //计数作为key private Text number = new Text(); //存储切下的数字

public void map(Object key,Text value,Context context) throws IOException, InterruptedException{

StringTokenizer st = new StringTokenizer(value.toString()); while(st.hasMoreTokens()){ number.set(st.nextToken());

context.write(no, new IntWritable(Integer.parseInt(number.toString()))); } } }

public static class Reduce extends Reducer{ //定义全局变量

int count = 0; //数字的数量 int sum = 0; //数字的总和 int max = -2147483648; int min = 2147483647;

public void reduce(IntWritable key,Iterable values,Context context) throws IOException, InterruptedException{ for(IntWritable val:values){ if(val.get()>max){ max = val.get(); }

if(val.get()

count++;

sum+=val.get(); }

int average = (int)sum/count; //计算平均数

//System.out.println(sum+\

context.write(new Text(\平均数\ context.write(new Text(\最大值\ context.write(new Text(\最小值\ } }

public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

// TODO Auto-generated method stub Configuration conf = new Configuration();

//conf.set(\ conf.addResource(\ args = new String[]{\p/output/average1_out\ //检查运行命令

String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs(); if(otherArgs.length != 2){

System.err.println(\ System.exit(2); }

//配置作业名

Job job = new Job(conf,\ //配置作业各个类

job.setJarByClass(Average1.class); job.setMapperClass(Map.class); job.setReducerClass(Reduce.class); //Mapper的输出类型

*强调内容* job.setOutputKeyClass(IntWritable.class); job.setOutputValueClass(IntWritable.class);

FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1);

} }

3.平均成绩

问题描述

给定三个输入文件,每个文件中分别写有多个学生的数学 英语 语文成绩,求每个学生三科的平均成绩。

解决方案

这个问题同样很简单,在map中解析数据并以学生名字作为key,成绩作为value输出。

测试数据

输入:

in1.txt

张三 80 李四 83 王五 91 赵六 88

in2.txt

张三 92 李四 100 王五 94 赵六 88

in3.txt

张三 89 李四 98 王五 84 赵六 93

预期结果

张三 87 李四 93 王五 89 赵六 89

看图说话

代码

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Hadoop那些事儿(四) - MapReduce编程实例(基础)(2)在线全文阅读。

Hadoop那些事儿(四) - MapReduce编程实例(基础)(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/419241.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: