MapReduce启动时输入文件不存在报错的校验原创
# 1.1 场景描述
MapReduce 或其他大数据处理框架,启动时加载数据文件,如果文件不存在,就会报错且终止程序。
在实际场景中,有些文件确实会存在不存在的情况,且情况正常,那么为了不报错接着进行逻辑运算,可以在加载文件前进行文件是否存在的校验。
# 1.2 解决方案
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* @Auther:weichao
* @功能描述:
* @Version:1.0
*/
public class Test002 {
public static void main(String[] args) throws Exception {
getInputFiles(new Configuration());
}
private static List<String> getInputFiles(Configuration conf) throws Exception {
// 采用线程安全的List, 避免删除数据报错
List<String> inputFiles = new CopyOnWriteArrayList<>();
inputFiles.add("cosn://test-logs-666/ttdir/hehe/");
inputFiles.add("cosn://test-logs-666/ttdir/haha/");
// 校验文件是否存在
for (String inputFile : inputFiles) {
Path filePath = new Path(inputFile);
FileSystem fs = filePath.getFileSystem(conf);
if (fs.exists(filePath)) {
System.out.println("load path : " + inputFile);
} else {
inputFiles.remove(inputFile);
System.out.println("No such file or directory on COSN: " + inputFile);
}
}
return inputFiles;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
上次更新: 2024/06/28, 14:46:16
- 02
- 2025-03-28拍婚纱照 原创04-02
- 03
- 2024-04-05的晚上 原创04-01