Zookeeper1.序列化

简介: 本文从源码角度解析Zookeeper的序列化机制,重点分析`org.zookeeper.jute`包中的核心接口:`InputArchive`、`OutputArchive`、`Index`和`Record`,并通过实例演示其在数据读写中的应用。

1.序列化
一、前言
  在完成了前面的理论学习后,现在可以从源码角度来解析Zookeeper的细节,首先笔者想从序列化入手,因为在网络通信、数据存储中都用到了序列化,下面开始分析。
二、序列化
  序列化主要在zookeeper.jute包中,其中涉及的主要接口如下
    · InputArchive
    · OutputArchive
    · Index
    · Record
2.1 InputArchive
  其是所有反序列化器都需要实现的接口,其方法如下 
InputArchive的类结构如下   

  1. BinaryInputArchive 
  2. CsvInputArchive 
  3. XmlInputArchive
    2.2 OutputArchive
      其是所有序列化器都需要实现此接口,其方法如下。  
    OutputArchive的类结构如下   
  4. BinaryOutputArchive 
  5. CsvOutputArchive 
  6. XmlOutputArchive
    2.3 Index
      其用于迭代反序列化器的迭代器。  
    Index的类结构如下
      
    1.BinaryIndex 
  7. CsxIndex 
    3.XmlIndex
    2.4 Record
      所有用于网络传输或者本地存储的类型都实现该接口,其方法如下 
     所有的实现类都需要实现seriallize和deserialize方法。
    三、示例
      下面通过一个示例来理解OutputArchive和InputArchive的搭配使用。 
    Java
    运行代码
    复制代码
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    package com.leesf.zookeeper_samples;
    public static void main( String[] args ) throws IOException {

     String path = "F:\\test.txt";
     // write operation
     OutputStream outputStream = new FileOutputStream(new File(path));
     BinaryOutputArchive binaryOutputArchive = BinaryOutputArchive.getArchive(outputStream);
    
     binaryOutputArchive.writeBool(true, "boolean");
     byte[] bytes = "leesf".getBytes();
     binaryOutputArchive.writeBuffer(bytes, "buffer");
     binaryOutputArchive.writeDouble(13.14, "double");
     binaryOutputArchive.writeFloat(5.20f, "float");
     binaryOutputArchive.writeInt(520, "int");
     Person person = new Person(25, "leesf");
     binaryOutputArchive.writeRecord(person, "leesf");
     TreeMap<String, Integer> map = new TreeMap<String, Integer>();
     map.put("leesf", 25);
     map.put("dyd", 25);
     Set<String> keys = map.keySet();
     binaryOutputArchive.startMap(map, "map");
     int i = 0;
     for (String key: keys) {
         String tag = i + "";
         binaryOutputArchive.writeString(key, tag);
         binaryOutputArchive.writeInt(map.get(key), tag);
         i++;
     }
    
     binaryOutputArchive.endMap(map, "map");
    
    // read operation
    InputStream inputStream = new FileInputStream(new File(path));
    BinaryInputArchive binaryInputArchive = BinaryInputArchive.getArchive(inputStream);

    System.out.println(binaryInputArchive.readBool("boolean"));
    System.out.println(new String(binaryInputArchive.readBuffer("buffer")));
    System.out.println(binaryInputArchive.readDouble("double"));
    System.out.println(binaryInputArchive.readFloat("float"));
    System.out.println(binaryInputArchive.readInt("int"));
    Person person2 = new Person();
    binaryInputArchive.readRecord(person2, "leesf");
    System.out.println(person2);       

    Index index = binaryInputArchive.startMap("map");
    int j = 0;
    while (!index.done()) {
        String tag = j + "";
        System.out.println("key = " + binaryInputArchive.readString(tag) 
            + ", value = " + binaryInputArchive.readInt(tag));
        index.incr();
        j++;
    }
}

static class Person implements Record {
    private int age;
    private String name;

    public Person() {

    }

    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }
    public void serialize(OutputArchive archive, String tag) throws IOException {
        archive.startRecord(this, tag);
        archive.writeInt(age, "age");
        archive.writeString(name, "name");
        archive.endRecord(this, tag);
    }
    public void deserialize(InputArchive archive, String tag) throws IOException {
        archive.startRecord(tag);
        age = archive.readInt("age");
        name = archive.readString("name");
        archive.endRecord(tag);            
    }    

    public String toString() {
        return "age = " + age + ", name = " + name;
    }
}

}
输出结果
true
leesf
13.14
5.2
520
age = 25, name = leesf
key = dyd, value = 25
key = leesf, value = 25
四、总结
这里只需要知道
序列化涉及的类存放在:org.zookeeper.jute包下
常用的类有:
InputArchive
OutputArchive
Index
Record

相关文章
|
1天前
|
存储 算法 Java
Zookeeper2.持久化FileTxnLog
本文深入分析ZooKeeper持久化核心类FileTxnLog源码,涵盖事务日志结构、文件格式及关键操作。重点解析append、commit、truncate等方法,揭示日志追加、校验、提交与截断机制,结合TxnLog接口与迭代器实现,全面理解ZooKeeper数据持久化原理。(238字)
Zookeeper4.Watcher机制(一)
本文深入分析ZooKeeper的Watcher机制核心类与源码实现,涵盖Watcher接口、Event枚举(KeeperState、EventType)、WatchedEvent事件封装、ClientWatchManager及ZKWatchManager的管理逻辑,重点解析事件触发与Watcher通知机制,帮助理解ZooKeeper分布式协调中的状态监听与回调原理。
|
1天前
|
存储 数据库
Zookeeper 3.持久化FileTxnSnapLog
FileTxnSnapLog是ZooKeeper中用于管理事务日志和快照的核心类,封装TxnLog与SnapShot,实现数据持久化。通过restore和save方法,支持从快照和日志中恢复及保存内存数据库DataTree,保障数据一致性。
|
1天前
|
存储 关系型数据库 调度
微服务原理篇(XXLJOB-幂等-MySQL)
本课程深入讲解微服务核心组件XXL-JOB任务调度原理,涵盖其架构、分布式任务处理、幂等性设计及MySQL存储引擎、索引机制、SQL优化与分库分表策略,全面提升系统性能与可靠性。
|
网络协议 Android开发
|
5天前
|
云安全 监控 安全
|
2天前
|
存储 机器学习/深度学习 人工智能
打破硬件壁垒!煎饺App:强悍AI语音工具,为何是豆包AI手机平替?
直接上干货!3000 字以上长文,细节拉满,把核心功能、使用技巧和实测结论全给大家摆明白,读完你就知道这款 “安卓机通用 AI 语音工具"——煎饺App它为何能打破硬件壁垒?它接下来,咱们就深度拆解煎饺 App—— 先给大家扒清楚它的使用逻辑,附上“操作演示”和“🚀快速上手不踩坑 : 4 条核心操作干货(必看)”,跟着走零基础也能快速上手;后续再用真实实测数据,正面硬刚煎饺 App的语音助手口令效果——创建京东「牛奶自动下单神器」口令 ,从修改口令、识别准确率到场景实用性,逐一测试不掺水,最后,再和豆包 AI 手机语音助手的普通版——豆包App对比测试下,简单地谈谈煎饺App的能力边界在哪?
|
10天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1175 7
|
1天前
|
人工智能
自动化读取内容,不会写爆款的普通人也能产出好内容,附coze工作流
陌晨分享AI内容二创工作流,通过采集爆款文案、清洗文本、智能改写,实现高效批量生产。五步完成从选题到输出,助力内容创作者提升效率,适合多场景应用。
205 104