问题
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