在spark开发过程中,当输入路径为多个时,用逗号(,)来拼接路径,但是有时候会遇到,多个输入目录有个别路径下没有文件,这时候提交任务,会直接报错,job不能运行
解决问题:
1、根据报错信息找到相应代码,报错信息如下
Input Pattern ***** matches 0 files
代码位置:FileInputFormat类151行
Path p = arr$[i$];
FileSystem fs = p.getFileSystem(job);
FileStatus[] matches = fs.globStatus(p, inputFilter);
if (matches == null) {
errors.add(new IOException("Input path does not exist: " + p));
} else if (matches.length == 0) {
errors.add(new IOException("Input Pattern " + p + " matches 0 files"));
} else {
可以看到输入的路径文件为null时,抛出异常没问题,但是输入的文件为空时,同样抛出了异常,去掉这个异常即可正常运行。
修改方法:
重写FileInputFormat类
import com.google.common.base.Charsets;
import com.google.common.base.Stopwatch;
import com.google.common.collect.Iterables;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.CompressionCodecFactory;
import org.apache.hadoop.io.compress.SplittableCompressionCodec;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.mapreduce.security.TokenCache;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@InterfaceAudience.Public
@InterfaceStability.Stable
public class EmptiableTextInputFormat extends FileInputFormat<LongWritable