当前位置: 首页 > web >正文

序列化和反序列化(hadoop)

1.先将上一个博客的Student复制粘贴后面加上H

在StudentH中敲下面代码

package com.example.sei;
import org.apache.hadoop.io.Writable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
//学生类,姓名,年龄
//支持hadoop序列化
//1.要实现Writable接口
//2.补充一个空参构造
public class StudentH implements Writable {
   String name;
   int age;

   //空参构造
   public StudentH() {}
    public StudentH(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    //write:在序列化的时候,调用。把对象写到文件
    @Override
    public void write(DataOutput dataOutput) throws IOException {
        //dataOutput.write(字段);
        dataOutput.writeUTF(name);
        dataOutput.writeInt(age);
    }
 
    //readFields:在反序列化的时候,调用。从文件中读取数据,还原这个对象
    //字段的顺序要与write中的顺序一致
    @Override
    public void readFields(DataInput dataInput) throws IOException {
        //字段=dataInput.read();
        name=dataInput.readUTF();
        age=dataInput.readInt();
    }
}

2.然后将TestStudent复制粘贴后面加上H

在TestStudentH中敲下面代码

package com.example.sei;
 
import java.io.*;
 
public class TestStudentH {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        StudentH student = new StudentH("John", 20);
        System.out.println(student.name + "  " + student.age );
 
        //hadoop序列化:把对象保存到一个文件中
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("student_hadoop.ser"));
      student.write(dos);
 
    }
}
这个是序列化

package com.example.sei;
 
import java.io.*;
 
public class TestStudentH {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
//        StudentH student = new StudentH("John", 20);
//        System.out.println(student.name + "  " + student.age );
//
//        //hadoop序列化:把对象保存到一个文件中
//        DataOutputStream dos = new DataOutputStream(new FileOutputStream("student_hadoop.ser"));
//        student.write(dos);
        //hadoop反序列化:从文件中读取对象
        DataInputStream dis = new DataInputStream(new FileInputStream("student_hadoop.ser"));
        StudentH student1 = new StudentH();
        student1.readFields(dis);
        System.out.println(student1.name + "  " + student1.age );
    }
}

 这个是反序列化。(()

http://www.xdnf.cn/news/5849.html

相关文章:

  • RabbitMQ发布订阅模式深度解析与实践指南
  • 解决 CentOS 7 镜像源无法访问的问题
  • 爬虫请求频率应控制在多少合适?
  • cocos creator 3.8 下的 2D 改动
  • Kubernetes Horizontal Pod Autosscaler(HPA)核心机制解析
  • 【android bluetooth 框架分析 02】【Module详解 6】【StorageModule 模块介绍】
  • C#进阶(1) ArrayList
  • TDengine编译成功后的bin目录下的文件的作用
  • 【计算机组成原理】第二部分 存储器--分类、层次结构
  • Altium Designer AD如何输出PIN带网络名的PDF装配图
  • 智能意图识别 + 内容定位,contextgem重构文档处理逻辑
  • ExoPlayer 如何实现音画同步
  • 记录为什么LIst数组“增删慢“,LinkedList链表“查改快“?
  • 信息学奥赛一本通 1535:【例 1】数列操作
  • 新一代动态可重构处理器技术,用于加速嵌入式 AI 应用
  • WSL 安装 Debian 12 后,Linux 如何安装 vim ?
  • OpenVLA (2) 机器人环境和环境数据
  • 【UAP】《Empirical Upper Bound in Object Detection and More》
  • 【HTML5】【AJAX的几种封装方法详解】
  • 【deekseek】TCP Offload Engine
  • LeetCode 648 单词替换题解
  • Baklib智能云平台加速企业数据治理
  • 桑德拉精神与开源链动2+1模式AI智能名片S2B2C商城小程序的协同价值研究
  • 01.类型转换+Scanner+制表符嫦娥例题
  • dockers笔记
  • FastDDS Transport功能模块初步整理
  • 《医院网络安全运营能力成熟度评估指南》(试行版)研究解读
  • Spring Boot 的自动配置为 Spring MVC 做了哪些事情?
  • matlab多智能体网络一致性研究
  • 【C++详解】类和对象(上)类的定义、实例化、this指针