Java API操做HDFS

package project.etl.core.util;java

import java.io.FileNotFoundException;
import java.io.IOException;apache

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.security.UserGroupInformation;工具

/**
* 操做HDFS工具類
* @author snow
* @date: 2019-10-13 10:01:59
*/
public class HDFSUtil {
  /**
  * 獲取默認文件系統
  * @return
  * @throws IOException
  * @author snow
  * @date: 2019-10-13 10:16:18
  */
  public static FileSystem getFileSystem() throws IOException {
    Configuration conf = new Configuration();
    FileSystem fileSystem = FileSystem.get(conf);
    return fileSystem;
  }
  /**
  * 獲取指定文件系統
  * @param fileSystemPath
  * @return fileSystem
  * @throws IOException
  * @author snow
  * @date: 2019-10-17 15:06:42
  */
  public static FileSystem getFileSystem(String fileSystemPath) throws IOException {
    Configuration conf = new Configuration();
    conf.set("fs.defaultFS", fileSystemPath);
    FileSystem fileSystem = FileSystem.get(conf);
    return fileSystem;
  }
  /**
  * 經過Hadoop配置文件獲取文件系統
  * @param coreSite core-site.xml文件路徑
  * @param hdfsSite hdfs-site.xml文件路徑
  * @return fileSystem
  * @throws IOException
  * @author snow
  * @date: 2019-10-17 15:10:48
  */
  public static FileSystem getFileSystem(String fileSystemPath,String coreSite,String hdfsSite) throws IOException {
    Configuration conf = new Configuration();
    conf.addResource(new Path(coreSite));
    conf.addResource(new Path(hdfsSite));
    FileSystem fileSystem = FileSystem.get(conf);
    return fileSystem;
  }
  /**
  * 經過Hadoop配置文件獲取文件系統並經過keytab驗證
  * @param coreSite core-site.xml文件路徑
  * @param hdfsSite hdfs-site.xml文件路徑
  * @param krd5Path krd5.conf文件路徑
  * @param user 用戶名
  * @param keytab keytab驗證文件路徑
  * @return fileSystem
  * @throws IOException
  * @author snow
  * @date: 2019-10-17 15:15:18
  */
  public static FileSystem getFileSystem(String coreSite,String hdfsSite,String krd5Path,String user,String keytab) throws IOException {
    Configuration conf = new Configuration();
    conf.addResource(new Path(coreSite));
    conf.addResource(new Path(hdfsSite));
    //當程序不在hadoop集羣內運行時設置
    System.setProperty("java.security.krb5.conf", krd5Path);
    UserGroupInformation.loginUserFromKeytab(user, keytab);
    FileSystem fileSystem = FileSystem.get(conf);
    return fileSystem;
  }
  /**
  * 經過Hadoop配置文件獲取文件系統並經過keytab驗證
  * @param coreSite core-site.xml文件路徑
  * @param hdfsSite hdfs-site.xml文件路徑
  * @param user 用戶名
  * @param keytab keytab驗證文件路徑
  * @return fileSystem
  * @throws IOException
  * @author snow
  * @date: 2019-10-17 15:25:35
  */
  public static FileSystem getFileSystem(String coreSite,String hdfsSite,String user,String keytab) throws IOException {
    Configuration conf = new Configuration();
    conf.addResource(new Path(coreSite));
    conf.addResource(new Path(hdfsSite));
    UserGroupInformation.loginUserFromKeytab(user, keytab);
    FileSystem fileSystem = FileSystem.get(conf);
    return fileSystem;
  }
  /**
  * 建立文件夾
  * @param fileSystem
  * @param path
  * @throws IllegalArgumentException
  * @throws IOException
  * @author snow
  * @date: 2019-10-17 15:43:44
  */
  public static void mkdirsFolder(FileSystem fileSystem,String path) throws IllegalArgumentException, IOException {
    if("".equals(path)) {
      System.out.println("path不能爲空!");
      return;
    }
    Path folder = new Path(path);
    if(!fileSystem.exists(folder)) {
      boolean bool = fileSystem.mkdirs(folder);
      if(bool) {
        System.out.println("文件夾建立成功!");
      }else {
        System.out.println("文件夾建立失敗!");
      }
    }
  }
  /**
  * 向文件系統指定路徑上傳單個文件或文件夾
  * @param fileSystem
  * @param localPath
  * @param HDFSPath
  * @throws IOException
  * @author snow
  * @date: 2019-10-13 10:27:21
  */
  public static void uploadFileToHDFS(FileSystem fileSystem,Path localPath,Path HDFSPath) throws IOException {
    fileSystem.copyFromLocalFile(localPath, HDFSPath);
  }
  /**
  * 下載文件系統指定路徑的單個文件或文件夾
  * @param fileSystem
  * @param HDFSPath
  * @param localPath
  * @author snow
  * @throws IOException
  * @date: 2019-10-13 11:02:54
  */
  public static void loadHDFSFile(FileSystem fileSystem,Path HDFSPath,Path localPath) throws IOException {
    fileSystem.copyToLocalFile(HDFSPath, localPath);
  }
  /**
  * 刪除文件系統文件或空文件夾
  * @param fileSystem
  * @param HDFSPath
  * @throws IOException
  * @author snow
  * @date: 2019-10-13 11:33:22
  */
  public static void delHDFSFile(FileSystem fileSystem,Path HDFSPath) throws IOException {
    if(fileSystem.exists(HDFSPath)) {
      //禁止遞歸刪除文件
      fileSystem.delete(HDFSPath, false);
    }
  }
  /**
  * 遞歸刪除文件系統指定文件夾
  * @param fileSystem
  * @param HDFSPath
  * @throws IOException
  * @author snow
  * @date: 2019-10-13 11:33:22
  */
  public static void delHDFSFileEcho(FileSystem fileSystem,Path HDFSPath) throws IOException {
    if(fileSystem.exists(HDFSPath)) {
      //遞歸刪除文件
      fileSystem.delete(HDFSPath, true);
    }
  }
  /**
  * 查看文件系統指定路徑下的文件及目錄
  * @param fileSystem
  * @param HDFSPath
  * @throws FileNotFoundException
  * @throws IOException
  * @author snow
  * @date: 2019-10-13 12:03:12
  */
  public static void checkHDFSFile(FileSystem fileSystem,Path HDFSPath) throws FileNotFoundException, IOException {
    if(fileSystem.exists(HDFSPath)) {
      FileStatus[] fileStatuses = fileSystem.listStatus(HDFSPath);
      for (FileStatus fileStatus : fileStatuses) {
        String isDir = fileStatus.isDirectory() ? "文件夾::" : "文件:";
        String path = fileStatus.getPath().toString();
        System.out.println(isDir + "\t" + path);
      }
    }else {
      System.out.println("文件或目錄不存在!");
    }
  }
  /**
  * 關閉鏈接
  * @param fileSystem
  * @throws IOException
  * @author snow
  * @date: 2019-10-13 12:05:44
  */
  public static void close(FileSystem fileSystem) throws IOException {
    fileSystem.close();
  }
}oop

相關文章
相關標籤/搜索