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



##########
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:
       Shouldn't this also ensure that all of the appenders are closed?




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