| /* |
| * 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.file.source.reader; |
| |
| import static org.apache.parquet.avro.AvroReadSupport.READ_INT96_AS_FIXED; |
| |
| import org.apache.seatunnel.api.table.type.SeaTunnelRowType; |
| import org.apache.seatunnel.connectors.seatunnel.file.config.HadoopConf; |
| import org.apache.seatunnel.connectors.seatunnel.file.exception.FilePluginException; |
| |
| import lombok.extern.slf4j.Slf4j; |
| import org.apache.hadoop.conf.Configuration; |
| import org.apache.hadoop.fs.CommonConfigurationKeys; |
| import org.apache.hadoop.fs.FileStatus; |
| import org.apache.hadoop.fs.FileSystem; |
| import org.apache.hadoop.fs.Path; |
| |
| import java.io.IOException; |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| @Slf4j |
| public abstract class AbstractReadStrategy implements ReadStrategy { |
| protected HadoopConf hadoopConf; |
| protected SeaTunnelRowType seaTunnelRowType; |
| |
| @Override |
| public void init(HadoopConf conf) { |
| this.hadoopConf = conf; |
| } |
| |
| @Override |
| public void setSeaTunnelRowTypeInfo(SeaTunnelRowType seaTunnelRowType) { |
| this.seaTunnelRowType = seaTunnelRowType; |
| } |
| |
| @Override |
| public Configuration getConfiguration(HadoopConf hadoopConf) { |
| Configuration configuration = new Configuration(); |
| configuration.set(READ_INT96_AS_FIXED, "true"); |
| if (hadoopConf != null) { |
| configuration.set(CommonConfigurationKeys.FS_DEFAULT_NAME_KEY, hadoopConf.getHdfsNameKey()); |
| configuration.set("fs.hdfs.impl", hadoopConf.getFsHdfsImpl()); |
| hadoopConf.setExtraOptionsForConfiguration(configuration); |
| } |
| return configuration; |
| } |
| |
| Configuration getConfiguration() throws FilePluginException { |
| if (null == hadoopConf) { |
| log.info("Local file reader didn't need hadoopConf"); |
| } |
| return getConfiguration(hadoopConf); |
| } |
| |
| boolean checkFileType(String path) { |
| return true; |
| } |
| |
| @Override |
| public List<String> getFileNamesByPath(HadoopConf hadoopConf, String path) throws IOException { |
| Configuration configuration = getConfiguration(hadoopConf); |
| List<String> fileNames = new ArrayList<>(); |
| FileSystem hdfs = FileSystem.get(configuration); |
| Path listFiles = new Path(path); |
| FileStatus[] stats = hdfs.listStatus(listFiles); |
| for (FileStatus fileStatus : stats) { |
| if (fileStatus.isDirectory()) { |
| fileNames.addAll(getFileNamesByPath(hadoopConf, fileStatus.getPath().toString())); |
| continue; |
| } |
| if (fileStatus.isFile()) { |
| // filter '_SUCCESS' file |
| if (!fileStatus.getPath().getName().equals("_SUCCESS")) { |
| fileNames.add(fileStatus.getPath().toString()); |
| } |
| } |
| } |
| return fileNames; |
| } |
| |
| } |