kryo serialization over storm

折月煮酒 提交于 2019-12-11 04:49:27

问题


I need to serialize complex object (opencv:Mat) over apache storm (deployed in remote cluster). Can anyone suggest me a good tutorial custom kryo serialization or propose a solution on how to do this? Thanks in advance!


回答1:


I have created a bean

     public class DataBean{

    Mat imageMatrix;
    int id;
    public DataBean(){

    }
    public DataBean(int id, Mat matrix) {
    setId(id);
    setImageMatrix(matrix);
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public Mat getImageMatrix() {
    return imageMatrix;
}
public void setImageMatrix(Mat matrix)
{
    this.imageMatrix=matrix;
}

}

Then I have created the serializer as follow:

      public class DataBeankryo extends Serializer<DataBean> implements Serializable{

    @Override
    public DataBean read(Kryo arg0, Input arg1, Class<DataBean> arg2) {
            DataBean bean=new DataBean();
            bean.setId(arg1.readInt());
            bean.setImageMatrix(arg0.readObject(arg1,org.opencv.core.Mat.class));
            return bean;
    }

    @Override
    public void write(Kryo arg0,  Output output, DataBean bean) {
            //arg0.register(org.opencv.core.Mat.class);
            output.writeInt(bean.getId());
            arg0.writeObject(output,bean.getImageMatrix(),arg0.getSerializer(org.opencv.core.Mat.class));
            //arg0.writeClassAndObject(output, bean);
            output.close();        }}

I hope this can help you!



来源:https://stackoverflow.com/questions/41787444/kryo-serialization-over-storm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!