public static List<FileStatus> getFileStatusRecurse(Path path, int level,  FileSystem fs)
      throws IOException {

    // if level is <0, the return all files/directories under the specified path
    if (level < 0) {
      List<FileStatus> result = new ArrayList<FileStatus>();
      try {
        FileStatus fileStatus = fs.getFileStatus(path);
        FileUtils.listStatusRecursively(fs, fileStatus, result);
      } catch (IOException e) {
        // globStatus() API returns empty FileStatus[] when the specified path
        // does not exist. But getFileStatus() throw IOException. To mimic the
        // similar behavior we will return empty array on exception. For external
        // tables, the path of the table will not exists during table creation
        return Collections.emptyList();
      }
      return result;
    }

    // construct a path pattern (e.g., /*/*) to find all dynamically generated paths
    StringBuilder sb = new StringBuilder(path.toUri().getPath());
    for (int i = 0; i < level; i++) {
      sb.append(Path.SEPARATOR).append("*");
    }
    Path pathPattern = new Path(path, sb.toString());
    return Lists.newArrayList(fs.globStatus(pathPattern, FileUtils.HIDDEN_FILES_PATH_FILTER));
  }
Get all file status from a root path and recursively go deep into certain levels.

@param path
         the root path
@param level
         the depth of directory to explore
@param fs
         the file system
@return array of FileStatus
@throws IOException
common/src/java/org/apache/hadoop/hive/common/HiveStatsUtils.java