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



##########
File path: 
spark3/src/main/java/org/apache/iceberg/actions/Spark3SnapshotAction.java
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.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.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+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.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.collection.JavaConverters;
+
+/**
+ * Creates a new Iceberg table based on a source Spark table. The new Iceberg 
table will
+ * have a different data and metadata directory allowing it to exist 
independently of the
+ * source table.
+ */
+class Spark3SnapshotAction extends Spark3CreateAction {
+  private static final Logger LOG = 
LoggerFactory.getLogger(Spark3SnapshotAction.class);
+
+  private final String destTableLocation;
+
+  Spark3SnapshotAction(SparkSession spark, CatalogPlugin sourceCatalog,
+                       Identifier sourceTableName, CatalogPlugin destCatalog,
+                       Identifier destTableName) {
+    super(spark, sourceCatalog, sourceTableName, destCatalog, destTableName);
+    // Cannot check if the table location that would be generated by the 
catalog will match the source
+    this.destTableLocation = null;
+  }
+
+  Spark3SnapshotAction(SparkSession spark, CatalogPlugin sourceCatalog,
+                       Identifier sourceTableName, CatalogPlugin destCatalog,
+                       Identifier destTableName, String destTableLocation) {
+    super(spark, sourceCatalog, sourceTableName, destCatalog, destTableName);
+    
Preconditions.checkArgument(!sourceTableLocation().equals(destTableLocation),
+        "Cannot create snapshot where destination location is the same as the 
source location. This" +
+            "would cause a mixing of original table created and snapshot 
created files.");
+    this.destTableLocation = destTableLocation;
+  }
+
+  @Override
+  public Long execute() {
+    StagedTable stagedTable;
+    Table icebergTable;
+
+    try {
+      stagedTable = destCatalog().stageCreate(destTableName(), 
v1SourceTable().schema(),
+          sourcePartitionSpec(), buildPropertyMap());
+      icebergTable = ((SparkTable) stagedTable).table();
+
+      if 
(!icebergTable.properties().containsKey(TableProperties.DEFAULT_NAME_MAPPING)) {
+        applyDefaultTableNameMapping(icebergTable);
+      }
+    } catch (TableAlreadyExistsException taeException) {
+      throw new IllegalArgumentException("Cannot create snapshot because a 
table already exists with that name",
+          taeException);
+    } catch (NoSuchNamespaceException nsnException) {
+      throw new IllegalArgumentException("Cannot create snapshot because the 
namespace given does not exist",
+          nsnException);
+    }
+
+    try {
+      String stagingLocation = icebergTable.location() + "/" + 
ICEBERG_METADATA_FOLDER;
+      LOG.info("Beginning snapshot of {} to {} using metadata location {}", 
sourceTableName(), destTableName(),
+          stagingLocation);
+
+      TableIdentifier v1TableIdentifier = v1SourceTable().identifier();
+      SparkTableUtil.importSparkTable(spark(), v1TableIdentifier, 
icebergTable, stagingLocation);
+      stagedTable.commitStagedChanges();
+
+    } catch (Exception e) {
+      LOG.error("Error when attempting to commit snapshot changes, rolling 
back", e);
+      if (stagedTable != null) {
+        stagedTable.abortStagedChanges();
+      }
+      throw e;
+    }
+
+    long numMigratedFiles;
+    Snapshot snapshot = icebergTable.currentSnapshot();
+    numMigratedFiles = 
Long.valueOf(snapshot.summary().get(SnapshotSummary.TOTAL_DATA_FILES_PROP));
+    LOG.info("Successfully loaded Iceberg metadata for {} files", 
numMigratedFiles);
+    return numMigratedFiles;
+  }
+
+  private Map<String, String> buildPropertyMap() {
+    ImmutableMap.Builder<String, String> propBuilder = ImmutableMap.<String, 
String>builder()

Review comment:
       Sounds good to me




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