首页 > 其他分享 >mapreduce案例二

mapreduce案例二

时间:2022-08-31 20:57:56浏览次数:63  
标签:12 mapreduce LongWritable 案例 job new import class

练习题:求每个平均消费,如果遇到异常数据,money异常,就回填为10
数据:
张三,12月3号,20
张三,12月3号,10
李四,12月3号,12
王五,12月3号,10
王五,12月2号
王五,12月2号,30
王二麻,12月2号,0
王二麻,12月2号,,
王二麻,12月2号, ,

 

代码

package com.shujia.avgmoney;

import org.apache.hadoop.conf.Configuration;
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 java.io.IOException;

/*
李四,12月3号,12
王五,12月3号,10
王五,12月2号
王五,12月2号,30
王二麻,12月2号,0
王二麻,12月2号,,
王二麻,12月2号, ,
 */
class AvgMapper extends Mapper<LongWritable,Text,Text,LongWritable>{
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String s = line.replaceAll("[,|,,|, ,]", "\t");
        String[] split = s.split("\t");
        if(split.length!=3) {
            String[] strings = new String[3];
            strings[0]=split[0];
            strings[1]=split[1];
            strings[2]="10";
            Text name = new Text(strings[0]);
            LongWritable res = new LongWritable(new Long(strings[2]));
            context.write(name,res);
        }else{
            Text name = new Text(split[0]);
            LongWritable res = new LongWritable(new Long(split[2]));
            context.write(name,res);
        }
    }
}

class AvgReducer extends Reducer<Text,LongWritable,Text,LongWritable> {
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        long sum=0;
        long count=0;
        for (LongWritable value : values) {
            long l = value.get();
            sum+=l;
            count++;
        }
        long avg=sum / count;
        context.write(key,new LongWritable(avg));
    }
}

public class AvgDemo {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJarByClass(AvgDemo.class);
        job.setMapperClass(AvgMapper.class);
        job.setReducerClass(AvgReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        job.waitForCompletion(true);
    }
}

注意的点:需要数据清洗,替换

结果:

 

标签:12,mapreduce,LongWritable,案例,job,new,import,class
From: https://www.cnblogs.com/wqy1027/p/16644466.html

相关文章

  • linux中awk的小案例使用
    1.排版整齐的输出第一列与最后一列awk'{printf"%-15s%s\n",$1,$NF}'data2.txt   2.对于数据的每个字段之间进行控制使用print函数打印,如果......
  • Hadoop-MapReducer学习
    一、InputFormat1.默认的是TextInputFormat,key偏移量,value一行内容。2.处理小文件CombineTextInputFormat把多个文件合并到一起统一切片。二、Mappersetup()初始化;map(......
  • 带有详细答案的 SQL 案例问题(分步)
    带有详细答案的SQL案例问题(分步)我们分析了超过50,000次从上市前到财富500强公司的采访大科技采访(BTI)策划最新SQL面试问题和解决方案的独家列表,以便您在下一......
  • 7个自定义定时任务并发送消息至邮箱或企业微信案例(crontab和at)
    前言更好熟悉掌握at、crontab定时自定义任务用法。实验at、crontab定时自定义任务运用场景案例。作业、笔记需要。定时计划任务相关命令及配置文件简要说明at......
  • 《低代码发展白皮书(2022年)》&《2022低代码·无代码应用案例汇编》,发布了
    摘要:信息通信研究院云计算与大数据研究所副所长栗蔚发布了《低代码发展白皮书(2022年)》&《2022低代码·无代码应用案例汇编》。本文分享自华为云社区《华为参编,中国信通院......
  • 泛型方法案例
    public<T>List<T>getGAPListByDate(StringwebDataSource,StringwebPlatType,DatestartDt,DateendDt,StringtableName,Class<T>cls){SimpleDateFormats......
  • springframework---基础Ioc入门案例
    1.整体的结构  2.学习的方向  3.控制反转(本身由自身去new对象,现在由外部来提供对象)  4.具体做法:首先在我们的pom.xml中导入包  然后刷新我们导入......
  • Modbus485转Profinet网关连接水表模块配置案例
     本案例控制的是Modbus485水表模块。系统主PLC选用西门子CPU,通过Modbus转Profinet网关采集水表模块通讯数据。 1、首先创建新项目文件; 2、导入GSD文件。将小疆......
  • halo博客实现链路追踪案例-skywalking
    es10.0.0.17skywalking10.0.0.2halo10.0.0.14先部署一个halo博客安装jdkyuminstalljava-11-openjdk-y下载halo博客jar包和skywalking-java-agent包wgethttp......
  • 注解-解析注解以及注解案例
    解析注解在程序中使用(解析)注解:获取注解中定义的属性值​1.获取注解定义的位置的对象(Class,Method,Field)​2.获取指定的注解:getAnnotation(Class)//其实就......