rdblue commented on a change in pull request #894: Spark: Implement an action 
to remove orphan files
URL: https://github.com/apache/incubator-iceberg/pull/894#discussion_r406832784
 
 

 ##########
 File path: spark/src/main/java/org/apache/iceberg/RemoveOrphanFilesAction.java
 ##########
 @@ -0,0 +1,279 @@
+/*
+ * 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;
+
+import com.google.common.collect.Lists;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Consumer;
+import java.util.function.Predicate;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileStatus;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.iceberg.exceptions.RuntimeIOException;
+import org.apache.iceberg.hadoop.HiddenPathFilter;
+import org.apache.iceberg.util.Tasks;
+import org.apache.spark.api.java.JavaRDD;
+import org.apache.spark.api.java.JavaSparkContext;
+import org.apache.spark.api.java.function.FlatMapFunction;
+import org.apache.spark.broadcast.Broadcast;
+import org.apache.spark.sql.Column;
+import org.apache.spark.sql.Dataset;
+import org.apache.spark.sql.Encoders;
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.SparkSession;
+import org.apache.spark.util.SerializableConfiguration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * An action that removes orphan metadata and data files by listing a given 
location and comparing
+ * the actual files in that location with data and metadata files referenced 
by all valid snapshots.
+ * The location must be accessible for listing via the Hadoop {@link 
FileSystem}.
+ * <p>
+ * By default, this action cleans up the table location returned by {@link 
Table#location()} and
+ * removes unreachable files that are older than 3 days using {@link 
Table#io()}. The behavior can be modified
+ * by passing a custom location to {@link #location} and a custom timestamp to 
{@link #olderThan(long)}.
+ * For example, someone might point this action to the data folder to clean up 
only orphan data files.
+ * In addition, there is a way to configure an alternative delete method via 
{@link #deleteWith(Consumer)}.
+ * <p>
+ * <em>Note:</em> It is dangerous to call this action with a short retention 
interval as it might corrupt
+ * the state of the table if another operation is writing at the same time.
+ */
+public class RemoveOrphanFilesAction implements Action<List<String>> {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(RemoveOrphanFilesAction.class);
+
+  private final SparkSession spark;
+  private final JavaSparkContext sparkContext;
+  private final SerializableConfiguration hadoopConf;
+  private final int partitionDiscoveryParallelism;
+  private final Table table;
+  private final TableOperations ops;
+
+  private String location = null;
+  private long olderThanTimestamp = System.currentTimeMillis() - 
TimeUnit.DAYS.toMillis(3);
+  private Consumer<String> deleteFunc = new Consumer<String>() {
 
 Review comment:
   Nit: we should be able to write this a `table.io()::deleteFile`

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


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to