首页 > 大数据 > 数据分析 > Kafka SimpleStringSchema 可能会造成空指针异常
By default, the serializer uses "UTF-8" for string/byte conversion. */ @PublicEvolving public class MyStringSchema implements DeserializationSchema, SerializationSchema { private static final long serialVersionUID = 1L; /** * The charset to use to convert between strings and bytes. The field is transient because we * serialize a different delegate object instead */ private transient Charset charset; /** Creates a new SimpleStringSchema that uses "UTF-8" as the encoding. */ public MyStringSchema() { this(StandardCharsets.UTF_8); } /** * Creates a new SimpleStringSchema that uses the given charset to convert between strings and * bytes. * * @param charset The charset to use to convert between strings and bytes. */ public MyStringSchema(Charset charset) { this.charset = checkNotNull(charset); } /** * Gets the charset used by this schema for serialization. * * @return The charset used by this schema for serialization. */ public Charset getCharset() { return charset; } // ------------------------------------------------------------------------ // Kafka Serialization // ------------------------------------------------------------------------ @Override public String deserialize(byte[] message) { if(Objects.isNull(message)){ return ""; } return new String(message, charset); } @Override public boolean isEndOfStream(String nextElement) { return false; } @Override public byte[] serialize(String element) { return element.getBytes(charset); } @Override public TypeInformation getProducedType() { return BasicTypeInfo.STRING_TYPE_INFO; } // ------------------------------------------------------------------------ // Java Serialization // ------------------------------------------------------------------------ private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeUTF(charset.name()); } private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); String charsetName = in.readUTF(); this.charset = Charset.forName(charsetName); } } ```
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70024433/viewspace-2931459/ ,如需转载,请注明出处,否则将追究法律责任。
注册时间:2022-11-21