kennknowles commented on code in PR #40481: URL: https://github.com/apache/beam/pull/40481#discussion_r4337447119
########## sdks/java/io/splunk/src/main/java/org/apache/beam/sdk/io/splunk/SplunkWriteSchemaTransformProvider.java: ########## @@ -0,0 +1,319 @@ +/* + * 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.io.splunk; + +import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkNotNull; + +import com.google.auto.service.AutoService; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.JsonPrimitive; +import java.net.HttpURLConnection; +import java.util.Collections; +import java.util.List; +import org.apache.beam.sdk.coders.RowCoder; +import org.apache.beam.sdk.schemas.Schema; +import org.apache.beam.sdk.schemas.transforms.SchemaTransform; +import org.apache.beam.sdk.schemas.transforms.SchemaTransformProvider; +import org.apache.beam.sdk.schemas.transforms.TypedSchemaTransformProvider; +import org.apache.beam.sdk.schemas.transforms.providers.ErrorHandling; +import org.apache.beam.sdk.transforms.Create; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.transforms.Flatten; +import org.apache.beam.sdk.transforms.MapElements; +import org.apache.beam.sdk.transforms.ParDo; +import org.apache.beam.sdk.values.PCollection; +import org.apache.beam.sdk.values.PCollectionList; +import org.apache.beam.sdk.values.PCollectionRowTuple; +import org.apache.beam.sdk.values.PCollectionTuple; +import org.apache.beam.sdk.values.Row; +import org.apache.beam.sdk.values.TupleTag; +import org.apache.beam.sdk.values.TupleTagList; +import org.apache.beam.sdk.values.TypeDescriptors; + +/** + * An implementation of {@link TypedSchemaTransformProvider} for writing to Splunk's Http Event + * Collector (HEC) using {@link SplunkIO}. + * + * <p>Each input row is converted to a {@link SplunkEvent}. The row must contain a string {@code + * event} field, and may contain {@code time} (INT64), {@code host}, {@code source}, {@code + * sourcetype} and {@code index} (strings) and {@code fields} (a row or a JSON object string). Any + * other fields are ignored. + */ +@AutoService(SchemaTransformProvider.class) +public class SplunkWriteSchemaTransformProvider + extends TypedSchemaTransformProvider<SplunkWriteSchemaTransformConfiguration> { + private static final String IDENTIFIER = "beam:schematransform:org.apache.beam:splunk_write:v1"; + static final String INPUT = "input"; + static final String OUTPUT = "output"; + static final String ERROR = "errors"; + public static final TupleTag<Row> ERROR_TAG = new TupleTag<Row>() {}; + public static final TupleTag<SplunkEvent> EVENT_TAG = new TupleTag<SplunkEvent>() {}; + + @Override + protected Class<SplunkWriteSchemaTransformConfiguration> configurationClass() { + return SplunkWriteSchemaTransformConfiguration.class; + } + + /** Returns the expected {@link SchemaTransform} of the configuration. */ + @Override + protected SchemaTransform from(SplunkWriteSchemaTransformConfiguration configuration) { + return new SplunkWriteSchemaTransform(configuration); + } + + /** Implementation of the {@link TypedSchemaTransformProvider} identifier method. */ + @Override + public String identifier() { + return IDENTIFIER; + } + + /** Implementation of the {@link TypedSchemaTransformProvider} input collection names method. */ + @Override + public List<String> inputCollectionNames() { + return Collections.singletonList(INPUT); + } + + /** Implementation of the {@link TypedSchemaTransformProvider} output collection names method. */ + @Override + public List<String> outputCollectionNames() { + return Collections.singletonList(ERROR); + } + + /** + * An implementation of {@link SchemaTransform} for Splunk Write jobs configured using {@link + * SplunkWriteSchemaTransformConfiguration}. + */ + static class SplunkWriteSchemaTransform extends SchemaTransform { + private final SplunkWriteSchemaTransformConfiguration configuration; + + SplunkWriteSchemaTransform(SplunkWriteSchemaTransformConfiguration configuration) { + this.configuration = configuration; + } + + @Override + public PCollectionRowTuple expand(PCollectionRowTuple input) { + configuration.validate(); + + PCollection<Row> inputRows = input.get(INPUT); + boolean handleErrors = ErrorHandling.hasOutput(configuration.getErrorHandling()); + + Schema errorSchema = + Schema.builder() + .addNullableRowField("failed_row", inputRows.getSchema()) Review Comment: Is this duplicate with the SplunkWriteErrorSchemaProvider? ########## sdks/java/io/splunk/src/test/java/org/apache/beam/sdk/io/splunk/SplunkWriteErrorTest.java: ########## @@ -55,4 +62,66 @@ public void testEquals() { .create(), actualError); } + + /** Test whether the default schema of {@link SplunkWriteError} has a field per accessor. */ + @Test + public void testDefaultSchema() throws NoSuchSchemaException { + Schema schema = SchemaRegistry.createDefault().getSchema(SplunkWriteError.class); + + assertEquals( + Schema.builder() + .addNullableField("statusCode", Schema.FieldType.INT32) Review Comment: This seems like just a duplicate of the code in the schema provider. Is there something being tested here? ########## sdks/python/apache_beam/yaml/integration_tests.py: ########## @@ -63,12 +64,20 @@ def get_impl(self): return BigEndianIntegerCoderImpl() +def _java_serialized_coder(payload, components, context): + # Java's VarIntCoder (used to key SplunkIO's stateful writer) shares the same + # fallback URN. The payload is the snappy-compressed serialized Java coder, + # and the start of its class name stays uncompressed, so it can be used to + # tell the two apart. + if b'VarInt' in payload: Review Comment: This seems like a major hack. What is the goal here? If there is a "standard coder" that is accidentally being serialized as `beam:coders:javasdk` then we should fix that in the Java SDK. -- 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]
