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



##########
File path: 
spark3/src/main/java/org/apache/iceberg/spark/actions/BaseSnapshotTableSparkAction.java
##########
@@ -0,0 +1,183 @@
+/*
+ * 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.spark.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.actions.BaseSnapshotTableActionResult;
+import org.apache.iceberg.actions.SnapshotTable;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.spark.JobGroupInfo;
+import org.apache.iceberg.spark.Spark3Util;
+import org.apache.iceberg.spark.Spark3Util.CatalogAndIdentifier;
+import org.apache.iceberg.spark.SparkTableUtil;
+import org.apache.iceberg.spark.source.StagedSparkTable;
+import org.apache.spark.sql.SparkSession;
+import org.apache.spark.sql.catalyst.TableIdentifier;
+import org.apache.spark.sql.connector.catalog.CatalogPlugin;
+import org.apache.spark.sql.connector.catalog.Identifier;
+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.
+ */
+public class BaseSnapshotTableSparkAction
+    extends BaseTableMigrationSparkAction<SnapshotTable, SnapshotTable.Result>
+    implements SnapshotTable {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(BaseSnapshotTableSparkAction.class);
+
+  private String destTableLocation = null;
+
+  public BaseSnapshotTableSparkAction(SparkSession spark, CatalogPlugin 
sourceCatalog, Identifier sourceTableIdent,
+                                      CatalogPlugin destCatalog, Identifier 
destTableIdent) {
+    super(spark, sourceCatalog, sourceTableIdent, destCatalog, destTableIdent);
+  }
+
+  @Override
+  protected SnapshotTable self() {
+    return this;
+  }
+
+  @Override
+  public SnapshotTable as(String ident) {
+    String ctx = "snapshot destination";
+    CatalogPlugin defaultCatalog = 
spark().sessionState().catalogManager().currentCatalog();
+    CatalogAndIdentifier catalogAndIdent = 
Spark3Util.catalogAndIdentifier(ctx, spark(), ident, defaultCatalog);
+    setDestCatalogAndIdent(catalogAndIdent.catalog(), 
catalogAndIdent.identifier());
+    return this;
+  }
+
+  @Override
+  public SnapshotTable tableProperties(Map<String, String> properties) {
+    setProperties(properties);
+    return this;
+  }
+
+  @Override
+  public SnapshotTable tableProperty(String property, String value) {
+    setProperty(property, value);
+    return this;
+  }
+
+  @Override
+  public SnapshotTable.Result execute() {
+    JobGroupInfo info = newJobGroupInfo("SNAPSHOT-TABLE", "SNAPSHOT-TABLE");
+    return withJobGroupInfo(info, this::doExecute);
+  }
+
+  private SnapshotTable.Result doExecute() {
+    Preconditions.checkArgument(destCatalog() != null && destTableIdent() != 
null,
+        "The destination catalog and identifier cannot be null. " +
+        "Make sure to configure the action with a valid destination table 
identifier via the `as` method.");
+
+    LOG.info("Staging a new Iceberg table {} as a snapshot of {}", 
destTableIdent(), sourceTableIdent());
+    StagedSparkTable stagedTable = stageDestTable();
+    Table icebergTable = stagedTable.table();
+
+    // TODO: Check the dest table location does not overlap with the source 
table location
+
+    boolean threw = true;
+    try {
+      LOG.info("Ensuring {} has a valid name mapping", destTableIdent());
+      ensureNameMappingPresent(icebergTable);
+
+      TableIdentifier v1TableIdent = v1SourceTable().identifier();
+      String stagingLocation = getMetadataLocation(icebergTable);
+      LOG.info("Generating Iceberg metadata for {} in {}", destTableIdent(), 
stagingLocation);
+      SparkTableUtil.importSparkTable(spark(), v1TableIdent, icebergTable, 
stagingLocation);
+
+      LOG.info("Committing staged changes to {}", destTableIdent());
+      stagedTable.commitStagedChanges();
+      threw = false;
+    } finally {
+      if (threw) {
+        LOG.error("Error when populating the staged table with metadata, 
aborting changes");
+
+        try {
+          stagedTable.abortStagedChanges();
+        } catch (Exception abortException) {
+          LOG.error("Cannot abort staged changes", abortException);
+        }
+      }
+    }
+
+    Snapshot snapshot = icebergTable.currentSnapshot();
+    long importedDataFilesCount = 
Long.parseLong(snapshot.summary().get(SnapshotSummary.TOTAL_DATA_FILES_PROP));
+    LOG.info("Successfully loaded Iceberg metadata for {} files to {}", 
importedDataFilesCount, destTableIdent());
+    return new BaseSnapshotTableActionResult(importedDataFilesCount);
+  }
+
+  @Override
+  protected Map<String, String> targetTableProps() {

Review comment:
       Here as well. No changes from #2420.




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