JingsongLi commented on code in PR #918:
URL: https://github.com/apache/incubator-paimon/pull/918#discussion_r1172346481


##########
paimon-hive/paimon-hive-connector-common/src/main/java/org/apache/paimon/hive/HiveDeserializer.java:
##########
@@ -0,0 +1,307 @@
+/*
+ * 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;
+
+import org.apache.paimon.data.GenericRow;
+import org.apache.paimon.data.InternalArray;
+import org.apache.paimon.data.InternalMap;
+import org.apache.paimon.data.InternalRow;
+import org.apache.paimon.hive.objectinspector.HivePaimonArray;
+import org.apache.paimon.hive.objectinspector.WriteableObjectInspector;
+import org.apache.paimon.shade.guava30.com.google.common.collect.Lists;
+import org.apache.paimon.shade.guava30.com.google.common.collect.Maps;
+import org.apache.paimon.types.ArrayType;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.MapType;
+import org.apache.paimon.types.RowType;
+import org.apache.paimon.utils.Preconditions;
+
+import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.StructField;
+import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/** A deserializer to deserialize hive objects to {@link InternalRow}. */
+public class HiveDeserializer {
+    private FieldDeserializer fieldDeserializer;

Review Comment:
   final



##########
paimon-hive/paimon-hive-connector-common/src/main/java/org/apache/paimon/hive/SchemaVisitor.java:
##########
@@ -0,0 +1,113 @@
+/*
+ * 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;
+
+import org.apache.paimon.shade.guava30.com.google.common.collect.Lists;
+import org.apache.paimon.types.ArrayType;
+import org.apache.paimon.types.DataField;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.MapType;
+import org.apache.paimon.types.RowType;
+
+import java.util.List;
+
+/**
+ * SchemaVisitor to visitor the sourceInspector and writerInspector to get the 
FieldDeserializer.
+ */
+public abstract class SchemaVisitor<P, R> {

Review Comment:
   replace this with `DataTypeDefaultVisitor` or `DataTypeVisitor`?



##########
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()) {
+                    batchTableCommit.commit(commitTables);
+                } catch (Exception e) {
+                    throw new RuntimeException(e);
+                }
+            }
+            deleteTemporaryFile(
+                    jobContext,
+                    generateJobLocation(table.location().getPath(), 
jobContext.getJobID()));
+        } else {
+            LOG.info("CommitJob not found table, Skipping job commit.");
+        }
+
+        LOG.info(
+                "Commit took {} ms for job {}",
+                System.currentTimeMillis() - startTime,
+                jobContext.getJobID());
+    }
+
+    @Override
+    public void abortJob(JobContext jobContext, int status) throws IOException 
{
+        FileStoreTable table = createFileStoreTable(jobContext.getJobConf());
+        if (table != null) {
+
+            LOG.info("AbortJob {} has started", jobContext.getJobID());
+            List<List<CommitMessage>> commitMessagesList =
+                    getAllPreCommitMessage(table.location().getPath(), 
jobContext, table.fileIO());
+            BatchWriteBuilder batchWriteBuilder = table.newBatchWriteBuilder();
+            for (List<CommitMessage> commitTables : commitMessagesList) {
+                try (BatchTableCommit batchTableCommit = 
batchWriteBuilder.newCommit()) {
+                    batchTableCommit.abort(commitTables);

Review Comment:
   Only one abort is OK



##########
paimon-hive/paimon-hive-connector-common/src/test/java/org/apache/paimon/hive/TestPaimonStorageHandlerUseMR.java:
##########
@@ -0,0 +1,1033 @@
+/*
+ * 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;
+
+import org.apache.paimon.CoreOptions;
+import org.apache.paimon.WriteMode;
+import org.apache.paimon.catalog.Identifier;
+import org.apache.paimon.data.BinaryString;
+import org.apache.paimon.data.Decimal;
+import org.apache.paimon.data.GenericArray;
+import org.apache.paimon.data.GenericMap;
+import org.apache.paimon.data.GenericRow;
+import org.apache.paimon.data.InternalRow;
+import org.apache.paimon.hive.mapred.PaimonOutputFormat;
+import org.apache.paimon.hive.runner.PaimonEmbeddedHiveRunner;
+import org.apache.paimon.options.CatalogOptions;
+import org.apache.paimon.options.Options;
+import org.apache.paimon.table.Table;
+import org.apache.paimon.table.sink.StreamTableCommit;
+import org.apache.paimon.table.sink.StreamTableWrite;
+import org.apache.paimon.table.sink.StreamWriteBuilder;
+import org.apache.paimon.types.DataField;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.DataTypes;
+import org.apache.paimon.types.RowKind;
+import org.apache.paimon.types.RowType;
+import org.apache.paimon.utils.StringUtils;
+
+import com.klarna.hiverunner.HiveShell;
+import com.klarna.hiverunner.annotations.HiveSQL;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ThreadLocalRandom;
+
+import static org.apache.paimon.hive.FileStoreTestUtils.DATABASE_NAME;
+import static org.apache.paimon.hive.FileStoreTestUtils.TABLE_NAME;
+import static 
org.apache.paimon.hive.RandomGenericRowDataGenerator.randomBigDecimal;
+import static org.assertj.core.api.Assertions.assertThat;
+
+/** IT cases for {@link PaimonStorageHandler} and {@link PaimonOutputFormat}. 
*/
+@RunWith(PaimonEmbeddedHiveRunner.class)
+public class TestPaimonStorageHandlerUseMR {

Review Comment:
   `HiveReadWriteITCase`?



##########
paimon-hive/paimon-hive-connector-common/src/main/java/org/apache/paimon/hive/PaimonSerDe.java:
##########
@@ -39,11 +42,18 @@
 public class PaimonSerDe extends AbstractSerDe {
 
     private PaimonInternalRowObjectInspector inspector;
+    private HiveSchema tableSchema;
+
+    private RowDataContainer rowData = new RowDataContainer();
+
+    private Map<ObjectInspector, HiveDeserializer> deserializers =

Review Comment:
   final.
   Note you have some fields without final.



##########
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:
   Why there are multiple commits?
   It is only a job?  There should be just 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