MapReduce序列化实例代码

news/2024/5/20 2:31:55 标签: 大数据, hadoop, intellij-idea, hdfs
1 )需求:统计每个学号该月的超市消费、食堂消费、总消费
2 )输入数据格式
序号 学号 超市消费 食堂消费
18 202200153105 8.78
12
3 )期望输出格式
key (学号) value bean 对象)
202200153105 8.78 12 20.78
代码一定要自己打,并且知道每一步的含义,在写代码时我也遇到了各种问题,比如不知道Bean对象怎么写,怎么实现序列化反序列化,以及对应的包导错导致一直运行不出来,前一段时间一直在准备六级考试,导致很多课程都落下好多,接下来有时间继续更新
1. 序列化概述
1 )什么是序列化
序列化就是把内存中的对象,转换成字节序列(或其他数据传输协议)以便于存储到磁盘(持久化)和网络传输。
反序列化就是将收到字节序列(或其他数据传输协议)或者是磁盘的持久化数据,转换成内存中的对象。
2 )为什么要序列化
对象只生存在内存里,关机断电就没有了。而且“活的”对象只能由本地的进程使用,不能被发送到网络上的另外一台计算机。 然而序列化可以存储“活的”对象,可以将“活的”对象发送到远程计算机。
3 )为什么不用 Java 的序列化
Java 的序列化是一个重量级序列化框架( Serializable ),一个对象被序列化后,会附带很多额外的信息(各种校验信息,Header ,继承体系等),不便于在网络中高效传输。所以,Hadoop 自己开发了 一套序列化机制(Writable)
4Hadoop序列化特点
1 )紧凑 :高效使用存储空间。
2 )快速:读写数据的额外开销小。
3 )互操作:支持多语言的交互
自定义序列化
实际开发过程中,基本序列化类型不能满足所有需求,比如在 Hadoop 框架内部 传递一个bean 对象,那么该对象就需要实现序列化接口。
package com.nefu.zhangna.maxcount;

import org.apache.hadoop.io.Writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class StudentBean implements Writable {
    private double foodFee;
    private double maketFee;
    private double totalFee;
    public StudentBean(){  //反序列化必须调用空参构造函数
    }

    public double getFoodFee() {
        return foodFee;
    }

    public void setFoodFee(double foodFee) {
        this.foodFee = foodFee;
    }

    public double getMaketFee() {
        return maketFee;
    }

    public void setMaketFee(double maketFee) {
        this.maketFee = maketFee;
    }

    public double getTotalfee() {
        return totalFee;
    }

    public void setTotalfee(double totalfee) {
        this.totalFee = totalfee;
    }
    public void setTotalFee(){
        this.totalFee=this.foodFee+this.maketFee;
    }
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeDouble(foodFee);
        out.writeDouble(maketFee);
        out.writeDouble(totalFee);
    }
    @Override
    public void readFields(DataInput in) throws IOException {
        this.foodFee=in.readDouble();
        this.maketFee=in.readDouble();
        this.totalFee=in.readDouble();
    }
    @Override
    public String toString(){
        return this.foodFee+"\t"+this.maketFee+"\t"+this.totalFee;
    }
}
Map阶段
1 )读取一行数据,切分字段
2 )抽取超市消费、食堂消费
3 )以学号为 key bean 对象为 value 输出, context.write( 学号, bean)
4 bean 对象能够传输的前提是实现序列化接口 Writable
package com.nefu.zhangna.maxcount;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class ZnMapper extends Mapper<LongWritable, Text,Text,StudentBean> {
    private Text outk=new Text();
    private StudentBean outv=new StudentBean();
    @Override
    protected void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException {
        String line=value.toString();
        String[] content=line.split("\t");
        String schoolnumber=content[1];
        String foodFee=content[2];
        String marketFee=content[3];
        outk.set(schoolnumber);
        outv.setFoodFee(Double.parseDouble(foodFee));
        outv.setMaketFee(Double.parseDouble(marketFee));
        outv.setTotalFee();
        context.write(outk,outv);
    }
}
Reduce
1 )累加每个 key (学号)对应的 foodfee marketfee totalfee
package com.nefu.zhangna.maxcount;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class ZnReducer extends Reducer<Text,StudentBean, Text,StudentBean> {
    private StudentBean outv=new StudentBean();
    @Override
    protected void reduce(Text key, Iterable<StudentBean> values, Context context) throws IOException, InterruptedException {
        double food=0;
        double market=0;
        for (StudentBean value:values){
            food=food+value.getFoodFee();
            market=market+value.getMaketFee();
        }
        outv.setFoodFee(food);
        outv.setMaketFee(market);
        outv.setTotalFee();
        context.write(key,outv);
    }
}
package com.nefu.zhangna.maxcount;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class ZnDriver  {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
         Configuration configuration=new Configuration();
         Job job=Job.getInstance(configuration);
         job.setJarByClass(ZnDriver.class);
         job.setMapperClass(ZnMapper.class);
         job.setReducerClass(ZnReducer.class);
         job.setMapOutputKeyClass(Text.class);
         job.setMapOutputValueClass(StudentBean.class);
         FileInputFormat.setInputPaths(job,new Path("D:\\mydata.txt"));
         FileOutputFormat.setOutputPath(job,new Path("D:\\cluster\\studentbean"));
         boolean result=job.waitForCompletion(true);
         System.exit(result?0:1);
    }
}

原始数据

经过map-reduce之后


http://www.niftyadmin.cn/n/5270253.html

相关文章

locust 压测 websocket

* 安装 python 3.8 https://www.python.org/ py --version * 安装 locust pip install locust2.5.1 -i http://pypi.douban.com/simple/ pip install locust2.5.1 -i https://pypi.mirrors.ustc.edu.cn/simple/ locust -V 备注&#xff1a;-i 是切换下载源 * 安装依赖 pip ins…

WebLangChain_ChatGLM:结合 WebLangChain 和 ChatGLM3 的中文 RAG 系统

WebLangChain_ChatGLM 介绍 本文将详细介绍基于网络检索信息的检索增强生成系统&#xff0c;即 WebLangChain。通过整合 LangChain&#xff0c;成功将大型语言模型与最受欢迎的外部知识库之一——互联网紧密结合。鉴于中文社区中大型语言模型的蓬勃发展&#xff0c;有许多可供利…

新建vue3项目

三种方法 一. 第一种方式 1、操作步骤&#xff1a; 创建项目目录 vue create 项目名称选择配置方式 ? Please pick a preset: #选择一个配置 Default &#xff08;[Vue 3] babel, eslint&#xff09;Default &#xff08;[Vue 2] babel, eslint&#xff09;Manually select …

一篇文章了解Flutter Json系列化和反序列化

目录 一. 使用dart:convert实现JSON格式编解码1. 生成数据模型类2. 将JSON数据转化成数据模型类3. 数据模型类转化成JSON字符串 二、借助json_serializable实现Json编解码1.添加json_annotation、build_runner、json_serializable依赖2. 创建一个数据模型类3. 使用命令行生成JS…

Android Studio好用的插件推荐

目录 一、插件推荐 二、如何下载 1.点击File—>Settings ​2.点击Plugins然后进行搜索下载 三、Android Studio 模板 一、插件推荐 这个插件可以为您自动生成Parcelable代码。Parcelable是一种用于在Android组件之间传递自定义对象的机制&#xff0c;但手动编写Parcela…

CSS新手入门笔记整理:CSS定位布局

定位布局概述 浮动布局比较灵活&#xff0c;但是不容易控制。而定位布局的出现&#xff0c;使得用户精准定位页面中的任意元素成为可能。当然了&#xff0c;由于定位布局缺乏灵活性&#xff0c;这给空间大小和位置不确定的版面布局带来困惑。因此在实际开发中&#xff0c;大家…

USB简介系列-04

系列文章目录 USB简介之四 文章目录 系列文章目录USB协议部分一、控制一个USB设备二、配置、接口和端点三、SETUP数据包总结USB协议部分 本部分讨论USB的协议部分。 一、控制一个USB设备 在详细介绍之前,我们需要先了解一下主机在插入设备时是如何识别和安装设备的。我们需…

CCNP课程实验-OSPF-CFG

目录 实验条件网络拓朴需求 配置实现基础配置1. 配置所有设备的IP地址 实现目标1. 要求按照下列标准配置一个OSPF网络。 路由协议采用OSPF&#xff0c;进程ID为89 &#xff0c;RID为loopback0地址。3. R4/R5/R6相连的三个站点链路OSPF网络类型配置成广播型&#xff0c;其中R5路…