吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1453|回复: 0
收起左侧

[学习记录] 【笔记】 Hadoop学习笔记MapReduce数学建模流量统计

[复制链接]
~望叔♂ 发表于 2020-12-9 16:29
数据原表:
image.png
##数据源来自全国数学建模竞赛专科组E 赛题
需求:统计各个水表用量
需求分析:Map:k:水表名,v:用量,Reduce:对v求和。
编写代码:
public class WcMapper extends Mapper<LongWritable,Text,Text, DoubleWritable> {
   
private  Text k=new Text();
    private DoubleWritable v =new DoubleWritable();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//       String lin=value.toString();//       指定编码格式,不然会乱码
        String lin = new String(value.getBytes(),0,value.getLength(),"GBK");
        String[] words= lin.split(",");
         String name=words[0];
         Double flow= Double.parseDouble(words[words.length-1]);
            k.set(name);
            v.set(flow);
            context.write(k,v);
       }
    }


这里直接copy Wordcount的reduce
public class WcReducer extends Reducer<Text, DoubleWritable,Text,DoubleWritable> {
    double sum ;
    DoubleWritable v=new DoubleWritable();
    @Override
    protected void reduce(Text key, Iterable<DoubleWritable> values, Context context) throws IOException, InterruptedException {
        sum =0;
        for (DoubleWritable count:values)
        {
            sum +=count.get();
        }
        v.set(sum);
        context.write(key,v);
    }
}

public class WcDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        // 1 获取配置信息以及封装任务
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);
        // 2 设置jar加载路径
        job.setJarByClass(WcDriver.class);
        // 3 设置mapreduce
        job.setMapperClass(WcMapper.class);
        job.setReducerClass(WcReducer.class);
        // 4 设置map输出
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(DoubleWritable.class);
        // 5 设置最终输出kv类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(DoubleWritable.class);
        // 6 设置输入和输出路径
        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        // 7 提交
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);

    }
}

打包上传Hadoop集群运行  
image.png

查看统计结果:

image.png
成功
这个代码是对wordcound的修改实现,第一次尝试自己编写MapReduce模型。

免费评分

参与人数 1吾爱币 +1 收起 理由
美丽的渗透者 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-12 11:20

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表