是否可以使用星火结构流从kafka中读取protobuf消息?
答案 0 :(得分:0)
sparkSession.udf().register("deserialize", getDeserializer(), schema);
DataStreamReader dataStreamReader = sparkSession.readStream().format("kafka");
for (Map.Entry<String, String> kafkaPropEntry : kafkaProps.entrySet()) {
dataStreamReader.option(kafkaPropEntry.getKey(), kafkaPropEntry.getValue());
}
Dataset<Row> kafkaRecords =
dataStreamReader.load()
.selectExpr("deserialize(value) as event").select("event.*");
final StructType schema = getSchema();
DataStreamReader dataStreamReader = sparkSession.readStream().format("kafka");
for (Map.Entry<String, String> kafkaPropEntry : kafkaProps.entrySet()) {
dataStreamReader.option(kafkaPropEntry.getKey(), kafkaPropEntry.getValue());
}
Dataset<Row> kafkaRecords = dataStreamReader.load()
.map(row -> getOutputRow((byte[]) row.get(VALUE_INDEX)), RowEncoder.apply(schema))
方法1有一个缺陷,因为反序列化方法被多次调用(事件中为Evert列)https://issues.apache.org/jira/browse/SPARK-17728。方法2使用map方法将protobuf直接映射到行。