docete commented on a change in pull request #9864: [FLINK-14254][table] Introduce FileSystemOutputFormat for batch URL: https://github.com/apache/flink/pull/9864#discussion_r336940493
########## File path: flink-table/flink-table-common/src/main/java/org/apache/flink/table/sink/filesystem/FileSystemFileCommitter.java ########## @@ -0,0 +1,264 @@ +/* + * 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.flink.table.sink.filesystem; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.core.fs.FileStatus; +import org.apache.flink.core.fs.FileSystem; +import org.apache.flink.core.fs.Path; +import org.apache.flink.table.api.TableException; +import org.apache.flink.util.Preconditions; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Default file system {@link FileCommitter} implementation. It just move all files to + * output path from temporary path. + * + * <p>NOTE: Here, we have no way to update catalog. + */ +@Internal +public class FileSystemFileCommitter extends FileCommitter { + + private static final Pattern PARTITION_NAME_PATTERN = Pattern.compile("([^/]+)=([^/]+)"); + + private final Path outputPath; + private final Map<String, String> staticPartitions; + private final String[] partitionColumns; + + public FileSystemFileCommitter( + boolean overwrite, + Path tmpPath, + Path outputPath, + Map<String, String> staticPartitions, + String[] partitionColumns) { + super(tmpPath, overwrite); + this.outputPath = outputPath; + this.staticPartitions = staticPartitions; + this.partitionColumns = partitionColumns; + } + + @Override + public void deletePath(Path taskTmpDir) throws Exception { + FileSystem.get(taskTmpDir.toUri()).delete(taskTmpDir, true); + } + + @Override + public void commit(long checkpointId) throws Exception { + FileSystem fs = FileSystem.get(temporaryPath.toUri()); + for (FileStatus taskStatus : fs.listStatus(temporaryPath)) { + String name = taskStatus.getPath().getName(); + if (name.startsWith("cp-")) { + long cpId = Long.parseLong(name.substring(3, name.length())); + + // commit paths that less than current checkpoint id. + if (cpId <= checkpointId) { + for (FileStatus status : fs.listStatus(taskStatus.getPath())) { + if (status.getPath().getName().startsWith("task-")) { + commitTaskOutputFiles(status.getPath()); + } + } + + // clean checkpoint path + deletePath(taskStatus.getPath()); + } + } + } + } + + /** + * Commit a task temporary path. + */ + private void commitTaskOutputFiles(Path path) throws Exception { + try { + boolean isPartitioned = partitionColumns.length > 0; + boolean isDynamicPartition = isPartitioned && partitionColumns.length != staticPartitions.size(); + + if (isPartitioned) { + if (isDynamicPartition) { + List<Tuple2<LinkedHashMap<String, String>, Path>> partitionSpecs = + searchPartSpecAndPaths(path, partitionColumns.length); + for (Tuple2<LinkedHashMap<String, String>, Path> spec : partitionSpecs) { + loadPartition(spec.f1, spec.f0); + } + } else { + LinkedHashMap<String, String> partSpec = new LinkedHashMap<>(); + for (String partCol : staticPartitions.keySet()) { + partSpec.put(partCol, staticPartitions.get(partCol)); + } + loadPartition(path, partSpec); + } + } else { + moveFiles(path, outputPath); + } + } finally { + deletePath(path); + } + } + + /** + * Load a single partition. + */ + private void loadPartition(Path srcDir, LinkedHashMap<String, String> partSpec) throws Exception { + moveFiles(srcDir, new Path(outputPath, makePartitionName(partSpec))); + } + + /** + * Moves all files under srcDir into destDir. Delete destDir all files first when overwrite. Review comment: Moves files from srcDir to destDir. Delete files in destDir first when overwrite. ---------------------------------------------------------------- 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] With regards, Apache Git Services
