johnjcasey commented on code in PR #29164: URL: https://github.com/apache/beam/pull/29164#discussion_r1393039420
########## sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/errorhandling/BadRecordRouter.java: ########## @@ -0,0 +1,129 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.sdk.transforms.errorhandling; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.Serializable; +import org.apache.beam.sdk.coders.Coder; +import org.apache.beam.sdk.transforms.DoFn.MultiOutputReceiver; +import org.apache.beam.sdk.transforms.errorhandling.BadRecord.Failure; +import org.apache.beam.sdk.transforms.errorhandling.BadRecord.Record; +import org.apache.beam.sdk.util.Preconditions; +import org.apache.beam.sdk.values.TupleTag; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public interface BadRecordRouter extends Serializable { + + BadRecordRouter THROWING_ROUTER = new ThrowingBadRecordRouter(); + + BadRecordRouter RECORDING_ROUTER = new RecordingBadRecordRouter(); + + TupleTag<BadRecord> BAD_RECORD_TAG = new TupleTag<>(); + + <RecordT> void route( + MultiOutputReceiver outputReceiver, + RecordT record, + @Nullable Coder<RecordT> coder, + @Nullable Exception exception, + String description, + String failingTransform) + throws Exception; + + class ThrowingBadRecordRouter implements BadRecordRouter { + + @Override + public <RecordT> void route( + MultiOutputReceiver outputReceiver, + RecordT record, + @Nullable Coder<RecordT> coder, + @Nullable Exception exception, + String description, + String failingTransform) + throws Exception { + if (exception != null) { + throw exception; + } + } + } + + class RecordingBadRecordRouter implements BadRecordRouter { + + private static final Logger LOG = LoggerFactory.getLogger(RecordingBadRecordRouter.class); + + @Override + public <RecordT> void route( + MultiOutputReceiver outputReceiver, + RecordT record, + @Nullable Coder<RecordT> coder, + @Nullable Exception exception, + String description, + String failingTransform) + throws Exception { + Preconditions.checkArgumentNotNull(record); + ObjectWriter objectWriter = new ObjectMapper().writer().withDefaultPrettyPrinter(); + + // Build up record information + BadRecord.Record.Builder recordBuilder = Record.builder(); + try { + recordBuilder.setJsonRecord(objectWriter.writeValueAsString(record)); + } catch (Exception e) { + LOG.error("Unable to serialize record as JSON. Human readable record will be null", e); + } + + // We will sometimes not have a coder for a failing record, for example if it has already been + // modified within the dofn. + if (coder != null) { + recordBuilder.setCoder(coder.toString()); + + try { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); Review Comment: Ty -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
