sunxiaojian commented on code in PR #8840:
URL: https://github.com/apache/seatunnel/pull/8840#discussion_r1976854098


##########
seatunnel-connectors-v2/connector-iceberg/src/main/java/org/apache/seatunnel/connectors/seatunnel/iceberg/compaction/IcebergCompactionHandler.java:
##########
@@ -0,0 +1,220 @@
+/*
+ * 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.seatunnel.connectors.seatunnel.iceberg.compaction;
+
+import org.apache.seatunnel.shade.com.google.common.collect.Iterables;
+import org.apache.seatunnel.shade.com.google.common.collect.Lists;
+
+import org.apache.seatunnel.api.source.Collector;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.connectors.seatunnel.iceberg.IcebergTableLoader;
+import 
org.apache.seatunnel.connectors.seatunnel.iceberg.sink.writer.RecordWriter;
+import 
org.apache.seatunnel.connectors.seatunnel.iceberg.sink.writer.WriteResult;
+import 
org.apache.seatunnel.connectors.seatunnel.iceberg.source.split.IcebergFileScanTaskSplit;
+
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.RewriteFiles;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.exceptions.CommitStateUnknownException;
+import org.apache.iceberg.io.CloseableIterator;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.util.Tasks;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+@Slf4j
+public class IcebergCompactionHandler {
+
+    public static final String COMPACTION_ACTION_MESSAGE = "compaction_end";
+    public static final String CURRENT_SNAPSHOT_ID = "current_snapshot_id";
+    public static final String DELETED_FILES = "deleted_files";
+
+    private final Table table;
+    private final FileIO fileIO;
+    private final RecordWriter writer;
+
+    public IcebergCompactionHandler(IcebergTableLoader icebergTableLoader, 
RecordWriter writer) {
+        this.table = icebergTableLoader.loadTable();
+        this.fileIO = this.table.io();
+        this.writer = writer;
+    }
+
+    public static void emitCompactionEndRecord(
+            Collector<SeaTunnelRow> output, List<IcebergFileScanTaskSplit> 
finishedSplits) {
+        if (finishedSplits.isEmpty()) {
+            log.warn("No compaction split, skip it.");
+            return;
+        }
+        List<String> deletedFiles =
+                finishedSplits.stream()
+                        .map(split -> split.getTask().file().path().toString())
+                        .collect(Collectors.toList());
+
+        Optional<IcebergFileScanTaskSplit> firstSplit = 
finishedSplits.stream().findFirst();
+        if (!firstSplit.isPresent()) {
+            log.warn("Failed to find any splits, skipping compaction end 
record emission.");
+            return;
+        }
+        IcebergFileScanTaskSplit oneSplit = firstSplit.get();
+        SeaTunnelRow compactionEndRecord = new SeaTunnelRow(0);
+        compactionEndRecord.setTableId(oneSplit.getTablePath().getFullName());
+
+        Map<String, Object> options = new HashMap<>();
+        options.put(COMPACTION_ACTION_MESSAGE, COMPACTION_ACTION_MESSAGE);
+        options.put(CURRENT_SNAPSHOT_ID, oneSplit.getCurrentSnapshotId());
+        options.put(DELETED_FILES, deletedFiles);
+        compactionEndRecord.setOptions(options);
+        output.collect(compactionEndRecord);

Review Comment:
   > why not do it only in sink? We can read data and do file compaction in 
sink, it's more useful.
   
   It's not appropriate to perform compression during the task execution.



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

To unsubscribe, e-mail: commits-unsubscr...@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to