4.MapReduce 序列化

目录

  • 概述
  • 序列化
    • 序列化
    • 反序例化
    • java自带的两种
      • Serializable
      • 非Serializable
    • hadoop序例化
      • 实践
    • 分片/InputFormat & InputSplit
      • 日志
  • 结束

概述

序列化是分布式计算中很重要的一环境,好的序列化方式,可以大大减少分布式计算中,网络传输的数据量。

序列化

序列化

对象 --> 字节序例 :存储到磁盘或者网络传输
MR 、Spark、Flink :分布式的执行框架 必然会涉及到网络传输

java 中的序列化:Serializable
Hadoop 中序列化特点: 紧凑、速度、扩展性、互操作
Spark 中使用了其它的序例化框架 Kyro

反序例化

字节序例 —> 对象

java自带的两种

Serializable

此处是 java 自带的 序例化 方式,这种方式简单方便,但体积大,不利于大数据量网络传输。

public class JavaSerDemo {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Person person = new Person(1, "张三", 33);
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("download/person.obj"));
        out.writeObject(person);

        ObjectInputStream in = new ObjectInputStream(new FileInputStream("download/person.obj"));
        Object o = in.readObject();
        System.out.println(o);
    }


    static class Person implements Serializable {
        private int id;
        private String name;
        private int age;

        public Person(int id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
}

非Serializable

public class DataSerDemo {

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

        Person person = new Person(1, "张三", 33);
        DataOutputStream out = new DataOutputStream(new FileOutputStream("download/person2.obj"));
        out.writeInt(person.getId());
        out.writeUTF(person.getName());
        out.close();

        DataInputStream in = new DataInputStream(new FileInputStream("download/person2.obj"));
        // 这里要注意,上面以什么顺序写出去,这里就要以什么顺序读取
        int id = in.readInt();
        String name = in.readUTF();
        in.close();
        System.out.println("id:" + id + " name:" + name);

    }

    /**
     *  注意: 不需要继承 Serializable
     */
    static class Person {
        private int id;
        private String name;
        private int age;

        public Person(int id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
}

hadoop序例化

官方地址速递

The key and value classes have to be serializable by the framework and hence need to implement the Writable interface. Additionally, the key classes have to implement the WritableComparable interface to facilitate sorting by the framework.
在这里插入图片描述
注意:Writable 两个方法,一个 write ,readFields

@InterfaceAudience.Public
@InterfaceStability.Stable
public interface Writable {

  void write(DataOutput out) throws IOException;

  void readFields(DataInput in) throws IOException;
}

实践

public class PersonWritable implements Writable {

    private int id;
    private String name;
    private int age;
    // 消费金额
    private int consumption;
    // 消费总金额
    private long consumptions;


    public PersonWritable() {
    }

    public PersonWritable(int id, String name, int age, int consumption) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.consumption = consumption;
    }

    public PersonWritable(int id, String name, int age, int consumption, long consumptions) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.consumption = consumption;
        this.consumptions = consumptions;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getConsumption() {
        return consumption;
    }

    public void setConsumption(int consumption) {
        this.consumption = consumption;
    }

    public long getConsumptions() {
        return consumptions;
    }

    public void setConsumptions(long consumptions) {
        this.consumptions = consumptions;
    }

    @Override
    public String toString() {
        return
                "id=" + id +
                        ", name='" + name + '\'' +
                        ", age='" + age + '\'' +
                        ", consumption=" + consumption + '\'' +
                        ", consumptions=" + consumptions;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(id);
        out.writeUTF(name);
        out.writeInt(age);
        out.writeInt(consumption);
        out.writeLong(consumptions);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        id = in.readInt();
        name = in.readUTF();
        age = in.readInt();
        consumption = in.readInt();
        consumptions = in.readLong();
    }
}
/**
 * 统计 个人 消费
 */
public class PersonStatistics {

    static class PersonStatisticsMapper extends Mapper<LongWritable, Text, IntWritable, PersonWritable> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] split = value.toString().split(",");
            int id = Integer.parseInt(split[0]);
            String name = split[1];
            int age = Integer.parseInt(split[2]);
            int consumption = Integer.parseInt(split[3]);
            PersonWritable writable = new PersonWritable(id, name, age, consumption, 0);
            context.write(new IntWritable(id), writable);
        }
    }

    static class PersonStatisticsReducer extends Reducer<IntWritable, PersonWritable, NullWritable, PersonWritable> {
        @Override
        protected void reduce(IntWritable key, Iterable<PersonWritable> values, Context context) throws IOException, InterruptedException {
            long count = 0L;
            PersonWritable person = null;
            for (PersonWritable data : values) {
                if (Objects.isNull(person)) {
                    person = data;
                }
                count = count + data.getConsumption();
            }
            person.setConsumptions(count);

            PersonWritable personWritable = new PersonWritable(person.getId(), person.getName(), person.getAge(), person.getConsumption(), count);

            context.write(NullWritable.get(), personWritable);
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration configuration = new Configuration();

        String sourcePath = "data/person.data";
        String distPath = "downloadOut/person-out.data";

        FileUtil.deleteIfExist(configuration, distPath);

        Job job = Job.getInstance(configuration, "person statistics");
        job.setJarByClass(PersonStatistics.class);
        //job.setCombinerClass(PersonStatistics.PersonStatisticsReducer.class);
        job.setMapperClass(PersonStatisticsMapper.class);
        job.setReducerClass(PersonStatisticsReducer.class);
        job.setMapOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(PersonWritable.class);
        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(PersonWritable.class);

        FileInputFormat.addInputPath(job, new Path(sourcePath));
        FileOutputFormat.setOutputPath(job, new Path(distPath));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
# person.data
1,张三,30,10
1,张三,30,20
2,李四,25,5

上述执行结果如下:
在这里插入图片描述

分片/InputFormat & InputSplit

官方文档速递

org.apache.hadoop.mapreduce.InputFormat
org.apache.hadoop.mapreduce.InputSplit

日志

执行 序列化 测试小程序,关注以下日志

# 总共加载一个文件,分隔成一个
2024-01-06 09:19:42,363 [main] [org.apache.hadoop.mapreduce.lib.input.FileInputFormat] [INFO] - Total input files to process : 1
2024-01-06 09:19:42,487 [main] [org.apache.hadoop.mapreduce.JobSubmitter] [INFO] - number of splits:1

结束

至此,MapReduce 序列化 至此结束,如有疑问,欢迎评论区留言。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/307368.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【VRTK】【VR开发】【Unity】19-VRTK实现旋转运动

课程配套学习项目源码资源下载 https://download.csdn.net/download/weixin_41697242/88485426?spm=1001.2014.3001.5503 【背景】 在实际开发中,旋转运动也是时常需要模拟的重要运动类型。常见的场景有开关门,方向盘轮胎以及拉动拉杆等等。 旋转运动的实现可以基于物理系…

服务器日常维护要素,应该如何做好维护

维护服务器的目的是为了让服务器的性能保持最佳状态&#xff0c;发现问题及时解决&#xff0c;没有问题也可以对相关的应用和配置进行调优。但也有很多用户疑问&#xff0c;服务器具体会有哪些方面需要维护的&#xff0c;今天就一起来看看吧。 服务器日常维护&#xff0c;主要包…

面向对象编程的五大原则,你了解吗?

面向对象编程的五大原则&#xff0c;你了解吗&#xff1f; 一、面向对象编程的概念 面向对象编程&#xff0c;是一种程序设计范式&#xff0c;也是一种编程语言的分类。它以对象作为程序的基本单元&#xff0c;将算法和数据封装其中&#xff0c;程序可以访问和修改对象关联的…

科研绘图(一)山脊图

从今日开始&#xff0c;为大家开辟一个新的系列科研绘图。同一个竞赛下&#xff0c;大家都近乎相同的解题思路下。之所以能出现一等二等三等奖的区别很大部分都在于结果的可视化&#xff0c;为了能更好地帮助大家进行可视化&#xff0c;今后将专门推出一个可视化板块&#xff0…

B端产品经理学习-权限管理

目录 权限管理的原则 常见的权限管理模型 总结 对企业而言&#xff0c;最重要的一个资源就是有价值的专有信息&#xff0c;也就是自己知道&#xff0c;而其他企业不知道的信息&#xff0c;因此&#xff0c;专有信息是企业的重要竞争力&#xff0c;权限管理的价值体现在专有信…

centos下升级git版本

1 问题描述 centos7系统默认的git安装版本是1.8&#xff0c;但是在项目构建中发现git版本过低&#xff0c;导致构建AI模型环境时出现各种错误&#xff0c;于是用源码编译的方式进行升级&#xff0c;同时该文章也适用于安装新的git。 2. 升级安装 2.1 第一步卸载原有的git r…

HarmonyOS应用开发学习笔记 UI布局学习 相对布局 (RelativeContainer)

UI布局学习 之 相对布局 &#xff08;RelativeContainer&#xff09; 官方文档 一、关键字 RelativeContainer&#xff0c; alignRules&#xff08;适配规则&#xff09; Text(Text02).alignRules({left: { anchor: text01, align: HorizontalAlign.Start },top: { anchor: t…

精通推荐算法4:经典DNN框架特征交叉模型 Deep Crossing(面试必备)

微软2016年提出的Deep Crossing模型奠定了深度学习精排模型的基本架构&#xff0c;具有十分重要的意义。它采用“Embedding MLP”的结构&#xff0c;成为目前推荐算法的基本范式。通过深度神经网络&#xff0c;实现大规模特征自动组合&#xff0c;大大减少了对人工构造交叉组合…

(一)Spring Cloud 直击微服务作用、架构应用、hystrix降级

直击微服务作用 微服务架构: 遇到了什么问题? 将单体架构拆分成微服务架构后,如果保证多个服务(项目)正常运行? 哪个技术可以解决这个问题? 微服务技术 服务治理: 服务管理,维护服务与服务之间的关系 这个技术如何使用? netflix/网…

智能化输电线路定位技术:提升电网运行效率的未来发展方向

随着科技的不断发展&#xff0c;电力行业也在逐步引入智能化技术&#xff0c;以提高输电线路的运行效率和安全性。在这篇文章中&#xff0c;恒峰智慧科技将探讨一种新的输电线路定位技术——分布式行波测量技术&#xff0c;它如何帮助我们实现这一目标。 一、分布式故障定位及隐…

游泳耳机排行榜前四名,分享几款值得推荐的游泳耳机

游泳是一项全面锻炼身体的运动&#xff0c;然而&#xff0c;水的阻力有时让人感到运动的笨拙和单调。为了让游泳更具趣味性和挑战性&#xff0c;选择一款高品质的游泳耳机至关重要。以下是游泳耳机排行榜前四名&#xff0c;以及几款强烈推荐的游泳耳机&#xff0c;它们结合防水…

Python教程(22)——Python面向对象的属性和方法

在Python面向对象编程中&#xff0c;属性和方法是类及其对象的重要组成部分。如果说封装、继承和多态是面向对象编程中的思想&#xff0c;那么类的属性和方法就是骨架&#xff0c;因为有属性和方法&#xff0c;面向对象编程才变的有血有肉。 属性 属性是类或对象所拥有的数据&…

UWB 技术及应用

超宽带技术为工业自动化提供独特优势&#xff0c;是首要的室内定位技术。 UWB 因其相对于 RFID、BLE 或 WiFi 等同类技术的众多优势而被认为是室内定位技术的黄金标准。它是基于位置的自动化的理想解决方案。 UWB 结合了短光速脉冲&#xff0c;可在宽带宽上精确测量信号到达时…

linux离线安装docker20.10.7 版本(亲测有效)

目录 1 下载2 安装2.1 新建docker.service2.2 为什么要写docker.service2.3 启动docker2.5 安装docker-compose2.5.1 为什么安装2.5.2 安装 3 /usr/local/bin/ 和 /usr/bin 区别 1 下载 将这个上传到linux 服务器的 随便一个目录 2 安装 解压离线安装包 tar -zxvf docker-20.1…

面试算法105:最大的岛屿

题目 海洋岛屿地图可以用由0、1组成的二维数组表示&#xff0c;水平或竖直方向相连的一组1表示一个岛屿&#xff0c;请计算最大的岛屿的面积&#xff08;即岛屿中1的数目&#xff09;。例如&#xff0c;在下图中有4个岛屿&#xff0c;其中最大的岛屿的面积为5。 分析 将岛屿…

局域网实现文件自动同步

软件下载地址: https://dbrwe.blog.csdn.net/article/details/132331206?spm1001.2014.3001.5502 打开【自动上传与同步】配置 在下面 自动同步 自动回传打上钩就可以同步或者下载文件

生成式人工智能市场规模、趋势和统计数据(2024-2026)

生成式人工智能市场规模、趋势和统计数据&#xff08;2024-2026&#xff09; 目录 生成式人工智能市场规模、趋势和统计数据&#xff08;2024-2026&#xff09;一、生成式人工智能行业亮点二、生成式人工智能市场规模三、生成式人工智能市场增长预测四、生成式人工智能采用统计…

结构型设计模式——适配器模式

适配器模式 这个更加好理解&#xff0c;就是做适配功能的类&#xff0c;例如&#xff0c;现在手机没有了圆形耳机接口&#xff0c;只有Type-C接口&#xff0c;因此你如果还想要使用圆形耳机的话需要买个圆形接口转Type-C的转换器&#xff08;适配器&#xff09;&#xff0c;这…

再不收藏就晚了,Axure RP Pro 各版本大集合

Axure RP Pro下载链接 https://pan.baidu.com/s/1hRJRY6t0ZONKhdwvykAc3g?pwd0531 1.鼠标右击【Axure RP Pro9.0】压缩包&#xff08;win11及以上系统需先点击“显示更多选项”&#xff09;选择【解压到 Axure RP Pro9.0】。 2.打开解压后的文件夹&#xff0c;鼠标右击【Axu…

基于ssm的一家运动鞋店的产品推广网站的设计论文

摘 要 现代经济快节奏发展以及不断完善升级的信息化技术&#xff0c;让传统数据信息的管理升级为软件存储&#xff0c;归纳&#xff0c;集中处理数据信息的管理方式。本一家运动鞋店就是在这样的大环境下诞生&#xff0c;其可以帮助管理者在短时间内处理完毕庞大的数据信息&am…
最新文章