lw309637554 commented on a change in pull request #1810:
URL: https://github.com/apache/hudi/pull/1810#discussion_r465449064



##########
File path: 
hudi-sync/hudi-dla-sync/src/main/java/org/apache/hudi/dla/DLASyncTool.java
##########
@@ -0,0 +1,211 @@
+/*
+ * 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.dla;
+
+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.hudi.common.fs.FSUtils;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.dla.util.Utils;
+import org.apache.hudi.exception.HoodieException;
+import org.apache.hudi.exception.InvalidTableException;
+import org.apache.hudi.hadoop.HoodieParquetInputFormat;
+import org.apache.hudi.hadoop.realtime.HoodieParquetRealtimeInputFormat;
+import org.apache.hudi.hive.SchemaDifference;
+import org.apache.hudi.hive.util.HiveSchemaUtil;
+import org.apache.hudi.sync.common.AbstractSyncHoodieClient;
+import org.apache.hudi.sync.common.AbstractSyncTool;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Logger;
+import org.apache.parquet.schema.MessageType;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.stream.Collectors;
+
+/**
+ * Tool to sync a hoodie table with a dla table. Either use it as a api
+ * DLASyncTool.syncHoodieTable(DLASyncConfig) or as a command line java -cp 
hoodie-hive.jar DLASyncTool [args]
+ * <p>
+ * This utility will get the schema from the latest commit and will sync dla 
table schema Also this will sync the
+ * partitions incrementally (all the partitions modified since the last commit)
+ */
+@SuppressWarnings("WeakerAccess")
+public class DLASyncTool extends AbstractSyncTool {
+
+  private static final Logger LOG = LogManager.getLogger(DLASyncTool.class);
+  public static final String SUFFIX_SNAPSHOT_TABLE = "_rt";
+  public static final String SUFFIX_READ_OPTIMIZED_TABLE = "_ro";
+
+  private final DLASyncConfig cfg;
+  private final HoodieDLAClient hoodieDLAClient;
+  private final String snapshotTableName;
+  private final Option<String> roTableTableName;
+
+  public DLASyncTool(Properties properties, FileSystem fs) {
+    super(properties, fs);
+    this.hoodieDLAClient = new 
HoodieDLAClient(Utils.propertiesToConfig(properties), fs);
+    this.cfg = Utils.propertiesToConfig(properties);
+    switch (hoodieDLAClient.getTableType()) {
+      case COPY_ON_WRITE:
+        this.snapshotTableName = cfg.tableName;
+        this.roTableTableName = Option.empty();
+        break;
+      case MERGE_ON_READ:
+        this.snapshotTableName = cfg.tableName + SUFFIX_SNAPSHOT_TABLE;
+        this.roTableTableName = cfg.skipROSuffix ? Option.of(cfg.tableName) :
+            Option.of(cfg.tableName + SUFFIX_READ_OPTIMIZED_TABLE);
+        break;
+      default:
+        LOG.error("Unknown table type " + hoodieDLAClient.getTableType());
+        throw new InvalidTableException(hoodieDLAClient.getBasePath());
+    }
+  }
+
+  @Override
+  public void syncHoodieTable() {
+    try {
+      switch (hoodieDLAClient.getTableType()) {
+        case COPY_ON_WRITE:
+          syncHoodieTable(snapshotTableName, false);
+          break;
+        case MERGE_ON_READ:
+          // sync a RO table for MOR
+          syncHoodieTable(roTableTableName.get(), false);
+          // sync a RT table for MOR
+          syncHoodieTable(snapshotTableName, true);
+          break;
+        default:
+          LOG.error("Unknown table type " + hoodieDLAClient.getTableType());
+          throw new InvalidTableException(hoodieDLAClient.getBasePath());
+      }
+    } catch (RuntimeException re) {
+      LOG.error("Got runtime exception when dla syncing", re);
+    } finally {
+      hoodieDLAClient.close();
+    }
+  }
+
+  private void syncHoodieTable(String tableName, boolean 
useRealtimeInputFormat) {
+    LOG.info("Trying to sync hoodie table " + tableName + " with base path " + 
hoodieDLAClient.getBasePath()
+        + " of type " + hoodieDLAClient.getTableType());
+    // Check if the necessary table exists
+    boolean tableExists = hoodieDLAClient.doesTableExist(tableName);
+    // Get the parquet schema for this table looking at the latest commit
+    MessageType schema = hoodieDLAClient.getDataSchema();
+    // Sync schema if needed
+    syncSchema(tableName, tableExists, useRealtimeInputFormat, schema);
+
+    LOG.info("Schema sync complete. Syncing partitions for " + tableName);
+    // Get the last time we successfully synced partitions
+    Option<String> lastCommitTimeSynced = Option.empty();

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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to