Exemple #1
0
 /**
  * HDFS Path에서 IP주소를 추출한다.
  *
  * @param path HDFS Path
  * @return IP Address
  */
 public static String getIpAddressFromPath(String path) {
   if (!path.startsWith(HDFS_URL_PREFIX)) {
     throw new FileSystemException(ExceptionUtils.getMessage("Invalid path '{}'", path));
   }
   String[] split = org.springframework.util.StringUtils.delete(path, HDFS_URL_PREFIX).split(":");
   return split[0];
 }
Exemple #2
0
 /**
  * 지정한 경로를 생성한다.
  *
  * @param fs FileSystem
  * @param path 생성할 경로
  * @return 정상적으로 생성한 경우 <tt>true</tt>
  */
 public static boolean mkdir(FileSystem fs, String path) {
   try {
     return FileSystem.mkdirs(fs, new Path(path), FsPermission.getDefault());
   } catch (Exception ex) {
     throw new FileSystemException(ExceptionUtils.getMessage("Cannot create '{}'", path), ex);
   }
 }
Exemple #3
0
 /**
  * 지정한 경로를 삭제한다.
  *
  * @param fs FileSystem
  * @param path 삭제할 경로
  * @return 정상적으로 삭제한 경우 <tt>true</tt>
  */
 public static boolean delete(FileSystem fs, String path) {
   try {
     return fs.delete(new Path(path), true);
   } catch (Exception ex) {
     throw new FileSystemException(ExceptionUtils.getMessage("Cannot delete '{}'", path), ex);
   }
 }
Exemple #4
0
 /**
  * 지정한 경로가 파일인지 확인한다.
  *
  * @param fs FileSystem
  * @param path 확인할 Path
  * @return 파일인 경우 <tt>true</tt>
  */
 public static boolean isFile(FileSystem fs, String path) {
   try {
     return fs.isFile(new Path(path));
   } catch (Exception ex) {
     throw new FileSystemException(ExceptionUtils.getMessage("Cannot access '{}'", path), ex);
   }
 }
Exemple #5
0
 /**
  * HDFS Path에서 Port를 추출한다.
  *
  * @param path HDFS Path
  * @return Port
  */
 public static String getPortFromPath(String path) {
   if (!path.startsWith(HDFS_URL_PREFIX)) {
     throw new FileSystemException(ExceptionUtils.getMessage("Invalid path '{}'", path));
   }
   String[] split = org.springframework.util.StringUtils.delete(path, HDFS_URL_PREFIX).split(":");
   if (split.length != 2) {
     throw new FileSystemException(
         "Invalid path pattern. Path pattern must be \"hdfs://IP:PORT\".");
   }
   return split[1];
 }
Exemple #6
0
  /**
   * 지정한 경로의 파일을 문자열로 로딩한다. 다음의 조건에 해당하면 처리하지 않는다.
   *
   * <ul>
   *   <li>파일이 아닌 경우
   *   <li>파일의 크기가
   * </ul>
   *
   * @param fs Hadoop의 {@link org.apache.hadoop.fs.FileSystem}
   * @param path Path
   * @param encoding 인코딩
   * @return 문자열
   */
  public static String load(FileSystem fs, String path, String encoding) {
    try {
      FileStatus fileStatus = fs.getFileStatus(new Path(path));
      long length = fileStatus.getLen();
      if (length > MAX_SIZE) {
        throw new IllegalArgumentException("Exceeded " + MAX_SIZE + " bytes : '" + path + "'");
      }
    } catch (Exception ex) {
      throw new FileSystemException(ExceptionUtils.getMessage("Cannot access '{}'", path), ex);
    }

    FSDataInputStream is = null;
    try {
      is = fs.open(new Path(path));
      return IOUtils.toString(is, encoding);
    } catch (IOException e) {
      throw new FileSystemException(ExceptionUtils.getMessage("Cannot load '{}'", path), e);
    } finally {
      IOUtils.closeQuietly(is);
    }
  }