If we want to publish java beans to apache kafka v 0.9.x, we might need to write our own custom Serializer/Deserializer for them.
This custom serializers can be registered in consumer/producer properties.import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.Map;
import org.apache.kafka.common.serialization.Deserializer;
import org.apache.kafka.common.serialization.Serializer;
import com.model.base.RawFile;
public class RawFileSerializer implements Serializer, Deserializer {
public RawFileSerializer() {
}
@Override
public RawFile deserialize(String arg0, byte[] fileContent) {
ByteArrayInputStream bis = new ByteArrayInputStream(fileContent);
ObjectInput in = null;
Object obj = null;
try {
in = new ObjectInputStream(bis);
obj = in.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
bis.close();
if (in != null) {
in.close();
}
} catch (IOException ex) {
// ignore close exception
}
}
return (RawFile) obj;
}
@Override
public void close() {
// TODO Auto-generated method stub
}
@Override
public void configure(Maparg0, boolean arg1) {
// TODO Auto-generated method stub
}
@Override
public byte[] serialize(String arg0, RawFile file) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
byte[] rawFileBytes = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(file);
rawFileBytes = bos.toByteArray();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
bos.close();
}
} catch (Exception ex) {
ex.getLocalizedMessage();
}
}
return rawFileBytes;
}
}
No comments:
Post a Comment