Logging Error message while reading or writing to Topics

末鹿安然 提交于 2020-08-08 17:58:04

问题


How do you log error messages while reading or writing to topic. We would be using Apache Beam API to read or write to topic. So I any exception is generated how do we log it. Can I send my data to other topic?

PubsubIO.writeMessages() PubsubIO.readMessages()

Can I write this DoFn and add debug logs log.debug("Publishing json message to pubsub topic"); PubsubIO.Write message = PubsubIO.writeMessages().to(pipelineOptions.getPubsubEnpEventTopic()); log.debug("Message published to pubsub");


回答1:


There are two methods 'withCoderAndParseFn' while reading from pubsub and 'withFormatFn' while writing to pubsub. These function are useful to parse messages while reading or writing. May be you can use them by applying your own logic in there like logging into stackdriver or pushing error data to pubsub.

More you can find here https://www.codota.com/web/assistant/code/rs/5c6563f8138b620001f3aa7c#L470

PubsubIO.<t>read().withCoderAndParseFn(coder, new ParsePayloadUsingCoder<>(coder));

private static class ParsePayloadUsingCoder<t> extends SimpleFunction<pubsubmessage, t=""> {
    private Coder<t> coder;

    public ParsePayloadUsingCoder(Coder<t> coder) {
      this.coder = coder;
    }

    @Override
    public T apply(PubsubMessage input) {
      try {
        return CoderUtils.decodeFromByteArray(coder, input.getPayload());
      } catch (CoderException e) {
        throw new RuntimeException("Could not decode Pubsub message", e);
      }
    }
  }


来源:https://stackoverflow.com/questions/58733142/logging-error-message-while-reading-or-writing-to-topics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!