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



##########
File path: 
flink/src/main/java/org/apache/iceberg/flink/writer/PartitionWriter.java
##########
@@ -0,0 +1,146 @@
+/*
+ * 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.writer;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import org.apache.flink.util.Preconditions;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.encryption.EncryptedOutputFile;
+import org.apache.iceberg.flink.PartitionKey;
+import org.apache.iceberg.io.FileAppender;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The PartitionFanoutWriter will open a writing data file for each partition 
and route the given record to the
+ * corresponding data file in the correct partition.
+ *
+ * @param <T> defines the data type of record to write.
+ */
+class PartitionWriter<T> extends BaseTaskWriter<T> {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(PartitionWriter.class);
+
+  private final PartitionSpec spec;
+  private final FileAppenderFactory<T> factory;
+  private final Function<PartitionKey, EncryptedOutputFile> outputFileGetter;
+  private final Function<T, PartitionKey> keyGetter;
+  private final Map<PartitionKey, WrappedFileAppender<T>> writers;
+  private final long targetFileSize;
+  private final FileFormat fileFormat;
+  private final List<DataFile> completeDataFiles;
+
+  PartitionWriter(PartitionSpec spec,
+                  FileAppenderFactory<T> factory,
+                  Function<PartitionKey, EncryptedOutputFile> outputFileGetter,
+                  Function<T, PartitionKey> keyGetter,
+                  long targetFileSize,
+                  FileFormat fileFormat) {
+    this.spec = spec;
+    this.factory = factory;
+    this.outputFileGetter = outputFileGetter;
+    this.keyGetter = keyGetter;
+    this.writers = Maps.newHashMap();
+    this.targetFileSize = targetFileSize;
+    this.fileFormat = fileFormat;
+    this.completeDataFiles = Lists.newArrayList();
+  }
+
+  @Override
+  public void append(T record) throws IOException {
+    PartitionKey partitionKey = keyGetter.apply(record);
+    Preconditions.checkArgument(partitionKey != null, "Partition key shouldn't 
be null");
+
+    WrappedFileAppender<T> writer = writers.get(partitionKey);
+    if (writer == null) {
+      writer = createWrappedFileAppender(partitionKey);
+      writers.put(partitionKey, writer);
+    }
+    writer.fileAppender.add(record);
+
+    // Roll the writer if reach the target file size.
+    writer.currentRows++;
+    if (writer.currentRows % ROW_DIVISOR == 0 && writer.fileAppender.length() 
>= targetFileSize) {
+      closeCurrentWriter(writer);
+      writers.remove(partitionKey);
+    }
+  }
+
+  @Override
+  public void close() throws IOException {
+    for (WrappedFileAppender<T> wrap : writers.values()) {
+      closeCurrentWriter(wrap);
+      LOG.debug("Close file appender: {}, completeDataFiles: {}",
+          wrap.encryptedOutputFile.encryptingOutputFile().location(),
+          completeDataFiles.size());
+    }
+    this.writers.clear();
+  }
+
+  @Override
+  public List<DataFile> getCompleteFiles() {
+    if (completeDataFiles.size() > 0) {
+      return ImmutableList.copyOf(this.completeDataFiles);
+    } else {
+      return Collections.emptyList();
+    }
+  }
+
+  @Override
+  public void reset() {
+    this.completeDataFiles.clear();

Review comment:
       The data file will be put into `completeDataFiles` only after we have 
closed the appenders.  So we can guarantee that all the appenders are closed. 

##########
File path: flink/src/main/java/org/apache/iceberg/flink/writer/TaskWriter.java
##########
@@ -0,0 +1,56 @@
+/*
+ * 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.writer;
+
+import java.io.IOException;
+import java.util.List;
+import org.apache.iceberg.DataFile;
+
+/**
+ * The writer interface which could accept records and provide the generated 
data files.
+ *
+ * @param <T> to indicate the record data type.
+ */
+public interface TaskWriter<T> {
+  int ROW_DIVISOR = 1000;
+
+  /**
+   * Append the row into the data files.
+   */
+  void append(T record) throws IOException;
+
+  /**
+   * Close the writer.
+   */
+  void close() throws IOException;
+
+  /**
+   * To get the full list of complete files, we should call this method after 
{@link TaskWriter#close()} because the
+   * close method will close all the opening data files and build {@link 
DataFile} to the return array list.
+   *
+   * @return the cached completed data files of this task writer.
+   */
+  List<DataFile> getCompleteFiles();
+
+  /**
+   * Reset to clear all the cached complete files.
+   */
+  void reset();

Review comment:
       Yes, as we discussed above,  unifying the `getCompleteFiles` and `reset` 
into a `pollCompleteFiles`   sounds good to me.

##########
File path: 
flink/src/main/java/org/apache/iceberg/flink/writer/OutputFileFactory.java
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.writer;
+
+import java.util.UUID;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.encryption.EncryptedOutputFile;
+import org.apache.iceberg.flink.PartitionKey;
+import org.apache.iceberg.io.OutputFile;
+
+public class OutputFileFactory {
+  private final String uuid = UUID.randomUUID().toString();
+
+  private final Table table;
+  private final FileFormat format;
+  private final long taskId;
+  private int fileCount;
+
+  public OutputFileFactory(Table table, FileFormat format, long taskId) {
+    this.table = table;
+    this.format = format;
+    this.taskId = taskId;
+    this.fileCount = 0;
+  }
+
+  /**
+   * All the data files inside the same task will share the same uuid 
identifier but could be distinguished by the
+   * increasing file count.
+   *
+   * @return the data file name to be written.
+   */
+  private String generateFilename() {
+    int hashCode = Math.abs(this.hashCode() % 10 ^ 5);
+    return format.addExtension(String.format("%05d-%d-%s-%05d", hashCode, 
taskId, uuid, fileCount++));

Review comment:
       Fine, I guess I misunderstood the meanings of spark file naming. Yeah, 
the flink task has the an attempt ID as you said. Let me address it in the next 
pach.

##########
File path: 
flink/src/main/java/org/apache/iceberg/flink/writer/OutputFileFactory.java
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.writer;
+
+import java.util.UUID;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.encryption.EncryptedOutputFile;
+import org.apache.iceberg.flink.PartitionKey;
+import org.apache.iceberg.io.OutputFile;
+
+public class OutputFileFactory {
+  private final String uuid = UUID.randomUUID().toString();
+
+  private final Table table;
+  private final FileFormat format;
+  private final long taskId;
+  private int fileCount;
+
+  public OutputFileFactory(Table table, FileFormat format, long taskId) {

Review comment:
       OK, I think I get the difference you mentioned between spark writer 
implementation and flink writer implementation:
   For spark,   it will load the iceberg table firstly at driver side, then 
create the `DataWriterFactory`  and serialize and dispatch it to each executor, 
then the executor will create its `DataWriter`, so here each executor won't 
need to load the iceberg table.   
   For flink , I currently implemented the `IcebergStreamWriter` by loading the 
iceberg table for each sub task, then each task get the table path and open the 
iceberg table, so the `Table` instance won't need to be serializable. Seems the 
iceberg table will be loaded 100 times if we have 100 parallerism for 
`IcebergStreamWriter`.  @JingsongLi Any thought about this issue ?  I mean: do 
we have some similar ways as spark did to optimize the iceberg table loading ?  

##########
File path: 
flink/src/main/java/org/apache/iceberg/flink/writer/PartitionWriter.java
##########
@@ -0,0 +1,146 @@
+/*
+ * 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.writer;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import org.apache.flink.util.Preconditions;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.encryption.EncryptedOutputFile;
+import org.apache.iceberg.flink.PartitionKey;
+import org.apache.iceberg.io.FileAppender;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The PartitionFanoutWriter will open a writing data file for each partition 
and route the given record to the
+ * corresponding data file in the correct partition.
+ *
+ * @param <T> defines the data type of record to write.
+ */
+class PartitionWriter<T> extends BaseTaskWriter<T> {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(PartitionWriter.class);
+
+  private final PartitionSpec spec;
+  private final FileAppenderFactory<T> factory;
+  private final Function<PartitionKey, EncryptedOutputFile> outputFileGetter;
+  private final Function<T, PartitionKey> keyGetter;
+  private final Map<PartitionKey, WrappedFileAppender<T>> writers;
+  private final long targetFileSize;
+  private final FileFormat fileFormat;
+  private final List<DataFile> completeDataFiles;
+
+  PartitionWriter(PartitionSpec spec,
+                  FileAppenderFactory<T> factory,
+                  Function<PartitionKey, EncryptedOutputFile> outputFileGetter,
+                  Function<T, PartitionKey> keyGetter,
+                  long targetFileSize,
+                  FileFormat fileFormat) {
+    this.spec = spec;
+    this.factory = factory;
+    this.outputFileGetter = outputFileGetter;
+    this.keyGetter = keyGetter;
+    this.writers = Maps.newHashMap();
+    this.targetFileSize = targetFileSize;
+    this.fileFormat = fileFormat;
+    this.completeDataFiles = Lists.newArrayList();
+  }
+
+  @Override
+  public void append(T record) throws IOException {
+    PartitionKey partitionKey = keyGetter.apply(record);
+    Preconditions.checkArgument(partitionKey != null, "Partition key shouldn't 
be null");
+
+    WrappedFileAppender<T> writer = writers.get(partitionKey);
+    if (writer == null) {
+      writer = createWrappedFileAppender(partitionKey);
+      writers.put(partitionKey, writer);
+    }
+    writer.fileAppender.add(record);
+
+    // Roll the writer if reach the target file size.
+    writer.currentRows++;
+    if (writer.currentRows % ROW_DIVISOR == 0 && writer.fileAppender.length() 
>= targetFileSize) {
+      closeCurrentWriter(writer);
+      writers.remove(partitionKey);
+    }
+  }
+
+  @Override
+  public void close() throws IOException {
+    for (WrappedFileAppender<T> wrap : writers.values()) {
+      closeCurrentWriter(wrap);
+      LOG.debug("Close file appender: {}, completeDataFiles: {}",
+          wrap.encryptedOutputFile.encryptingOutputFile().location(),
+          completeDataFiles.size());
+    }
+    this.writers.clear();
+  }
+
+  @Override
+  public List<DataFile> getCompleteFiles() {
+    if (completeDataFiles.size() > 0) {
+      return ImmutableList.copyOf(this.completeDataFiles);
+    } else {
+      return Collections.emptyList();
+    }
+  }
+
+  @Override
+  public void reset() {
+    this.completeDataFiles.clear();
+  }
+
+  private WrappedFileAppender<T> createWrappedFileAppender(PartitionKey 
partitionKey) {
+    EncryptedOutputFile outputFile = outputFileGetter.apply(partitionKey);
+    FileAppender<T> appender = 
factory.newAppender(outputFile.encryptingOutputFile(), fileFormat);
+    return new WrappedFileAppender<>(partitionKey, outputFile, appender);
+  }
+
+  private void closeCurrentWriter(WrappedFileAppender<T> wrap) throws 
IOException {
+    DataFile dataFile = closeFileAppender(wrap.fileAppender, 
wrap.encryptedOutputFile, spec, wrap.partitionKey);
+    completeDataFiles.add(dataFile);
+  }
+
+  private static class WrappedFileAppender<T> {
+    private final PartitionKey partitionKey;

Review comment:
       It's will be simpler to write the following logic with attached the 
`partitionKey` in WrappedFileAppender.




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