kbendick commented on a change in pull request #1185: URL: https://github.com/apache/iceberg/pull/1185#discussion_r460362001
########## File path: flink/src/main/java/org/apache/iceberg/flink/IcebergDataStream.java ########## @@ -0,0 +1,109 @@ +/* + * 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.iceberg.flink; + +import java.util.UUID; +import org.apache.flink.streaming.api.datastream.DataStream; +import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator; +import org.apache.flink.table.api.TableSchema; +import org.apache.flink.types.Row; +import org.apache.hadoop.conf.Configuration; +import org.apache.iceberg.DataFile; + +public class IcebergDataStream { + + private static final String ICEBERG_STREAM_WRITER = "Iceberg-Stream-Writer"; + private static final String ICEBERG_FILES_COMMITTER = "Iceberg-Files-Committer"; + + private final DataStream<Row> dataStream; + private final String path; + private final Configuration conf; + private final TableSchema tableSchema; + private final Integer parallelism; + + private IcebergDataStream(DataStream<Row> dataStream, String path, + Configuration conf, TableSchema tableSchema, + Integer parallelism) { + this.dataStream = dataStream; + this.path = path; + this.conf = conf; + this.tableSchema = tableSchema; + this.parallelism = parallelism; + } + + public static Builder buildFor(DataStream<Row> dataStream) { + return new Builder().dataStream(dataStream); + } + + public static class Builder { + private DataStream<Row> dataStream; + private String path; + private Configuration conf; + private TableSchema tableSchema; + private Integer parallelism; + + private Builder dataStream(DataStream<Row> newDataStream) { + this.dataStream = newDataStream; + return this; + } + + public Builder path(String newPath) { + this.path = newPath; + return this; + } + + public Builder config(Configuration newConfig) { + this.conf = newConfig; + return this; + } + + public Builder tableSchema(TableSchema newTableSchema) { + this.tableSchema = newTableSchema; + return this; + } + + public Builder parallelism(int newParallelism) { + this.parallelism = newParallelism; + return this; + } + + public IcebergDataStream build() { + return new IcebergDataStream(dataStream, path, conf, tableSchema, parallelism); + } + } + + public void append() { + IcebergStreamWriter streamWriter = IcebergStreamWriter.createStreamWriter(path, tableSchema, conf); + IcebergFilesCommitter filesCommitter = new IcebergFilesCommitter(path, conf); + + SingleOutputStreamOperator<DataFile> operator = dataStream + .transform(ICEBERG_STREAM_WRITER, DataFileTypeInfo.TYPE_INFO, streamWriter) + .uid(UUID.randomUUID().toString()); + + if (parallelism != null && parallelism > 0) { + operator.setParallelism(parallelism); + } + + operator.addSink(filesCommitter) + .name(ICEBERG_FILES_COMMITTER) + .uid(UUID.randomUUID().toString()) Review comment: Setting the uid to be a randomly generated number is not a good practice. Operator uid's should be stable, such that between intentional redeployments or simply on restarts due to lost task managers, Flink will know which operators each state belongs to. If the uid is changed between restarts, state could be lost. You can find more information about the use of operator uid's with flink's savepoints (intentional restarts) and why stateful operators should be assigned a stable uid here: https://ci.apache.org/projects/flink/flink-docs-stable/ops/upgrading.html#matching-operator-state ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
