Hadoop序列化与反序列化具体实践

首先创建两个类

两个类的代码

Student类:

java 复制代码
import org.apache.hadoop.io.Writable;

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

public class Student implements Writable {
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Student() {
    }

    public String name;
    public int age;

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(name);
        dataOutput.writeInt(age);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        name = dataInput.readUTF();
        age = dataInput.readInt();
    }
}

TestStudent类:

java 复制代码
import java.io.*;

public class TestStudent {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Student student = new Student("小花", 18);

        // hadoop序列化
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("Student_hadoop.txt"));
        student.write(dos);

        // hadoop 反序列化
        DataInputStream dis = new DataInputStream(new FileInputStream("Student_hadoop.txt"));
        Student student1 = new Student();
        student1.readFields(dis);
        System.out.println(student1.name+ " "+student1.age);
    }
}
相关推荐
开源能源管理系统2 分钟前
MyEMS开源能源管理系统:赋能平板玻璃行业绿色低碳生产
大数据·开源·能源·能源管理系统·平板玻璃
萤丰信息3 分钟前
科技赋能智慧园区:解码绿色转型的“数字密码”
java·大数据·人工智能·科技·安全·智慧城市·智慧园区
培培说证4 分钟前
大专大数据技术专业零基础能考的证书有哪些?
大数据
sunnyday042637 分钟前
深入理解分布式锁:基于Redisson的多样化锁实现
spring boot·redis·分布式
鸿乃江边鸟40 分钟前
Spark native向量化组件 datafusion comet
大数据·spark·native·向量化
LINGYI0003 小时前
什么是品牌全渠道电商代运营公司?
大数据·电商代运营
俊哥大数据9 小时前
【项目10】基于Flink房地产领域大数据实时分析系统
大数据·flink
sensen_kiss9 小时前
INT303 Big Data Analysis 大数据分析 Pt.12 推荐系统(Recommendation Systems)
大数据·数据挖掘·数据分析
txinyu的博客9 小时前
解析业务层的key冲突问题
开发语言·c++·分布式
数字化转型20259 小时前
企业数字化架构集成能力建设
大数据·程序人生·机器学习