kryo序列化风暴

时间:2017-01-22 04:44:46

标签: apache-storm kryo

我需要通过apache storm(部署在远程集群中)序列化复杂对象(opencv:Mat)。任何人都可以建议我一个很好的教程自定义kryo序列化或提出如何做到这一点的解决方案? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

我创建了一个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;
}

}

然后我创建了序列化程序如下:

      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();        }}

我希望这可以帮到你!