Exemplo n.º 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];
 }
Exemplo n.º 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);
   }
 }
Exemplo n.º 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);
   }
 }
Exemplo n.º 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);
   }
 }
Exemplo n.º 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];
 }
Exemplo n.º 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);
    }
  }
 /**
  * 지정한 URL에 대해서 Input Stream을 반환한다.
  *
  * @param url URL
  * @return Input Stream
  */
 public static InputStream openStream(String url) {
   String message = ExceptionUtils.getMessage("URL '{}' 리소스에 대해서 Input Stream을 얻을 수 없습니다.", url);
   try {
     return new URL(url).openStream();
   } catch (MalformedURLException ex) {
     if (ex.getMessage().contains("no protocol") && url.startsWith("/")) {
       try {
         return new URL("file:" + url).openStream();
       } catch (Exception e) {
         throw new SystemException(message, e);
       }
     }
     throw new SystemException(message, ex);
   } catch (Exception ex) {
     throw new SystemException(message, ex);
   }
 }
 /**
  * 지정한 URL에 대해서 리소스가 존재하는지 확인한다.
  *
  * @param url URL
  * @return 존재하는 경우 <tt>true</tt>
  */
 public static boolean isExist(String url) {
   String message = ExceptionUtils.getMessage("URL '{}' 리소스에 대해서 Input Stream을 얻을 수 없습니다.", url);
   InputStream inputStream = null;
   try {
     inputStream = new URL(url).openStream();
     return true;
   } catch (MalformedURLException ex) {
     if (ex.getMessage().contains("no protocol") && url.startsWith("/")) {
       try {
         inputStream = new URL("file:" + url).openStream();
         return true;
       } catch (Exception e) {
         return false;
       }
     }
     return false;
   } catch (Exception ex) {
     return false;
   } finally {
     IOUtils.closeQuietly(inputStream);
   }
 }