leesf commented on code in PR #5532:
URL: https://github.com/apache/hudi/pull/5532#discussion_r873708484


##########
hudi-sync/hudi-adb-sync/src/main/java/org/apache/hudi/sync/adb/AdbSyncTool.java:
##########
@@ -0,0 +1,290 @@
+/*
+ * 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.hudi.sync.adb;
+
+import org.apache.hudi.common.config.TypedProperties;
+import org.apache.hudi.common.fs.FSUtils;
+import org.apache.hudi.common.model.HoodieFileFormat;
+import org.apache.hudi.common.model.HoodieTableType;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.hadoop.utils.HoodieInputFormatUtils;
+import org.apache.hudi.hive.SchemaDifference;
+import org.apache.hudi.hive.util.HiveSchemaUtil;
+import org.apache.hudi.sync.common.AbstractSyncHoodieClient.PartitionEvent;
+import 
org.apache.hudi.sync.common.AbstractSyncHoodieClient.PartitionEvent.PartitionEventType;
+import org.apache.hudi.sync.common.AbstractSyncTool;
+import org.apache.hudi.sync.common.util.ConfigUtils;
+
+import com.beust.jcommander.JCommander;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat;
+import org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe;
+import org.apache.parquet.schema.MessageType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Adb sync tool is mainly used to sync hoodie tables to Alibaba Cloud 
AnalyticDB(ADB),
+ * it can be used as API `AdbSyncTool.syncHoodieTable(AdbSyncConfig)` or as 
command
+ * line `java -cp hoodie-hive.jar AdbSyncTool [args]`
+ *
+ * <p>
+ * This utility will get the schema from the latest commit and will sync ADB 
table schema,
+ * incremental partitions will be synced as well.
+ */
+@SuppressWarnings("WeakerAccess")
+public class AdbSyncTool extends AbstractSyncTool {
+  private static final Logger LOG = LoggerFactory.getLogger(AdbSyncTool.class);
+
+  public static final String SUFFIX_SNAPSHOT_TABLE = "_rt";
+  public static final String SUFFIX_READ_OPTIMIZED_TABLE = "_ro";
+
+  private final AdbSyncConfig adbSyncConfig;
+  private final AbstractAdbSyncHoodieClient hoodieAdbClient;
+  private final String snapshotTableName;
+  private final Option<String> roTableTableName;
+  private static final byte[] LOCK = new byte[1];
+
+  public AdbSyncTool(TypedProperties props, Configuration conf, FileSystem fs) 
{
+    super(props, conf, fs);
+    this.adbSyncConfig = new AdbSyncConfig(props);
+    this.hoodieAdbClient = getHoodieAdbClient(adbSyncConfig, fs);
+    switch (hoodieAdbClient.getTableType()) {
+      case COPY_ON_WRITE:
+        this.snapshotTableName = adbSyncConfig.tableName;
+        this.roTableTableName = Option.empty();
+        break;
+      case MERGE_ON_READ:
+        this.snapshotTableName = adbSyncConfig.tableName + 
SUFFIX_SNAPSHOT_TABLE;
+        this.roTableTableName = adbSyncConfig.skipROSuffix ? 
Option.of(adbSyncConfig.tableName)
+            : Option.of(adbSyncConfig.tableName + SUFFIX_READ_OPTIMIZED_TABLE);
+        break;
+      default:
+        throw new HoodieAdbSyncException("Unknown table type:" + 
hoodieAdbClient.getTableType()
+            + ", basePath:" + hoodieAdbClient.getBasePath());
+    }
+  }
+
+  private AbstractAdbSyncHoodieClient getHoodieAdbClient(AdbSyncConfig 
adbSyncConfig, FileSystem fs) {
+    return new HoodieAdbJdbcClient(adbSyncConfig, fs);
+  }
+
+  @Override
+  public void syncHoodieTable() {
+    syncHoodieTable(true);
+  }
+
+  public void syncHoodieTable(boolean closeClient) {
+    try {
+      switch (hoodieAdbClient.getTableType()) {
+        case COPY_ON_WRITE:
+          syncHoodieTable(snapshotTableName, false, false);
+          break;
+        case MERGE_ON_READ:
+          // Sync a ro table for MOR table
+          syncHoodieTable(roTableTableName.get(), false, true);
+          // Sync a rt table for MOR table
+          if (!adbSyncConfig.skipRTSync) {
+            syncHoodieTable(snapshotTableName, true, false);
+          }
+          break;
+        default:
+          throw new HoodieAdbSyncException("Unknown table type:" + 
hoodieAdbClient.getTableType()
+              + ", basePath:" + hoodieAdbClient.getBasePath());
+      }
+    } catch (Exception re) {
+      throw new HoodieAdbSyncException("Sync hoodie table to ADB failed, 
tableName:" + adbSyncConfig.tableName, re);
+    } finally {
+      if (closeClient) {
+        hoodieAdbClient.close();

Review Comment:
   I think this logic can be removed, if closeClient is false, when will the 
hoodieAdbClient close?



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