reuvenlax commented on code in PR #27866: URL: https://github.com/apache/beam/pull/27866#discussion_r1286537866
########## sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/StorageApiDynamicDestinationsProto.java: ########## @@ -0,0 +1,103 @@ +/* + * 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.gcp.bigquery; + +import com.google.api.services.bigquery.model.TableRow; +import com.google.cloud.bigquery.storage.v1.TableSchema; +import com.google.protobuf.Descriptors; +import com.google.protobuf.Message; +import java.io.Serializable; +import java.lang.reflect.InvocationTargetException; +import javax.annotation.Nullable; +import org.apache.beam.sdk.io.gcp.bigquery.BigQueryServices.DatasetService; +import org.apache.beam.sdk.util.Preconditions; +import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Supplier; +import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Suppliers; +import org.checkerframework.checker.nullness.qual.NonNull; + +/** Storage API DynamicDestinations used when the input is a Beam Row. */ +class StorageApiDynamicDestinationsProto<T extends Message, DestinationT extends @NonNull Object> + extends StorageApiDynamicDestinations<T, DestinationT> { + private final Class<T> protoClass; + Supplier<Descriptors.Descriptor> getDescriptor; + + @SuppressWarnings({"unchecked", "nullness"}) + StorageApiDynamicDestinationsProto( + DynamicDestinations<T, DestinationT> inner, Class<T> protoClass) { + super(inner); + this.protoClass = protoClass; + this.getDescriptor = + Suppliers.memoize( + (Supplier<Descriptors.Descriptor> & Serializable) + () -> { + try { + return (Descriptors.Descriptor) + Preconditions.checkStateNotNull(this.protoClass.getMethod("getDescriptor")) + .invoke(null); + } catch (IllegalAccessException + | InvocationTargetException + | NoSuchMethodException e) { + throw new IllegalArgumentException(e); + } + }); + } + + @Override + public MessageConverter<T> getMessageConverter( + DestinationT destination, DatasetService datasetService) throws Exception { + return new Converter( + TableRowToStorageApiProto.schemaToProtoTableSchema( + Preconditions.checkStateNotNull(getSchema(destination)))); + } + + class Converter implements MessageConverter<T> { + TableSchema tableSchema; + + Converter(TableSchema tableSchema) { + this.tableSchema = tableSchema; + } + + @Override + public TableSchema getTableSchema() { + return tableSchema; + } + + @Override + public Descriptors.Descriptor getDescriptor(boolean includeCdcColumns) throws Exception { + org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument( + !includeCdcColumns); Review Comment: FYI - I'm debating whether allowing it. We could simply support that by falling back to StorageApiDynamicDestinationsTableRow, and just passing in the format function. The downside is that enabling CDC (or ignoreUnknownValues) will then suddenly make the pipeline slower as under the covers we'll be converting the proto into a TableRow and then back into a generated protocol buffer. -- 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]
