AdalbertMemSQL commented on code in PR #23535: URL: https://github.com/apache/beam/pull/23535#discussion_r1018033307
########## sdks/java/io/singlestore/src/main/java/org/apache/beam/sdk/io/singlestore/Write.java: ########## @@ -0,0 +1,255 @@ +/* + * 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.singlestore; + +import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument; +import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull; + +import com.google.auto.value.AutoValue; +import java.io.IOException; +import java.io.InputStream; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; +import java.nio.charset.StandardCharsets; +import java.sql.Connection; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; +import javax.sql.DataSource; +import org.apache.beam.sdk.coders.ListCoder; +import org.apache.beam.sdk.coders.StringUtf8Coder; +import org.apache.beam.sdk.coders.VoidCoder; +import org.apache.beam.sdk.options.ValueProvider; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.transforms.PTransform; +import org.apache.beam.sdk.transforms.ParDo; +import org.apache.beam.sdk.transforms.display.DisplayData; +import org.apache.beam.sdk.transforms.windowing.GlobalWindow; +import org.apache.beam.sdk.values.PCollection; +import org.apache.beam.sdk.values.PDone; +import org.apache.commons.dbcp2.DelegatingStatement; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.joda.time.Instant; + +@AutoValue +public abstract class Write<T> extends PTransform<PCollection<T>, PDone> { + + private static final int DEFAULT_BATCH_SIZE = 100000; + private static final int BUFFER_SIZE = 524288; + + abstract @Nullable DataSourceConfiguration getDataSourceConfiguration(); + + abstract @Nullable ValueProvider<String> getTable(); + + abstract @Nullable ValueProvider<Integer> getBatchSize(); + + abstract @Nullable UserDataMapper<T> getUserDataMapper(); + + abstract Builder<T> toBuilder(); + + @AutoValue.Builder + abstract static class Builder<T> { + abstract Builder<T> setDataSourceConfiguration(DataSourceConfiguration dataSourceConfiguration); + + abstract Builder<T> setTable(ValueProvider<String> table); + + abstract Builder<T> setBatchSize(ValueProvider<Integer> batchSize); + + abstract Builder<T> setUserDataMapper(UserDataMapper<T> userDataMapper); + + abstract Write<T> build(); + } + + public Write<T> withDataSourceConfiguration(DataSourceConfiguration config) { + checkNotNull(config, "dataSourceConfiguration can not be null"); + return toBuilder().setDataSourceConfiguration(config).build(); + } + + public Write<T> withTable(String table) { + checkNotNull(table, "table can not be null"); + return withTable(ValueProvider.StaticValueProvider.of(table)); + } + + public Write<T> withTable(ValueProvider<String> table) { + checkNotNull(table, "table can not be null"); + return toBuilder().setTable(table).build(); + } + + public Write<T> withUserDataMapper(UserDataMapper<T> userDataMapper) { + checkNotNull(userDataMapper, "userDataMapper can not be null"); + return toBuilder().setUserDataMapper(userDataMapper).build(); + } + + /** + * Provide a maximum number of rows that is written by one SQL statement. Default is 100000. + * + * @param batchSize maximum number of rows that is written by one SQL statement + */ + public Write<T> withBatchSize(Integer batchSize) { + checkNotNull(batchSize, "batchSize can not be null"); + return withBatchSize(ValueProvider.StaticValueProvider.of(batchSize)); + } + + /** Same as {@link #withBatchSize(Integer)} but accepting a ValueProvider. */ + public Write<T> withBatchSize(ValueProvider<Integer> batchSize) { + checkNotNull(batchSize, "batchSize can not be null"); + return toBuilder().setBatchSize(batchSize).build(); + } + + @Override + public PDone expand(PCollection<T> input) { + DataSourceConfiguration dataSourceConfiguration = + Util.getRequiredArgument( + getDataSourceConfiguration(), "withDataSourceConfiguration() is required"); + String table = Util.getRequiredArgument(getTable(), "withTable() is required"); + UserDataMapper<T> userDataMapper = + Util.getRequiredArgument(getUserDataMapper(), "withUserDataMapper() is required"); + int batchSize = Util.getArgumentWithDefault(getBatchSize(), DEFAULT_BATCH_SIZE); + checkArgument(batchSize > 0, "batchSize should be greater then 0"); + + input + .apply( + ParDo.of( + new DoFn<T, List<String>>() { + @ProcessElement + public void processElement(ProcessContext context) throws Exception { + context.output(userDataMapper.mapRow(context.element())); + } + })) + .setCoder(ListCoder.of(StringUtf8Coder.of())) + .apply(ParDo.of(new BatchFn<>(batchSize))) + .apply(ParDo.of(new WriteFn<Void>(dataSourceConfiguration, table))) Review Comment: That makes sense. I will return a PCollection of integers, where each integer will represent a number of rows written by each LOAD DATA query. It will allow you to check the total number of written rows and see how the task is parallelized. -- 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]
