Skip to content

Commit

Permalink
Spark: Add new actions entry point, SparkActions (apache#2473)
Browse files Browse the repository at this point in the history
  • Loading branch information
aokolnychyi authored Apr 15, 2021
1 parent 66f4683 commit 0a59a35
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 org.apache.iceberg.Table;
import org.apache.iceberg.actions.ActionsProvider;
import org.apache.iceberg.actions.ExpireSnapshots;
import org.apache.iceberg.actions.RemoveOrphanFiles;
import org.apache.iceberg.actions.RewriteManifests;
import org.apache.spark.sql.SparkSession;

abstract class BaseSparkActions implements ActionsProvider {

private final SparkSession spark;

protected BaseSparkActions(SparkSession spark) {
this.spark = spark;
}

protected SparkSession spark() {
return spark;
}

@Override
public RemoveOrphanFiles removeOrphanFiles(Table table) {
return new BaseRemoveOrphanFilesSparkAction(spark, table);
}

@Override
public RewriteManifests rewriteManifests(Table table) {
return new BaseRewriteManifestsSparkAction(spark, table);
}

@Override
public ExpireSnapshots expireSnapshots(Table table) {
return new BaseExpireSnapshotsSparkAction(spark, table);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 org.apache.iceberg.actions.ActionsProvider;
import org.apache.spark.sql.SparkSession;

/**
* An implementation of {@link ActionsProvider} for Spark.
* <p>
* This class is the primary API for interacting with actions in Spark that users should use
* to instantiate particular actions.
*/
public class SparkActions extends BaseSparkActions {

private SparkActions(SparkSession spark) {
super(spark);
}

public static SparkActions get(SparkSession spark) {
return new SparkActions(spark);
}

public static SparkActions get() {
return new SparkActions(SparkSession.active());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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 org.apache.iceberg.actions.ActionsProvider;
import org.apache.iceberg.actions.MigrateTable;
import org.apache.iceberg.actions.SnapshotTable;
import org.apache.iceberg.spark.Spark3Util;
import org.apache.iceberg.spark.Spark3Util.CatalogAndIdentifier;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.connector.catalog.CatalogPlugin;

/**
* An implementation of {@link ActionsProvider} for Spark.
* <p>
* This class is the primary API for interacting with actions in Spark that users should use
* to instantiate particular actions.
*/
public class SparkActions extends BaseSparkActions {

private SparkActions(SparkSession spark) {
super(spark);
}

public static SparkActions get(SparkSession spark) {
return new SparkActions(spark);
}

public static SparkActions get() {
return new SparkActions(SparkSession.active());
}

@Override
public SnapshotTable snapshotTable(String tableIdent) {
String ctx = "snapshot source";
CatalogPlugin defaultCatalog = spark().sessionState().catalogManager().currentCatalog();
CatalogAndIdentifier catalogAndIdent = Spark3Util.catalogAndIdentifier(ctx, spark(), tableIdent, defaultCatalog);
return new BaseSnapshotTableSparkAction(spark(), catalogAndIdent.catalog(), catalogAndIdent.identifier());
}

@Override
public MigrateTable migrateTable(String tableIdent) {
String ctx = "migrate target";
CatalogPlugin defaultCatalog = spark().sessionState().catalogManager().currentCatalog();
CatalogAndIdentifier catalogAndIdent = Spark3Util.catalogAndIdentifier(ctx, spark(), tableIdent, defaultCatalog);
return new BaseMigrateTableSparkAction(spark(), catalogAndIdent.catalog(), catalogAndIdent.identifier());
}
}

0 comments on commit 0a59a35

Please sign in to comment.