leaves12138 commented on code in PR #2368: URL: https://github.com/apache/incubator-paimon/pull/2368#discussion_r1406998389
########## paimon-hive/paimon-hive-catalog/src/main/java/org/apache/paimon/hive/migrate/HiveMigrator.java: ########## @@ -0,0 +1,334 @@ +/* + * 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.migrate; + +import org.apache.paimon.CoreOptions; +import org.apache.paimon.catalog.Identifier; +import org.apache.paimon.data.BinaryRow; +import org.apache.paimon.data.BinaryWriter; +import org.apache.paimon.fs.FileIO; +import org.apache.paimon.fs.FileStatus; +import org.apache.paimon.fs.Path; +import org.apache.paimon.hive.HiveCatalog; +import org.apache.paimon.io.DataFileMeta; +import org.apache.paimon.migrate.FileMetaUtils; +import org.apache.paimon.migrate.Migrator; +import org.apache.paimon.schema.Schema; +import org.apache.paimon.table.AbstractFileStoreTable; +import org.apache.paimon.table.AppendOnlyFileStoreTable; +import org.apache.paimon.table.BucketMode; +import org.apache.paimon.table.sink.CommitMessage; +import org.apache.paimon.types.DataField; +import org.apache.paimon.types.RowType; + +import org.apache.hadoop.hive.metastore.IMetaStoreClient; +import org.apache.hadoop.hive.metastore.api.FieldSchema; +import org.apache.hadoop.hive.metastore.api.Partition; +import org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.function.Predicate; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +import static org.apache.paimon.hive.HiveTypeUtils.toPaimonType; + +/** Migrate hive table to paimon table. */ +public class HiveMigrator implements Migrator { + + private static final Predicate<FileStatus> HIDDEN_PATH_FILTER = + p -> !p.getPath().getName().startsWith("_") && !p.getPath().getName().startsWith("."); + + private final FileIO fileIO; + private final HiveCatalog hiveCatalog; + private final IMetaStoreClient client; + private final String sourceDatabase; + private final String sourceTable; + private final String targetDatabase; + private final String targetTable; + private final Map<String, String> options; + + public HiveMigrator( + HiveCatalog hiveCatalog, + String sourceDatabase, + String sourceTable, + String targetDatabase, + String targetTable, + Map<String, String> options) { + this.hiveCatalog = hiveCatalog; + this.fileIO = hiveCatalog.fileIO(); + this.client = hiveCatalog.getHmsClient(); + this.sourceDatabase = sourceDatabase; + this.sourceTable = sourceTable; + this.targetDatabase = targetDatabase; + this.targetTable = targetTable; + this.options = options; + } + + public void executeMigrate(boolean sync) throws Exception { + if (!client.tableExists(sourceDatabase, sourceTable)) { + throw new RuntimeException("Source hive table does not exist"); + } + + Table sourceHiveTable = client.getTable(sourceDatabase, sourceTable); + Map<String, String> properties = new HashMap<>(sourceHiveTable.getParameters()); + checkPrimaryKey(); + + AbstractFileStoreTable paimonTable = + createPaimonTableIfNotExists( + client.getSchema(sourceDatabase, sourceTable), + sourceHiveTable.getPartitionKeys(), + properties); + checkPaimonTable(paimonTable); + + List<String> partitionsNames = + client.listPartitionNames(sourceDatabase, sourceTable, Short.MAX_VALUE); + checkCompatible(sourceHiveTable, paimonTable); + + List<MigrateTask> tasks = new ArrayList<>(); + if (partitionsNames.isEmpty()) { + tasks.add(importUnPartitionedTableTask(fileIO, sourceHiveTable, paimonTable)); + } else { + tasks.addAll( + importPartitionedTableTask( + client, fileIO, partitionsNames, sourceHiveTable, paimonTable)); + } + + if (sync) { + List<CommitMessage> commitMessages = new ArrayList<>(); + tasks.forEach(task -> commitMessages.add(task.get())); + paimonTable.newBatchWriteBuilder().newCommit().commit(commitMessages); + } else { + Queue<CommitMessage> commitMessages = new LinkedBlockingQueue<>(); + List<Future<?>> futures = new ArrayList<>(); + ExecutorService executors = + Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); Review Comment: done -- 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]
