aokolnychyi commented on a change in pull request #1525:
URL: https://github.com/apache/iceberg/pull/1525#discussion_r538809469



##########
File path: 
spark3/src/main/java/org/apache/iceberg/actions/Spark3MigrateAction.java
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.iceberg.actions;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.SnapshotSummary;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableProperties;
+import org.apache.iceberg.spark.SparkSessionCatalog;
+import org.apache.iceberg.spark.SparkTableUtil;
+import org.apache.iceberg.spark.source.SparkTable;
+import org.apache.spark.sql.SparkSession;
+import org.apache.spark.sql.catalyst.TableIdentifier;
+import org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException;
+import org.apache.spark.sql.catalyst.analysis.NoSuchTableException;
+import org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException;
+import org.apache.spark.sql.connector.catalog.CatalogPlugin;
+import org.apache.spark.sql.connector.catalog.Identifier;
+import org.apache.spark.sql.connector.catalog.StagedTable;
+import org.apache.spark.sql.connector.catalog.TableCatalog;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import scala.Some;
+import scala.collection.JavaConverters;
+
+/**
+ * Takes a Spark table in the sourceCatalog and attempts to transform it into 
an Iceberg
+ * Table in the same location with the same identifier. Once complete the 
identifier which
+ * previously referred to a non-iceberg table will refer to the newly migrated 
iceberg
+ * table.
+ */
+class Spark3MigrateAction extends Spark3CreateAction {
+  private static final Logger LOG = 
LoggerFactory.getLogger(Spark3MigrateAction.class);
+  private static final String BACKUP_SUFFIX = "_BACKUP_";
+
+  Spark3MigrateAction(SparkSession spark, CatalogPlugin sourceCatalog, 
Identifier sourceTableName) {
+    super(spark, sourceCatalog, sourceTableName, sourceCatalog, 
sourceTableName);
+  }
+
+  @Override
+  public Long execute() {
+    Table icebergTable;
+
+    // Move source table to a new name, halting all modifications and allowing 
us to stage
+    // the creation of a new Iceberg table in its place
+    String backupName = sourceTableName().name() + BACKUP_SUFFIX;
+    Identifier backupIdentifier = Identifier.of(sourceTableName().namespace(), 
backupName);
+    try {
+      destCatalog().renameTable(sourceTableName(), backupIdentifier);
+    } catch (NoSuchTableException e) {
+      throw new IllegalArgumentException("Cannot find table to migrate", e);
+    } catch (TableAlreadyExistsException e) {
+      throw new IllegalArgumentException("Cannot rename migration source to 
backup name", e);
+    }
+
+    StagedTable stagedTable = null;
+    boolean threw = true;
+    try {
+      Map<String, String> newTableProperties = new HashMap<>();
+      newTableProperties.put(TableCatalog.PROP_PROVIDER, "iceberg");

Review comment:
       I think this would work well with `targetTableProps` proposed 
[here](https://github.com/apache/iceberg/pull/1525/files#r538469312).




----------------------------------------------------------------
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:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to