1. 程式人生 > >java API 操作HDFS檔案系統

java API 操作HDFS檔案系統

1.Maven 構建java工程


2.新增HDFS相關依賴

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <hadoop.version>2.6.0-cdh5.7.0</hadoop.version>
</properties>
<dependencies>
<!--新增hadoop依賴-->
<dependency>
        <groupId>
org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>${hadoop.version}</version> <scope>provided</scope> </dependency>
     <!--新增單元測試的依賴-->
    <dependency>
        <groupId>junit</groupId>
<artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency>
</dependencies>

3.開發Java API操作HDFS檔案

package com.imooc.hadoop.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;

/**
 * hadoop HDFS java API 操作
 */
public class HDFSApp {

    FileSystem fileSystem = null;
    Configuration configuration = null;
    //hdfs地址
    public static final String HDFS_PASH = "hdfs://hadoop000:8020";
    /**
     * 建立HDFS目錄
     * @throws Exception
     */
    @Test
    public void mkdir() throws Exception{
        fileSystem.mkdirs(new Path("/hdfsapi/test"));
    }

    /**
     * 建立檔案
     * @throws Exception
     */
    @Test
    public void create() throws Exception{
        FSDataOutputStream outputStream = fileSystem.create(new Path("/hdfsapi/test/a.txt"));
//        FSDataOutputStream outputStream = fileSystem.create(new Path("/hdfsapi/test/b.txt"),true,1024,(short)1,1048576L);
        outputStream.write("hello hadoop".getBytes());
        outputStream.flush();
        outputStream.close();
    }

    /**
     * 檢視HDFS檔案的內容
     * @throws Exception
     */
    @Test
    public void cat() throws Exception{
        FSDataInputStream in = fileSystem.open(new Path("/hdfsapi/test/a.txt"));
        IOUtils.copyBytes(in,System.out,1024);
        in.close();
    }
    /**
     * 重新命名
     */
    @Test
    public void rename() throws Exception {
        Path oldPath = new Path("/hdfsapi/test/a.txt");
        Path newPath = new Path("/hdfsapi/test/b.txt");
        fileSystem.rename(oldPath, newPath);
    }

    /**
     * 上傳檔案到HDFS
     *
     * @throws Exception
     */
    @Test
    public void copyFromLocalFile() throws Exception {
        Path localPath = new Path("C:/soft/apache/apache-maven-3.5.0/conf/settings.xml");
        Path hdfsPath = new Path("/hdfsapi/test");
        fileSystem.copyFromLocalFile(localPath, hdfsPath);
    }
    /**
     * 上傳檔案到HDFS
     */
    @Test
    public void copyFromLocalFileWithProgress() throws Exception {
        InputStream in = new BufferedInputStream(
                new FileInputStream(
                        new File("D:\\001程式設計學習\\慕課Hadoop\\software\\spark-2.1.0-bin-2.6.0-cdh5.7.0.tgz")));

        FSDataOutputStream output = fileSystem.create(new Path("/hdfsapi/test/spark-1.6.1.tgz"),
                new Progressable() {
                    public void progress() {
                        System.out.println(".");//帶進度條提示資訊
                    }
                });

        IOUtils.copyBytes(in,output,4096);
    }

    /**
     * 下載HDFS檔案
     */
    @Test
    public void copyToLocalFile() throws Exception {
        Path localPath = new Path("D:\\h.xml");
        Path hdfsPath = new Path("/hdfsapi/test/a.xml");
        fileSystem.copyToLocalFile(false,hdfsPath, localPath,true);
    }

    /**
     * 檢視某個目錄下的所有檔案
     */
    @Test
    public void listFiles() throws Exception {
        FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/hdfsapi"));

        for(FileStatus fileStatus : fileStatuses) {
            String isDir = fileStatus.isDirectory() ? "資料夾" : "檔案";
            short replication = fileStatus.getReplication();
            long len = fileStatus.getLen();
            String path = fileStatus.getPath().toString();

            System.out.println(isDir + "\t" + replication + "\t" + len + "\t" + path);
        }

    }

    /**
     * 刪除
     * recursive 遞迴刪除
     * @throws Exception
     */
    @Test
    public void delete() throws Exception{
        fileSystem.delete(new Path("/hdfsapi/test/b.txt"),true);
    }
    @Before
    public void setUp() throws Exception{
        System.out.println("HDFSApp.setUp");
        configuration = new Configuration();
        fileSystem = FileSystem.get(new URI(HDFS_PASH),configuration,"hadoop");
    }

    @After
    public void tearDown() throws Exception{
        configuration = null;
        fileSystem = null;
        System.out.println("HDFSApp.tearDown");
    }
}