简体   繁体   English

我应该如何定义 Flink 的 Schema 来从 Pulsar 读取 Protocol Buffer 数据

[英]How should I define Flink's Schema to read Protocol Buffer data from Pulsar

I am using Pulsar-Flink to read data from Pulsar in Flink.我正在使用Pulsar-Flink从 Flink 中的 Pulsar 读取数据。 I am having difficulty when the data's format is Protocol Buffer.当数据的格式是协议缓冲区时,我遇到了困难。

In the GitHub top page, Pulsar-Flink is using SimpleStringSchema .在 GitHub 的首页中,Pulsar-Flink 正在使用SimpleStringSchema However, seemingly it does not comply with Protocol Buffer officially.但是,它似乎并没有正式遵守 Protocol Buffer。 Does anyone know how to deal with the data format?有谁知道如何处理数据格式? How should I define the schema?我应该如何定义架构?

StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();
Properties props = new Properties();
props.setProperty("topic", "test-source-topic")
FlinkPulsarSource<String> source = new FlinkPulsarSource<>(serviceUrl, adminUrl, new SimpleStringSchema(), props);

DataStream<String> stream = see.addSource(source);

// chain operations on dataStream of String and sink the output
// end method chaining

see.execute();

FYI, I am writing Scala code, so if your explanation is for Scala(not for Java), it is really helpful.仅供参考,我正在编写 Scala 代码,所以如果您的解释是针对 Scala(不是针对 Java),那真的很有帮助。 Surely, any kind of advice is welcome.!当然,欢迎任何形式的建议。! Including Java.包括Java。

You should implement your own DeserializationSchema .您应该实现自己的DeserializationSchema Let's assume that you have a protobuf message Address and have generated the respective Java class.让我们假设你有一个 protobuf 消息地址并且已经生成了相应的 Java class。 Then the schema should look like the following:那么架构应该如下所示:

public class ProtoDeserializer implements DeserializationSchema<Address> {
    @Override
    public TypeInformation<Address> getProducedType() {
        return TypeInformation.of(Address.class);
    }

    @Override
    public Address deserialize(byte[] message) throws IOException {
        return Address.parseFrom(message);
    }

    @Override
    public boolean isEndOfStream(Address nextElement) {
        return false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM