1. 程式人生 > 實用技巧 >HDFS客戶端操作(JAVA程式碼)

HDFS客戶端操作(JAVA程式碼)

環境準備

windows需要配置hadoop的windows下的依賴

安裝包地址:

連結:https://pan.baidu.com/s/1QtbH-x-S0QytkmaoSosdEw
提取碼:2ktw

將安裝包解壓至自己所選擇的目錄,然後配置環境變數:

HADOOP_HOME : 解壓的目錄

PATH後新增:%HADOOP_HOME%\bin

maven所需依賴

新建Maven專案,新增以下依賴:

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.12.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.1.3</version>
        </dependency>

實際操作

實際操作前先講所需要的物件設為全域性變數,方便今後操作:

private URI uri;
private Configuration configuration;
private String user;
private FileSystem fileSystem;

使用@Before註釋的函式,可以在@Test註釋下的函式執行前執行,可以進行物件的初始化:

	@Before
    public void init() throws IOException, InterruptedException {
        uri = URI.create("hdfs://hadoop100:8020");
        configuration = new Configuration();
        user = "nevesettle";
        fileSystem = FileSystem.get(uri,configuration,user);
    }

@After則相反

	@After
    public void closeFs() throws IOException {
        fileSystem.close();
    }

檔案上傳

	/**
     * 上傳檔案
     * @throws IOException
     */
    @Test
    public void uploadFile() throws IOException {
        fileSystem.copyFromLocalFile(false,false,
                new Path("D:\\Program files\\QQ\\QQ Data\\794133319\\FileRecv\\資源集合.txt"),
                new Path("/"));
    }

使用的函式為:

copyFromLocalFile(boolean delSrc, boolean overwrite,Path src, Path dst)
  • delSrc:是否刪除原始檔
  • overwrite:目標地址有相同檔案是否覆蓋
  • src:原始檔地址
  • dst:目標檔案地址

檔案下載

	/**
     * 檔案下載
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void downFile() throws IOException {
        fileSystem.copyToLocalFile(false,
                new Path("/資源集合.txt"),
                new Path("d:\\"),
                true);
    }

使用的函式為:

copyToLocalFile(boolean delSrc, Path src, Path dst,boolean useRawLocalFileSystem)
  • delSrc:是否刪除原始檔
  • src:原始檔地址
  • dst:目標檔案地址
  • useRawLocalFileSystem:是否進行檔案傳輸校驗(CRC校驗)

檔案更名及移動位置

	/**
     * 檔名字修改
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void updateFile() throws IOException {
        fileSystem.rename(new Path("/資源集合.txt"),
                new Path("/資源.txt"));
    }

使用的函式為:

rename(Path src, Path dst)
  • src:原始檔
  • dst:目標檔案
  • 前後目錄相同,檔名不同,則是修改檔名
  • 前後目錄不同,檔名相同,則是修改目錄
  • 前後目錄和檔名都不同,則是移動位置並修改檔名

檔案刪除

	/**
     * 檔案刪除
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void deleteFile() throws IOException {
        fileSystem.delete(new Path("/資源.txt"),false);
    }

使用的函式為:

delete(Path f, boolean recursive)
  • f:刪除的檔案或目錄地址
  • recursive:是否遞迴刪除
  • 當要刪除的為檔案是,遞迴刪除為false
  • 當要刪除的是目錄且為空時,遞迴刪除為false
  • 當要刪除的是目錄且不為空,則遞迴刪除為true

IO流的檔案上傳

   /**
     * IO流的檔案上傳
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void uploadFileByIO() throws IOException {
        //原始檔地址
        String srcFilePath = "D:\\資源集合.txt";
        //目標檔案地址
        String destFilePath = "/資源集合.txt";
        //輸入流
        FileInputStream fis = new FileInputStream(new File(srcFilePath));
        //通過hdfs封裝的方法獲取輸出流
        FSDataOutputStream fos = fileSystem.create(new Path(destFilePath));

        //最原始的方法
//        int i = 0;
//        while ( ( i = fis.read()) != -1 ){
//            fot.write(i);
//        }

        //hdfs封裝的方法
        IOUtils.copyBytes(fis,fos,configuration);

        //關閉IO流
        IOUtils.closeStream(fis);
        IOUtils.closeStream(fos);

    }
  • IOUtils 使用時注意要使用hadoop的包,不要使用錯了
  • configuration 為我們的全域性變數,即配置

IO流的檔案下載

	/**
     * IO流的檔案下載
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void downFileByIO() throws IOException {

        //原始檔
        String srcFilePath = "/資源集合.txt";

        //目標檔案
        String destFilePath = "d:\\資源.txt";

        //獲得輸入流
        FSDataInputStream fis = fileSystem.open(new Path(srcFilePath));

        //獲得輸出流
        FileOutputStream fos = new FileOutputStream(new File(destFilePath));

        //傳輸
        IOUtils.copyBytes(fis,fos,configuration);

        //關閉
        IOUtils.closeStream(fis);
        IOUtils.closeStream(fos);


    }