2
votes

I have simple object with two field which extending the hazelcast DataSerializable interface. putting in that map working fine but while retrieving with same name its showing an exception says : 'Problem while reading DataSerializable, namespace: 0, ID: 0'.

I am using hazelcast client '3.12.4' and hazelcast cluster with latest docker base image.

Please let me know guys if anyone of you faced the similar issue? I have not used any db as of now for simplicity purpose. my hazelcast client only saves a simple object in IMap and then retrieving from IMap.

Please find my code snippet below:

Domain Object:

public class Employee implements DataSerializable {

    private String name;
    private Integer serialNumber;

    public Employee() {
    }

    public Employee(String name, Integer serialNumber) {
        this.name = name;
        this.serialNumber = serialNumber;
    }

    ** Getter and Setter **

    @Override
    public void readData(ObjectDataInput in) throws IOException {
        this.name = in.readUTF();
        this.serialNumber = (Integer) in.readInt();
    }

    @Override
    public void writeData(ObjectDataOutput out) throws IOException {
        out.writeUTF(name);
        out.writeInt(serialNumber);
    }

Hazelcast save and get:

IMap<String, Employee> map = hazelcastInstance.getMap("employee");
        map.put(employee.getName(), employee);

EntryObject e = new PredicateBuilder().getEntryObject();
        Predicate predicate = e.get("serialNumber").lessThan(200);

Collection<Employee> result = map.values(predicate);
}

Thanks in advance for your help.

1
Does your class have a default constructor ? Hazelcast has to create the object before running the readData method of DataSerializable to populate the fields. If you can post the class definition or even an mvce that would be helpful - Neil Stevenson
@NeilStevenson I am using the default constructor in my domain object.Please find the code snippet in my post for more details. - Sumanta
Does IMap.get(key) work ? You're doing a search so need the domain model on the serverside, see github.com/hazelcast/… - Neil Stevenson
@NeilStevenson when I am trying to get the map value by key,Its working fine. Above mentioned exception I am getting while this predicate trying to fetch serialNumber less than 200 and and after that map.values are getting called. Do you mean that this domain model class should be there in my cluster side also ? - Sumanta
Yes, for DataSerializable the domain model needs to be available serverside in order for the search to examine each record to determine if it is a match or not. Can you try adding the domain model to the classpath of the Docker container and report back ? - Neil Stevenson

1 Answers

1
votes

Based on the comment dialogue, the answer here seems to be two-fold

map.get(k) works fine, so the serialization logic itself appears to correct.

In order to query DataSerializable objects, the domain objects need to be on the classpath of the server. The server must fully deserialise each object of that type to determine if it matches against the query predicate. The requires the docker container's classpath extended using the "-e CLASSPATH" option.