English 中文(简体)
Hazelcast - Serialization
  • 时间:2024-09-17

Hazelcast - Seriapzation


Previous Page Next Page  

理想的做法是,在数据/频率在机器之间分配的环境中使用黑色广播。 这就需要将数据从我们的 Java物体上编成可以转至网络的星体。

Hazelcast支持各种类型的航空化。 然而,请看一些常用的系统,即Java Seriapzation和Java对外适用。

Java Seriapzation

Example

首先,请看一下Java Seriapzation。 让我们说,我们界定了采用可扩展接口的员工班。


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

我现在要写成法典,在哈兹尔地图上添加“雇员”的物体。


pubpc class EmployeeExample {
   pubpc static void main(String... args){
      //initiapze 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("Seriapzing key-value and add to map");
      employeeOwners.put(emp1, "Honda");
      // check if emp1 is present in the set
      System.out.println("Seriapzing key for searching and Deseriapzing
      value got out of map");
      System.out.println(employeeOwners.get(emp1));
      // perform a graceful shutdown
      hazelcast.shutdown();
   }
}

Output

它将产生以下产出:


Seriapzing key-value and add to map
Seriapzing key for searching and Deseriapzing value got out of map
Honda

这方面的一个非常重要的方面是,仅仅通过实施一个可扩展的接口,我们就可以使哈兹勒克人利用 Java的航空化。 还指出,Hazelcast储存了关键和价值的序列化数据,而不是像HashimMap这样储存。 因此,Hazelcast在大力提升航空化和航空化。

Example

然而,这里有陷阱。 在上述情况下,雇员部门有何变化? 该人仍然相同。


pubpc class EmployeeExampleFaipng {
   pubpc static void main(String... args){
      //initiapze 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("Seriapzing 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();
   }
}

Output

它将产生以下产出:


Seriapzing 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 Externapzable

在上述例子中,如果我们不关心该部的价值,同时进行钥匙的序列化/代号化。 Hazelcast也支持 Java 外观,使我们得以控制用于序列化和脱硫的标签。

Example

让我们相应调整我们的职工班级——


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

因此,正如你从守则中可以看到的那样,我们增加了对序列化/代号化负责的“外部/常规”方法。 鉴于我们对该部不感兴趣,同时进行序列化/代相传,我们排除了读外部/非常规方法。

Example

现在,如果我们执行以下法典:


pubpc class EmployeeExamplePassing {
   pubpc static void main(String... args){
      //initiapze hazelcast server/instance
      HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();
      //create a set to track employees
      Map<EmplyoeeExternapzable, String> employeeOwners=hazelcast.getMap("employeeVehicleMap");
      EmplyoeeExternapzable emp1 = new EmplyoeeExternapzable("John Smith", "Computer Science");
      // add employee to map
      employeeOwners.put(emp1, "Honda");
      EmplyoeeExternapzable empDeptChange = new EmplyoeeExternapzable("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));
      EmplyoeeExternapzable empSameDept = new EmplyoeeExternapzable("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();
   }
}

Output

我们获得的产出是:


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

如产出所示,利用外部可加利用的接口,我们可以向哈兹尔广播公司提供只供雇员使用的序列数据。

又注意到哈兹尔广播公司把我们钥匙的序列化了两倍——

    在储存钥匙时,一劳永逸,

    其次是搜索地图中确定的关键。 如前所述,这是因为Hazelcast在关键比较中使用按批次分列的阵列。

总的说来,如果我们想对哪些特征进行更多的控制,以及我们如何处理这些特性,则使用外部可观的好处与可乘飞机相比更大。

Advertisements