一、 问题详细描述
- 本文不是描述如何在spark中配置kryo序列化
- spark中仍然使用的是java原生的序列化机制
- kafka存储的数据是使用kryo序列化的,也可以按照类似的方法去自定义反序列化
二、文件描述
(1)kafka存储的数据:VideoEvent
(2)反序列化类: VideoEventDataKryoDeSerializer
(3)spark读取kafka的数据并且进行反序列化:ReadPhotoKryo (其实就是简单的map函数和反序列化函数就行了)
三、使用目的
(1)减少数据的存储量,之前使用的是json格式序列化保存。这里原有的数据量为:30(FPS)*60(s)19201080(JPG),使用不同的序列化方式(json、protostuff、kryo)上传到kafka broker进行对比,因为应用需求,最终选择了kryo.
(2)在spark中kafka consumer的配置,是不可以专门指定key.deserializer和value.deserializer,他会被默认使用ByteArrayDeserializer。所以value一般为字节。参考官方链接 http://spark.apache.org/docs/latest/structured-streaming-kafka-integration.html
(3)还有一种方式,使用的是scala,实现与本文相似的功能,参考链接[https://databricks.com/blog/2017/04/26/processing-data-in-apache-kafka-with-structured-streaming-in-apache-spark-2-2.html],(https://databricks.com/blog/2017/04/26/processing-data-in-apache-kafka-with-structured-streaming-in-apache-spark-2-2.html)
四、代码
代码后期仍需修正,先给出简易可行版本
4.1 存储的数据 VideoEvent.java
ublic class VideoEventData implements Serializable {
private Timestamp timestamp;
private int rows;
private int cols;
private int type;
private byte[] jpgImageBytes = null;
private String data;
public VideoEventData(){
}
public VideoEventData(String cameraId, Timestamp timestamp, int rows, int cols, int type, byte[] jpgImageBytes, String data) {
this.cameraId = cameraId;
this.timestamp = timestamp;
this.rows = rows;
this.cols = cols;
this.type = type;
this.jpgImageBytes = jpgImageBytes;
this.data = data;
}
//getter/setter.....