| 41 | public class MergeFiles { |
| 42 | |
| 43 | public static void main(Configuration conf, String[] args) throws Exception { |
| 44 | Options opts = createOptions(); |
| 45 | CommandLine cli = new DefaultParser().parse(opts, args); |
| 46 | HelpFormatter formatter = new HelpFormatter(); |
| 47 | if (cli.hasOption('h')) { |
| 48 | formatter.printHelp("merge", opts); |
| 49 | return; |
| 50 | } |
| 51 | String outputFilename = cli.getOptionValue("output"); |
| 52 | if (outputFilename == null || outputFilename.isEmpty()) { |
| 53 | System.err.println("output filename is null"); |
| 54 | formatter.printHelp("merge", opts); |
| 55 | return; |
| 56 | } |
| 57 | boolean ignoreExtension = cli.hasOption("ignoreExtension"); |
| 58 | |
| 59 | List<Path> inputFiles = new ArrayList<>(); |
| 60 | OrcFile.WriterOptions writerOptions = OrcFile.writerOptions(conf); |
| 61 | |
| 62 | String[] files = cli.getArgs(); |
| 63 | for (String root : files) { |
| 64 | Path rootPath = new Path(root); |
| 65 | FileSystem fs = rootPath.getFileSystem(conf); |
| 66 | for (RemoteIterator<LocatedFileStatus> itr = fs.listFiles(rootPath, true); itr.hasNext(); ) { |
| 67 | LocatedFileStatus status = itr.next(); |
| 68 | if (status.isFile() && (ignoreExtension || status.getPath().getName().endsWith(".orc"))) { |
| 69 | inputFiles.add(status.getPath()); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | if (inputFiles.isEmpty()) { |
| 74 | System.err.println("No files found."); |
| 75 | System.exit(1); |
| 76 | } |
| 77 | |
| 78 | List<Path> mergedFiles = OrcFile.mergeFiles( |
| 79 | new Path(outputFilename), writerOptions, inputFiles); |
| 80 | |
| 81 | List<Path> unSuccessMergedFiles = new ArrayList<>(); |
| 82 | if (mergedFiles.size() != inputFiles.size()) { |
| 83 | Set<Path> mergedFilesSet = new HashSet<>(mergedFiles); |
| 84 | for (Path inputFile : inputFiles) { |
| 85 | if (!mergedFilesSet.contains(inputFile)) { |
| 86 | unSuccessMergedFiles.add(inputFile); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if (!unSuccessMergedFiles.isEmpty()) { |
| 92 | System.err.println("List of files that could not be merged:"); |
| 93 | unSuccessMergedFiles.forEach(path -> System.err.println(path.toString())); |
| 94 | } |
| 95 | |
| 96 | System.out.printf("Output path: %s, Input files size: %d, Merge files size: %d%n", |
| 97 | outputFilename, inputFiles.size(), mergedFiles.size()); |
| 98 | if (!unSuccessMergedFiles.isEmpty()) { |
| 99 | System.exit(1); |
| 100 | } |