Hazelcast - 序列化


Hazelcast 非常适合数据/查询跨机器分布的环境。这需要将数据从 Java 对象序列化为可以通过网络传输的字节数组。

Hazelcast 支持各种类型的序列化。不过,让我们看一下一些常用的,即Java Serialization 和JavaExternalized。

Java序列化

例子

首先让我们看一下Java序列化。比方说,我们定义了一个 Employee 类并实现了 Serialized 接口。

public class Employee implements Serializable{
   private static final long serialVersionUID = 1L;
   private String name;
   private String department;
   public Employee(String name, String department) {
      super();
      this.name = name;
      this.department = department;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getDepartment() {
      return department;
   }
   public void setDepartment(String department) {
      this.department = department;
   }
   @Override
   public String toString() {
      return "Employee [name=" + name + ", department=" + department + "]";
   }
}

现在让我们编写代码以将 Employee 对象添加到 Hazelcast 映射中。

public class EmployeeExample {
   public static void main(String... args){
      //initialize hazelcast server/instance
      HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();
      //create a set to track employees
      Map<Employee, String> employeeOwners=hazelcast.getMap("employeeVehicleMap");
      Employee emp1 = new Employee("John Smith", "Computer Science");
      // add employee to set
      System.out.println("Serializing key-value and add to map");
      employeeOwners.put(emp1, "Honda");
      // check if emp1 is present in the set
      System.out.println("Serializing key for searching and Deserializing
      value got out of map");
      System.out.println(employeeOwners.get(emp1));
      // perform a graceful shutdown
      hazelcast.shutdown();
   }
}

输出

它将产生以下输出 -

Serializing key-value and add to map
Serializing key for searching and Deserializing value got out of map
Honda

这里一个非常重要的方面是,只需实现 Serialized 接口,我们就可以让 Hazelcast 使用 Java 序列化。另请注意,Hazelcast 存储键和值的序列化数据,而不是像 HashMap 那样将其存储在内存中。因此,Hazelcast 承担了序列化和反序列化的繁重工作。

例子

然而,这里有一个陷阱。在上述情况下,如果员工的部门发生变化怎么办?人还是一样。

public class EmployeeExampleFailing {
   public static void main(String... args){
      //initialize hazelcast server/instance
      HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();
      //create a set to track employees
      Map<Employee, String> employeeOwners=hazelcast.getMap("employeeVehicleMap");
      Employee emp1 = new Employee("John Smith", "Computer Science");
      // add employee to map
      System.out.println("Serializing key-value and add to map");
      employeeOwners.put(emp1, "Honda");
      Employee empDeptChange = new Employee("John Smith", "Electronics");
      // check if emp1 is present in the set
      System.out.println("Checking if employee with John Smith is present");
      System.out.println(employeeOwners.containsKey(empDeptChange));
      Employee empSameDept = new Employee("John Smith", "Computer Science");
      System.out.println("Checking if employee with John Smith is present");
      System.out.println(employeeOwners.containsKey(empSameDept));
      // perform a graceful shutdown
      hazelcast.shutdown();
   }
}

输出

它将产生以下输出 -

Serializing key-value and add to map
Checking if employee with name John Smith is present
false
Checking if employee with name John Smith is present
true

这是因为 Hazelcast 在比较时不会反序列化键,即 Employee。它直接比较序列化密钥的字节码。因此,所有属性具有相同值的对象将被视为相同。但是,如果这些属性的值发生变化,例如上述场景中的部门,则这两个键将被视为唯一。

Java 外部化

在上面的示例中,如果我们在执行键的序列化/反序列化时不关心部门的值怎么办?Hazelcast 还支持 JavaExternalized,这使我们能够控制用于序列化和反序列化的标签。

例子

让我们相应地修改我们的 Employee 类 -

public class EmplyoeeExternalizable implements Externalizable {
   private static final long serialVersionUID = 1L;
   private String name;
   private String department;
   public EmplyoeeExternalizable(String name, String department) {
      super();
      this.name = name;
      this.department = department;
   }
   @Override
   public void readExternal(ObjectInput in) throws IOException,
   ClassNotFoundException {
      System.out.println("Deserializaing....");
      this.name = in.readUTF();
   }
   @Override
   public void writeExternal(ObjectOutput out) throws IOException {
      System.out.println("Serializing....");
      out.writeUTF(name);
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getDepartment() {
      return department;
   }
   public void setDepartment(String department) {
      this.department = department;
   }
   @Override
   public String toString() {
      return "Employee [name=" + name + ", department=" + department + "]";
   }
}

因此,从代码中可以看到,我们添加了 readExternal/writeExternal 方法,它们负责序列化/反序列化。鉴于我们在序列化/反序列化时对部门不感兴趣,我们排除了 readExternal/writeExternal 方法中的部门。

例子

现在,如果我们执行以下代码 -

public class EmployeeExamplePassing {
   public static void main(String... args){
      //initialize hazelcast server/instance
      HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();
      //create a set to track employees
      Map<EmplyoeeExternalizable, String> employeeOwners=hazelcast.getMap("employeeVehicleMap");
      EmplyoeeExternalizable emp1 = new EmplyoeeExternalizable("John Smith", "Computer Science");
      // add employee to map
      employeeOwners.put(emp1, "Honda");
      EmplyoeeExternalizable empDeptChange = new EmplyoeeExternalizable("John Smith", "Electronics");
      // check if emp1 is present in the set
      System.out.println("Checking if employee with John Smith is present");
      System.out.println(employeeOwners.containsKey(empDeptChange));
      EmplyoeeExternalizable empSameDept = new EmplyoeeExternalizable("John Smith", "Computer Science");
      System.out.println("Checking if employee with John Smith is present");
      System.out.println(employeeOwners.containsKey(empSameDept));
      // perform a graceful shutdown
      hazelcast.shutdown();
   }
}

输出

我们得到的输出是 -

Serializing....
Checking if employee with John Smith is present
Serializing....
true
Checking if employee with John Smith is present
Serializing....
true

如输出所示,使用Externalized接口,我们可以为Hazelcast提供仅员工姓名的序列化数据。

另请注意,Hazelcast 将我们的密钥序列化两次 -

  • 一旦存储密钥时,

  • 第二个用于在地图中搜索给定的键。如前所述,这是因为 Hazelcast 使用序列化字节数组进行键比较。

总的来说,如果我们想更好地控制要序列化的属性以及如何处理它们,那么与 Serialized 相比,使用Externalized 具有更多好处。