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;}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic String toString() {return"id=" + id +", name='" + name + '\'' +", age='" + age + '\'' +", consumption=" + consumption + '\'' +", consumptions=" + consumptions;}@Overridepublic void write(DataOutput out) throws IOException {out.writeInt(id);out.writeUTF(name);out.writeInt(age);out.writeInt(consumption);out.writeLong(consumptions);}@Overridepublic 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> {@Overrideprotected 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> {@Overrideprotected 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.hqwc.cn/news/336046.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

深度学习在交通标志识别中的应用

深度学习在交通标志识别中的应用 深度学习在交通标志识别中的应用1. 交通标志识别的背景2. CNN在交通标志识别中的应用3. 数据集准备4. 模型训练与优化5. 模型评估与部署结语 深度学习在交通标志识别中的应用 交通标志是道路上的重要元素&#xff0c;它们提供了关键的信息&…

Pandas使用简介

Pandas相关题目 【Python】—— Pandas 初体验&#xff08;一&#xff09; 【Python】—— Pandas 初体验&#xff08;二&#xff09; 【Python】—— pandas 数据分析 【Python】—— pandas数据处理 Pandas是基于Numpy构建的、开源的Python数据分析工具包&#xff0c;借助高效…

超市商品管理系统设计 C++实现

超市商品管理系统设计—C实现 文章目录 超市商品管理系统设计---C实现一、内容要求大纲图 二、源代码&#xff08;包含大量注释&#xff09;1、main.cpp文件2、supermarket.h文件3、supermarket.cpp文件4、administrator.h文件5、administrator.cpp文件6、user.h文件7、user.cp…

代理API如此强大,每个Web开发人员都应该掌握它!

80%的Web开发者都不知道的代理API的8个主要使用场景&#xff01; Proxy API 非常强大&#xff0c;非常有用。在这篇文章中&#xff0c;我将介绍它的 8 种使用场景。 ​在日常工作中&#xff0c;相信很多开发者都使用过Web调试代理工具&#xff0c;比如Fiddler或者Charles&…

【JAVA】怎么确保一个集合不能被修改

&#x1f34e;个人博客&#xff1a;个人主页 &#x1f3c6;个人专栏&#xff1a; JAVA ⛳️ 功不唐捐&#xff0c;玉汝于成 目录 前言 正文 示例&#xff1a; 不可修改的List&#xff1a; 不可修改的Set&#xff1a; 不可修改的Map&#xff1a; 结语 我的其他博…

2024年全国教资笔试报名流程(建议电脑报名),看看有啥新要求?

一.报名、考试时间节点 1.笔试报名时间: 2024年1月12日-15日 2.笔试考试时间:2024年3月9日 3.笔试成绩查询时间:2024年4月15日 4.面试报名时间:2024年4月15日 5.面试考试时间:2024年5月18日 6.面试成绩查询时间:2024年6月14日 二.笔试报名流程: 登陆→考生注册 →填报个…

送货单打印要用什么打印机和软件

在打印送货单时&#xff0c;打印机和软件的选择都是非常重要的。根据需求&#xff0c;可以选择喷墨打印机、激光打印机或针式打印机等类型&#xff0c;而软件我们可以选择专业的送货单打印软件&#xff0c;例如&#xff1a;方可销售送货单软件&#xff08;推荐&#xff09;就是…

使用串口 DMA 模式接收不定长数据

一、简介 曾经遇到客户有一个需求&#xff0c;需要用串口 DMA 的方式接收不定长度的数据&#xff0c;DMA 有个缺点就是在每次传输前需要设定好传输的字节长度&#xff0c;这种方式显然对于接收不定长度的数据来说没有那么灵活。但 DMA 也有着显著的优点&#xff0c;如可直接访…

Hive基础知识(六):Hive 配置运行日志信息、打印当前库和表头、参数配置方式

1. Hive 运行日志信息配置 1&#xff09;Hive 的 log 默认存放在/tmp/atguigu/hive.log 目录下&#xff08;当前用户名下&#xff09; 2&#xff09;修改 hive 的 log 存放日志到/opt/module/hive/logs &#xff08;1&#xff09;修改/opt/module/hive/conf/hive-log4j2.prop…

Java EE 博客系统(Servlet版)

文章目录 1. 基本情况2. 准备工作3. 博客列表页4. 博客详情页5. 实现登录6. 强制要求登录7. 显示用户信息8. 退出登录9. 发布博客10. 如果程序出现问题怎么办&#xff1f; 1. 基本情况 这里的博客系统主要是四个界面 博客列表页 显示出当前网站上都有哪些博客博客详情页 点击…

【实用技巧】Windows 电脑向iPhone或iPad传输视频方法1:无线传输

一、内容简介 本文介绍如何使用 Windows 电脑向 iPhone 或 iPad 传输视频&#xff0c;以 iPhone 为例&#xff0c;iPad的操作方法类似&#xff0c;本文不作赘述。 二、所需原材料 Windows 电脑&#xff08;桌面或其它文件夹中存有要导入的视频&#xff09;、iPhone 14。 待…

Vue项目nginx部署到线上,访问时加前缀解决方案

一、业务场景&#xff1a; 最近项目开发完了&#xff0c;需要部署一个测试版本和正式版本到线上&#xff0c;测试版本前面需要加一个dev前缀&#xff0c;遇到了一些坑&#xff0c;分享给大家 二、目前效果 三、具体实现步骤&#xff1a; &#xff08;1&#xff09;实现静态文…