kbendick commented on a change in pull request #1185: URL: https://github.com/apache/iceberg/pull/1185#discussion_r460362464
########## 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: Most of the time, I simply assign the `name` as the `uid`. In Scala, I use an implicit class to assign name and uid at the same time with the same value. This assumes that the name of an operator is unique, but so far I've not run into issues with the handful of jobs that I oversee. ---------------------------------------------------------------- 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]
