Alibaba-HZY commented on code in PR #918:
URL: https://github.com/apache/incubator-paimon/pull/918#discussion_r1172490372


##########
paimon-hive/paimon-hive-connector-common/src/main/java/org/apache/paimon/hive/mapred/PaimonOutputCommitter.java:
##########
@@ -0,0 +1,284 @@
+/*
+ * 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.paimon.hive.mapred;
+
+import org.apache.paimon.fs.FileIO;
+import org.apache.paimon.fs.Path;
+import org.apache.paimon.shade.guava30.com.google.common.collect.ImmutableMap;
+import org.apache.paimon.table.FileStoreTable;
+import org.apache.paimon.table.sink.BatchTableCommit;
+import org.apache.paimon.table.sink.BatchTableWrite;
+import org.apache.paimon.table.sink.BatchWriteBuilder;
+import org.apache.paimon.table.sink.CommitMessage;
+
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.JobContext;
+import org.apache.hadoop.mapred.OutputCommitter;
+import org.apache.hadoop.mapred.TaskAttemptContext;
+import org.apache.hadoop.mapred.TaskAttemptID;
+import org.apache.hadoop.mapreduce.JobID;
+import org.apache.hadoop.mapreduce.TaskType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import static org.apache.paimon.hive.utils.HiveUtils.createFileStoreTable;
+
+/** A Paimon table committer for adding data files to the Paimon table. */
+public class PaimonOutputCommitter extends OutputCommitter {
+
+    private static final String PRE_COMMIT = ".preCommit";
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(PaimonOutputCommitter.class);
+
+    @Override
+    public void setupJob(JobContext jobContext) throws IOException {}
+
+    @Override
+    public void setupTask(TaskAttemptContext taskAttemptContext) throws 
IOException {}
+
+    @Override
+    public boolean needsTaskCommit(TaskAttemptContext taskAttemptContext) 
throws IOException {
+        // We need to commit if this is the last phase of a MapReduce process
+        return TaskType.REDUCE.equals(
+                        
taskAttemptContext.getTaskAttemptID().getTaskID().getTaskType())
+                || taskAttemptContext.getJobConf().getNumReduceTasks() == 0;
+    }
+
+    @Override
+    public void commitTask(TaskAttemptContext taskAttemptContext) throws 
IOException {
+
+        TaskAttemptID attemptID = taskAttemptContext.getTaskAttemptID();
+        JobConf jobConf = taskAttemptContext.getJobConf();
+        FileStoreTable table = createFileStoreTable(jobConf);
+
+        Map<String, PaimonRecordWriter> writers =
+                Optional.ofNullable(PaimonRecordWriter.getWriters(attemptID))
+                        .orElseGet(
+                                () -> {
+                                    LOG.info(
+                                            "CommitTask found no writers for 
output table: {}, attemptID: {}",
+                                            table.name(),
+                                            attemptID);
+                                    return ImmutableMap.of();
+                                });
+        PaimonRecordWriter writer = writers.get(table.name());
+        if (writer != null) {
+
+            try (BatchTableWrite batchTableWrite = writer.batchTableWrite()) {
+                List<CommitMessage> commitTables = 
batchTableWrite.prepareCommit();
+                createPreCommitFile(
+                        commitTables,
+                        generatePreCommitFileLocation(
+                                table.location().getPath(),
+                                attemptID.getJobID(),
+                                attemptID.getTaskID().getId()),
+                        table.fileIO());
+            } catch (Exception e) {
+                LOG.error(
+                        "CommitTask prepareCommit error for specific table: 
{}, attemptID: {}",
+                        table.name(),
+                        attemptID);
+                throw new RuntimeException(e);
+            }
+        } else {
+            LOG.info(
+                    "CommitTask found no writer for specific table: {}, 
attemptID: {}",
+                    table.name(),
+                    attemptID);
+        }
+        PaimonRecordWriter.removeWriters(attemptID);
+    }
+
+    @Override
+    public void abortTask(TaskAttemptContext taskAttemptContext) throws 
IOException {
+        Map<String, PaimonRecordWriter> writers =
+                
PaimonRecordWriter.removeWriters(taskAttemptContext.getTaskAttemptID());
+
+        // close writer and delete files
+        if (writers != null) {
+            for (PaimonRecordWriter writer : writers.values()) {
+                writer.close(true);
+            }
+        }
+    }
+
+    @Override
+    public void commitJob(JobContext jobContext) throws IOException {
+        JobConf jobConf = jobContext.getJobConf();
+
+        long startTime = System.currentTimeMillis();
+        LOG.info("CommitJob {} has started", jobContext.getJobID());
+        FileStoreTable table = createFileStoreTable(jobConf);
+
+        if (table != null) {
+            BatchWriteBuilder batchWriteBuilder = table.newBatchWriteBuilder();
+            List<List<CommitMessage>> commitMessagesList =
+                    getAllPreCommitMessage(table.location().getPath(), 
jobContext, table.fileIO());
+            for (List<CommitMessage> commitTables : commitMessagesList) {
+                try (BatchTableCommit batchTableCommit = 
batchWriteBuilder.newCommit()) {

Review Comment:
   yes We only need one commit
    



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to