自定义架构示例
要使用自定义架构,你需要做的就是实现 SerializationSchema
或 DeserializationSchema
接口之一。
public class MyMessageSchema implements DeserializationSchema<MyMessage>, SerializationSchema<MyMessage> {
@Override
public MyMessage deserialize(byte[] bytes) throws IOException {
return MyMessage.fromString(new String(bytes));
}
@Override
public byte[] serialize(MyMessage myMessage) {
return myMessage.toString().getBytes();
}
@Override
public TypeInformation<MyMessage> getProducedType() {
return TypeExtractor.getForClass(MyMessage.class);
}
// Method to decide whether the element signals the end of the stream.
// If true is returned the element won't be emitted.
@Override
public boolean isEndOfStream(MyMessage myMessage) {
return false;
}
}
MyMessage
类定义如下:
public class MyMessage{
public int id;
public String payload;
public Date timestamp;
public MyMessage(){}
public static MyMessage fromString( String s ){
String[] tokens = s.split( "," );
if(tokens.length != 3) throw new RuntimeException( "Invalid record: " + s );
try{
MyMessage message = new MyMessage();
message.id = Integer.parseInt(tokens[0]);
message.payload = tokens[1];
message.timestamp = new Date( Long.parseLong(tokens[0]));
return message;
}catch(NumberFormatException e){
throw new RuntimeException("Invalid record: " + s);
}
}
public String toString(){
return String.format("%d,%s,%dl", id, payload, timestamp.getTime());
}
}