rdblue commented on a change in pull request #1145:
URL: https://github.com/apache/iceberg/pull/1145#discussion_r450396590



##########
File path: flink/src/main/java/org/apache/iceberg/flink/IcebergStreamWriter.java
##########
@@ -0,0 +1,225 @@
+/*
+ * 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.io.IOException;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.java.ClosureCleaner;
+import org.apache.flink.streaming.api.datastream.DataStream;
+import org.apache.flink.streaming.api.functions.sink.SinkFunction;
+import org.apache.flink.streaming.api.operators.AbstractStreamOperator;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperator;
+import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
+import org.apache.flink.table.api.TableSchema;
+import org.apache.flink.types.Row;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.MetricsConfig;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.exceptions.RuntimeIOException;
+import org.apache.iceberg.flink.data.FlinkParquetWriters;
+import org.apache.iceberg.flink.writer.FileAppenderFactory;
+import org.apache.iceberg.flink.writer.OutputFileFactory;
+import org.apache.iceberg.flink.writer.TaskWriter;
+import org.apache.iceberg.flink.writer.TaskWriterFactory;
+import org.apache.iceberg.hadoop.SerializableConfiguration;
+import org.apache.iceberg.io.FileAppender;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.parquet.Parquet;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.types.TypeUtil;
+import org.apache.iceberg.util.PropertyUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.apache.iceberg.TableProperties.DEFAULT_FILE_FORMAT;
+import static org.apache.iceberg.TableProperties.DEFAULT_FILE_FORMAT_DEFAULT;
+import static org.apache.iceberg.TableProperties.WRITE_TARGET_FILE_SIZE_BYTES;
+import static 
org.apache.iceberg.TableProperties.WRITE_TARGET_FILE_SIZE_BYTES_DEFAULT;
+
+class IcebergStreamWriter extends AbstractStreamOperator<SerializableDataFile>
+    implements OneInputStreamOperator<Row, SerializableDataFile> {
+
+  private static final long serialVersionUID = 1L;
+  private static final Logger LOG = 
LoggerFactory.getLogger(IcebergStreamWriter.class);
+
+  private final String tablePath;
+  private final SerializableConfiguration conf;
+  private Schema readSchema;
+
+  private transient Table table;
+  private transient TaskWriter<Row> writer;
+  private transient int subTaskId;
+
+  /**
+   * Be careful to do the initialization in this constructor, because in 
{@link DataStream#addSink(SinkFunction)}
+   * it will call {@link ClosureCleaner#clean(Object, 
ExecutionConfig.ClosureCleanerLevel, boolean)} to set all the
+   * non-serializable members to be null.
+   *
+   * @param tablePath  The base path of the iceberg table.
+   * @param readSchema The schema of source data.
+   * @param conf       The hadoop's configuration.
+   */
+  private IcebergStreamWriter(String tablePath, Schema readSchema, 
Configuration conf) {
+    this.tablePath = tablePath;
+    this.conf = new SerializableConfiguration(conf);
+    this.readSchema = readSchema;
+  }
+
+  @Override
+  public void open() {
+    this.table = TableUtil.findTable(tablePath, conf.get());
+    if (this.readSchema != null) {
+      // reassign ids to match the existing table schema
+      readSchema = TypeUtil.reassignIds(readSchema, table.schema());

Review comment:
       Okay, that sounds correct to me.
   
   One thing that doesn't make sense is why the read schema is coming from the 
user with IDs. We would normally want the projection to be provided as names to 
project from the table schema, then use `table.schema().select("col1", "col2", 
...)` to get the read schema.
   
   The way we do this for Spark SQL is we convert the Iceberg schema to the SQL 
representation, then are passed back a subset of that schema. Next, we convert 
the SQL schema back to Iceberg using a method like the `reassignIds` you use 
here. I think what I didn't expect was that you'd get an Iceberg schema passed 
in as the user's schema. Does Flink have a schema representation we could use 
instead?




----------------------------------------------------------------
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]

Reply via email to