SteNicholas commented on code in PR #394: URL: https://github.com/apache/flink-table-store/pull/394#discussion_r1035471430
########## flink-table-store-spark/src/main/java/org/apache/flink/table/store/spark/SparkWrite.java: ########## @@ -0,0 +1,153 @@ +/* + * 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.flink.table.store.spark; + +import org.apache.flink.table.store.file.operation.Lock; +import org.apache.flink.table.store.table.SupportsWrite; +import org.apache.flink.table.store.table.Table; +import org.apache.flink.table.store.table.sink.BucketComputer; +import org.apache.flink.table.store.table.sink.FileCommittable; +import org.apache.flink.table.store.table.sink.SerializableCommittable; +import org.apache.flink.table.store.table.sink.TableCommit; +import org.apache.flink.table.store.table.sink.TableWrite; +import org.apache.flink.table.types.logical.RowType; + +import org.apache.spark.api.java.function.Function; +import org.apache.spark.api.java.function.Function2; +import org.apache.spark.sql.Row; +import org.apache.spark.sql.connector.write.V1Write; +import org.apache.spark.sql.sources.InsertableRelation; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +/** Spark {@link V1Write}, it is required to use v1 write for grouping by bucket. */ +public class SparkWrite implements V1Write { + + private final Table table; + private final String queryId; + private final Lock.Factory lockFactory; + + public SparkWrite(Table table, String queryId, Lock.Factory lockFactory) { + if (!(table instanceof SupportsWrite)) { + throw new UnsupportedOperationException("Unsupported table: " + table.getClass()); + } + this.table = table; + this.queryId = queryId; + this.lockFactory = lockFactory; + } + + @Override + public InsertableRelation toInsertableRelation() { + return (data, overwrite) -> { + if (overwrite) { + throw new UnsupportedOperationException("Overwrite is unsupported."); + } + + long identifier = 0; + List<SerializableCommittable> committables = + data.toJavaRDD() + .groupBy(new ComputeBucket(table)) + .mapValues(new WriteRecords(table, queryId, identifier)) + .values() + .reduce(new ListConcat<>()); + TableCommit tableCommit = ((SupportsWrite) table).newCommit(queryId); + try (Lock lock = lockFactory.create()) { + lock.runWithLock( + () -> { + tableCommit.commit( + identifier, + committables.stream() + .map(SerializableCommittable::delegate) + .collect(Collectors.toList())); + return null; + }); + + } catch (Exception e) { + throw new RuntimeException(e); + } + }; + } + + private static class ComputeBucket implements Function<Row, Integer> { + + private final Table table; + private final RowType type; + + private transient BucketComputer lazyComputer; + + private ComputeBucket(Table table) { + this.table = table; + this.type = table.rowType(); + } + + private BucketComputer computer() { + if (lazyComputer == null) { + lazyComputer = ((SupportsWrite) table).bucketComputer(); + } + return lazyComputer; + } + + @Override + public Integer call(Row row) { + return computer().bucket(new SparkRowData(type, row)); + } + } + + private static class WriteRecords + implements Function<Iterable<Row>, List<SerializableCommittable>> { + + private final Table table; + private final RowType type; + private final String queryId; + private final long commitIdentifier; + + private WriteRecords(Table table, String queryId, long commitIdentifier) { + this.table = table; + this.type = table.rowType(); + this.queryId = queryId; + this.commitIdentifier = commitIdentifier; + } + + @Override + public List<SerializableCommittable> call(Iterable<Row> iterables) throws Exception { + TableWrite write = ((SupportsWrite) table).newWrite(queryId); + for (Row row : iterables) { + write.write(new SparkRowData(type, row)); + } + List<FileCommittable> committables = write.prepareCommit(true, commitIdentifier); + write.close(); Review Comment: Remove this caller after using try. -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org