@Test public void givenAPathToDir_whenCountFilesIsCalledWithFilter_thenProperFileCountIsReturned() throws IOException { Path tempDir = Files.createTempDirectory(this.getClass().getSimpleName()); Files.createTempFile(tempDir, "count_me", "a"); Files.createTempFile(tempDir, "count_me", "a"); Files.createTempFile(tempDir, "not_count_me", "b"); int result = HdfsUtils.countFiles(new Configuration(), tempDir.toString(), x -> x.getName().endsWith("a")); assertEquals(2, result); }
public static int countFiles(Configuration hadoopConf, String pathname, PathFilter pathFilter) throws IOException { Path path = new Path(pathname); FileSystem fileSystem = FileSystem.get(hadoopConf); if (fileSystem.exists(path) && fileSystem.isDirectory(path)) { return fileSystem.listStatus(path, pathFilter).length; } throw new RuntimeException(String.format("Path does not exist or is not a directory: %s", pathname)); }
HdfsUtils { public static int countFiles(Configuration hadoopConf, String pathname, PathFilter pathFilter) throws IOException { Path path = new Path(pathname); FileSystem fileSystem = FileSystem.get(hadoopConf); if (fileSystem.exists(path) && fileSystem.isDirectory(path)) { return fileSystem.listStatus(path, pathFilter).length; } throw new RuntimeException(String.format("Path does not exist or is not a directory: %s", pathname)); } }
HdfsUtils { public static int countFiles(Configuration hadoopConf, String pathname, PathFilter pathFilter) throws IOException { Path path = new Path(pathname); FileSystem fileSystem = FileSystem.get(hadoopConf); if (fileSystem.exists(path) && fileSystem.isDirectory(path)) { return fileSystem.listStatus(path, pathFilter).length; } throw new RuntimeException(String.format("Path does not exist or is not a directory: %s", pathname)); } private  HdfsUtils(); }
HdfsUtils { public static int countFiles(Configuration hadoopConf, String pathname, PathFilter pathFilter) throws IOException { Path path = new Path(pathname); FileSystem fileSystem = FileSystem.get(hadoopConf); if (fileSystem.exists(path) && fileSystem.isDirectory(path)) { return fileSystem.listStatus(path, pathFilter).length; } throw new RuntimeException(String.format("Path does not exist or is not a directory: %s", pathname)); } private  HdfsUtils(); static void remove(Configuration hadoopConf, String pathname); static List<String> listDirs(Configuration hadoopConf, String pathname); static int countFiles(Configuration hadoopConf, String pathname, PathFilter pathFilter); }
HdfsUtils { public static int countFiles(Configuration hadoopConf, String pathname, PathFilter pathFilter) throws IOException { Path path = new Path(pathname); FileSystem fileSystem = FileSystem.get(hadoopConf); if (fileSystem.exists(path) && fileSystem.isDirectory(path)) { return fileSystem.listStatus(path, pathFilter).length; } throw new RuntimeException(String.format("Path does not exist or is not a directory: %s", pathname)); } private  HdfsUtils(); static void remove(Configuration hadoopConf, String pathname); static List<String> listDirs(Configuration hadoopConf, String pathname); static int countFiles(Configuration hadoopConf, String pathname, PathFilter pathFilter);  }