如何在Hazelcast中读取和写入确定的数据

发布于 2025-01-30 11:43:00 字数 4990 浏览 2 评论 0原文

我有一个问题,文档和github示例并没有提供有关如何做的清晰示例。...

我有这个班级

public class KeySerializable implements IdentifiedDataSerializable{



private String claveReq;
private int id_interno_pe;
private String cod_nrbe_en;
private int num_sec_ac;
public KeySerializable(String claveReq, int id_interno_pe, String cod_nrbe_en, int num_sec_ac) {
    this.claveReq = claveReq;
    this.id_interno_pe = id_interno_pe;
    this.cod_nrbe_en = cod_nrbe_en;
    this.num_sec_ac = num_sec_ac;
}
public KeySerializable() {
}
public void writeData(ObjectDataOutput out) throws IOException {
    out.writeString(claveReq);
    out.writeInt(id_interno_pe);
    out.writeString(cod_nrbe_en);
    out.writeInt(num_sec_ac);
    
}
public void readData(ObjectDataInput in) throws IOException {
    this.claveReq = in.readString();
    this.id_interno_pe = in.readInt();
    this.cod_nrbe_en = in.readString();
    this.num_sec_ac = in.readInt();
    
}
public int getFactoryId() {
    return KeySerializableFactory.FACTORY_ID;
}
public int getClassId() {
    return KeySerializableFactory.KEY_SERIALIZABLE_TYPE;
}
@Override
public String toString() {
    return "KeySerializable [claveReq=" + claveReq + ", id_interno_pe=" + id_interno_pe + ", cod_nrbe_en="
            + cod_nrbe_en + ", num_sec_ac=" + num_sec_ac + "]";
}

}

,这个班级

public class ResponseSerializablePlus implements IdentifiedDataSerializable{




private int id_interno_pe;
private String cod_nrbe_en;
private int num_sec_ac;
private int statusCode;
private HashMap<String,List<String>> headers;
private byte[] content;
public ResponseSerializablePlus(int id_interno_pe, String cod_nrbe_en, int num_sec_ac, int statusCode,
        HashMap<String, List<String>> headers, byte[] content) {
    this.id_interno_pe = id_interno_pe;
    this.cod_nrbe_en = cod_nrbe_en;
    this.num_sec_ac = num_sec_ac;
    this.statusCode = statusCode;
    this.headers = headers;
    this.content = content;
}
public ResponseSerializablePlus() {
}
public void writeData(ObjectDataOutput out) throws IOException {
    out.writeInt(id_interno_pe);
    out.writeString(cod_nrbe_en);
    out.writeInt(num_sec_ac);
    out.write(statusCode);
    out.writeObject(headers);
    out.writeByteArray(content);
    
}
public void readData(ObjectDataInput in) throws IOException {
    this.id_interno_pe = in.readInt();
    this.cod_nrbe_en = in.readString();
    this.num_sec_ac = in.readInt();
    this.statusCode = in.readInt();
    this.headers = in.readObject();
    this.content = in.readByteArray();
    
}
public int getFactoryId() {
    return ResponseSerializablePlusFactory.FACTORY_ID;
}
public int getClassId() {
    return ResponseSerializablePlusFactory.RESPONSE_SERIALIZABLE_PLUS_CLASS;
}
@Override
public String toString() {
    return "ResponseSerializablePlus [id_interno_pe=" + id_interno_pe + ", cod_nrbe_en=" + cod_nrbe_en
            + ", num_sec_ac=" + num_sec_ac + ", statusCode=" + statusCode + ", headers=" + headers + ", content="
            + Arrays.toString(content) + "]";
}

和其他班级

public class ResponseSerializable implements IdentifiedDataSerializable{



private int statusCode;
private HashMap<String,List<String>> headers;
private byte[] content;
public ResponseSerializable(int statusCode, HashMap<String, List<String>> headers, byte[] content) {
    this.statusCode = statusCode;
    this.headers = headers;
    this.content = content;
}
public ResponseSerializable() {
}

public void writeData(ObjectDataOutput out) throws IOException {
    out.write(statusCode);
    out.writeObject(headers);
    out.writeByteArray(content);
    
}

public void readData(ObjectDataInput in) throws IOException {
    this.statusCode = in.readInt();
    this.headers = in.readObject();
    this.content = in.readByteArray();
    
}
public int getFactoryId() {
    
    return ResponseSerializableFactory.FACTORY_ID;
}
public int getClassId() {
    return ResponseSerializableFactory.RESPONSE_TYPE;
}
@Override
public String toString() {
    return "ResponseSerializable [statusCode=" + statusCode + ", headers=" + headers + ", content="
            + Arrays.toString(content) + "]";
}

}

以及工厂始终相同,但是有不同的类别

public class KeySerializableFactory implements DataSerializableFactory{

public static final int FACTORY_ID = 1;

public static final int KEY_SERIALIZABLE_TYPE = 1;

public IdentifiedDataSerializable create(int typeId) {
    if ( typeId == KEY_SERIALIZABLE_TYPE ) {
        return new KeySerializable();
    } else {
        return null;
    }
}

}

and im总是有这个错误

”在此处输入图像描述”

hazelcast中的文档和github示例没有提供有关如何使用getters and steters的好示例,我不明白在这里该怎么做写或阅读对象

有提示吗?你能帮助我吗?

i have a problem and the documentations and github examples doesn't provide a clear example about how to do it....

i have this class

public class KeySerializable implements IdentifiedDataSerializable{



private String claveReq;
private int id_interno_pe;
private String cod_nrbe_en;
private int num_sec_ac;
public KeySerializable(String claveReq, int id_interno_pe, String cod_nrbe_en, int num_sec_ac) {
    this.claveReq = claveReq;
    this.id_interno_pe = id_interno_pe;
    this.cod_nrbe_en = cod_nrbe_en;
    this.num_sec_ac = num_sec_ac;
}
public KeySerializable() {
}
public void writeData(ObjectDataOutput out) throws IOException {
    out.writeString(claveReq);
    out.writeInt(id_interno_pe);
    out.writeString(cod_nrbe_en);
    out.writeInt(num_sec_ac);
    
}
public void readData(ObjectDataInput in) throws IOException {
    this.claveReq = in.readString();
    this.id_interno_pe = in.readInt();
    this.cod_nrbe_en = in.readString();
    this.num_sec_ac = in.readInt();
    
}
public int getFactoryId() {
    return KeySerializableFactory.FACTORY_ID;
}
public int getClassId() {
    return KeySerializableFactory.KEY_SERIALIZABLE_TYPE;
}
@Override
public String toString() {
    return "KeySerializable [claveReq=" + claveReq + ", id_interno_pe=" + id_interno_pe + ", cod_nrbe_en="
            + cod_nrbe_en + ", num_sec_ac=" + num_sec_ac + "]";
}

}

and this class

public class ResponseSerializablePlus implements IdentifiedDataSerializable{




private int id_interno_pe;
private String cod_nrbe_en;
private int num_sec_ac;
private int statusCode;
private HashMap<String,List<String>> headers;
private byte[] content;
public ResponseSerializablePlus(int id_interno_pe, String cod_nrbe_en, int num_sec_ac, int statusCode,
        HashMap<String, List<String>> headers, byte[] content) {
    this.id_interno_pe = id_interno_pe;
    this.cod_nrbe_en = cod_nrbe_en;
    this.num_sec_ac = num_sec_ac;
    this.statusCode = statusCode;
    this.headers = headers;
    this.content = content;
}
public ResponseSerializablePlus() {
}
public void writeData(ObjectDataOutput out) throws IOException {
    out.writeInt(id_interno_pe);
    out.writeString(cod_nrbe_en);
    out.writeInt(num_sec_ac);
    out.write(statusCode);
    out.writeObject(headers);
    out.writeByteArray(content);
    
}
public void readData(ObjectDataInput in) throws IOException {
    this.id_interno_pe = in.readInt();
    this.cod_nrbe_en = in.readString();
    this.num_sec_ac = in.readInt();
    this.statusCode = in.readInt();
    this.headers = in.readObject();
    this.content = in.readByteArray();
    
}
public int getFactoryId() {
    return ResponseSerializablePlusFactory.FACTORY_ID;
}
public int getClassId() {
    return ResponseSerializablePlusFactory.RESPONSE_SERIALIZABLE_PLUS_CLASS;
}
@Override
public String toString() {
    return "ResponseSerializablePlus [id_interno_pe=" + id_interno_pe + ", cod_nrbe_en=" + cod_nrbe_en
            + ", num_sec_ac=" + num_sec_ac + ", statusCode=" + statusCode + ", headers=" + headers + ", content="
            + Arrays.toString(content) + "]";
}

and this other class

public class ResponseSerializable implements IdentifiedDataSerializable{



private int statusCode;
private HashMap<String,List<String>> headers;
private byte[] content;
public ResponseSerializable(int statusCode, HashMap<String, List<String>> headers, byte[] content) {
    this.statusCode = statusCode;
    this.headers = headers;
    this.content = content;
}
public ResponseSerializable() {
}

public void writeData(ObjectDataOutput out) throws IOException {
    out.write(statusCode);
    out.writeObject(headers);
    out.writeByteArray(content);
    
}

public void readData(ObjectDataInput in) throws IOException {
    this.statusCode = in.readInt();
    this.headers = in.readObject();
    this.content = in.readByteArray();
    
}
public int getFactoryId() {
    
    return ResponseSerializableFactory.FACTORY_ID;
}
public int getClassId() {
    return ResponseSerializableFactory.RESPONSE_TYPE;
}
@Override
public String toString() {
    return "ResponseSerializable [statusCode=" + statusCode + ", headers=" + headers + ", content="
            + Arrays.toString(content) + "]";
}

}

and the factory it's always the same but with different classes

public class KeySerializableFactory implements DataSerializableFactory{

public static final int FACTORY_ID = 1;

public static final int KEY_SERIALIZABLE_TYPE = 1;

public IdentifiedDataSerializable create(int typeId) {
    if ( typeId == KEY_SERIALIZABLE_TYPE ) {
        return new KeySerializable();
    } else {
        return null;
    }
}

}

and im always having this bunch errors

enter image description here

the documentation and the github examples from hazelcast doesn't provide a good example about how to use the getters and setters and i don't understand what to do here to write or read an object

any hint? can you help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩于岁月 2025-02-06 11:43:00

您不应该调用readdataWritedata自己。当数据即将被序列化/应对数据时,这些方法由Hazelcast在内部进行调用。

在您的身边,您只能注册您在Hazelcast创建的可序列化工厂。

然后,在收到实现标识的dataSerizable的类的实例并为其注册了工厂时,Hazelcast将调用我在适当位置上提到的我提到的方法,并返回对象读取的对象。

从您共享的代码示例中,您需要从newResponse.writedata开始删除行(...,假设您进行了出厂注册

。 >响应eRializablePlus 和响应类:您需要使用writeInt方法来编写statuscode readdata 和写入方法应彼此一致。

You shouldn't be calling readData or writeData yourself. These methods are called by Hazelcast internally when the data is about to be serialized/deserialized.

On your side, you should only register the data serializable factories you have created to the Hazelcast.

Then, upon receiving an instance of a class that implements IdentifiedDataSerializable and has a factory registered for it, Hazelcast will call the methods I mentioned above in appropriate places and return you the object read.

From the code sample you shared, you need to drop the line starting with newResponse.writeData(..., and everything should be working, assuming you did the factory registration.

Also, please fix your ResponseSerializablePlus and ResponseSerializable classes: You need to use writeInt method to write the statusCode field. The readData and writeData methods should be consistent with each other.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文