使用 IDEA 搭建 Hadoop 3.1.1 項目

Hadoop 的版本是 3.1.1java

1. 啓動 Hadoop 服務

$ start-all.sh
複製代碼

2. 新建 IDEA 的 Maven 項目

2.1 選中 Maven,Project SDK 選擇爲 1.8,再點擊 Next shell

點擊 Next

2.2 填寫好 GroupId,ArtifactId 後,點擊 Next apache

2.3 點擊 Finish 數組

image.png

3. 修改 Target bytecode version

打開 Setting,選中 Build, Execution, Deployment -> Compiler -> java,將 Target bytecode version 改成 1.8 或 8。bash

Target bytecode version

確認這幾個配置下的 jdk 版本都爲 1.8 app

Project SDK

Module SDK

4. 導入須要的 jar 包

4.1 選中 Dependencies 後點擊下方的 + 號,選擇「JARs or directories」 函數

添加 jar 包
JARs or directories

4.2 進入 Hadoop 目錄下的 share/hadoop/ 中,把這幾個包都導進去oop

share/hadoop/

選擇 OK

繼續 OK

4.2 在 pom.xml 中添加以下依賴學習

<dependencies>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!--&lt;!&ndash; https://mvnrepository.com/artifact/commons-logging/commons-logging &ndash;&gt;-->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

        <!--&lt;!&ndash; https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common &ndash;&gt;-->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>3.1.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-core -->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-core</artifactId>
            <version>1.2.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.1.1</version>
        </dependency>
        
    </dependencies>
複製代碼

5. 編寫 Hadoop 項目的 Java 代碼

5.1 新建 Java 類「Test.java」大數據

image.png

5.2 編寫代碼

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class Test {

    // 在 HDFS 中新建一個 test 文件夾
    public static void main(String[] args) {

        FileSystem fileSystem = null;
        try {
            fileSystem = FileSystem.get(new URI("hdfs://localhost:9000/"),new Configuration(),"binguner");
            fileSystem.mkdirs(new Path("/test"));
            fileSystem.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

}
複製代碼

5.3 運行 Java 程序

image.png

6. 運行結果

6.1 運行前的 HDFS 目錄下沒有 test 文件夾

6.2 運行後的 HDFS 目錄下多了 test 文件夾

7. FileSystem 經常使用接口

  • 7.1 mkdirs
public boolean mkdirs(Path f) throws IOException {
    return this.mkdirs(f, FsPermission.getDirDefault());
}
複製代碼

參數是新的文件夾的路徑,能夠在文件夾裏嵌套文件夾進行建立。

  • 7.2 create
public FSDataOutputStream create(Path f) throws IOException {
        return this.create(f, true);
    }

    public FSDataOutputStream create(Path f, boolean overwrite) throws IOException {
        return this.create(f, overwrite, this.getConf().getInt("io.file.buffer.size", 4096), this.getDefaultReplication(f), this.getDefaultBlockSize(f));
    }

    public FSDataOutputStream create(Path f, Progressable progress) throws IOException {
        return this.create(f, true, this.getConf().getInt("io.file.buffer.size", 4096), this.getDefaultReplication(f), this.getDefaultBlockSize(f), progress);
    }

    public FSDataOutputStream create(Path f, short replication) throws IOException {
        return this.create(f, true, this.getConf().getInt("io.file.buffer.size", 4096), replication, this.getDefaultBlockSize(f));
    }

    public FSDataOutputStream create(Path f, short replication, Progressable progress) throws IOException {
        return this.create(f, true, this.getConf().getInt("io.file.buffer.size", 4096), replication, this.getDefaultBlockSize(f), progress);
    }

    public FSDataOutputStream create(Path f, boolean overwrite, int bufferSize) throws IOException {
        return this.create(f, overwrite, bufferSize, this.getDefaultReplication(f), this.getDefaultBlockSize(f));
    }

    public FSDataOutputStream create(Path f, boolean overwrite, int bufferSize, Progressable progress) throws IOException {
        return this.create(f, overwrite, bufferSize, this.getDefaultReplication(f), this.getDefaultBlockSize(f), progress);
    }

    public FSDataOutputStream create(Path f, boolean overwrite, int bufferSize, short replication, long blockSize) throws IOException {
        return this.create(f, overwrite, bufferSize, replication, blockSize, (Progressable)null);
    }

    public FSDataOutputStream create(Path f, boolean overwrite, int bufferSize, short replication, long blockSize, Progressable progress) throws IOException {
        return this.create(f, FsCreateModes.applyUMask(FsPermission.getFileDefault(), FsPermission.getUMask(this.getConf())), overwrite, bufferSize, replication, blockSize, progress);
    }

    public abstract FSDataOutputStream create(Path var1, FsPermission var2, boolean var3, int var4, short var5, long var6, Progressable var8) throws IOException;

    public FSDataOutputStream create(Path f, FsPermission permission, EnumSet<CreateFlag> flags, int bufferSize, short replication, long blockSize, Progressable progress) throws IOException {
        return this.create(f, permission, flags, bufferSize, replication, blockSize, progress, (ChecksumOpt)null);
    }

    public FSDataOutputStream create(Path f, FsPermission permission, EnumSet<CreateFlag> flags, int bufferSize, short replication, long blockSize, Progressable progress, ChecksumOpt checksumOpt) throws IOException {
        return this.create(f, permission, flags.contains(CreateFlag.OVERWRITE), bufferSize, replication, blockSize, progress);
    }
複製代碼

create 有多個重載函數,它的參數能夠指定是否覆蓋已有的文件、文件備份數量、寫入文件緩衝區大小、文件塊大小以及文件權限。它的返回值是一個 FSDataOutputStream,經過返回的 FSDataOutputStream 對象能夠對文件進行寫入。

  • 7.3 copyFromLocal
public void copyFromLocalFile(Path src, Path dst) throws IOException {
        this.copyFromLocalFile(false, src, dst);
    }

    public void copyFromLocalFile(boolean delSrc, Path src, Path dst) throws IOException {
        this.copyFromLocalFile(delSrc, true, src, dst);
    }

    public void copyFromLocalFile(boolean delSrc, boolean overwrite, Path[] srcs, Path dst) throws IOException {
        Configuration conf = this.getConf();
        FileUtil.copy(getLocal(conf), srcs, this, dst, delSrc, overwrite, conf);
    }
複製代碼

將本地文件拷貝到文件系統,參數能夠指定上傳本地文件的路徑,上傳的多個路徑組成的 Path 數組,存放目標對路徑,能夠指定是否刪除本地本地的文件或者覆蓋 hdfs 上已經建立的文件。

  • 7.4 copyToLocalFile
public void copyToLocalFile(Path src, Path dst) throws IOException {
        this.copyToLocalFile(false, src, dst);
    }

    public void copyToLocalFile(boolean delSrc, Path src, Path dst) throws IOException {
        this.copyToLocalFile(delSrc, src, dst, false);
    }
複製代碼

將目標文件複製到本地指定路徑,delSrc 參數指定移動文件後是否要刪除源文件。

  • 7.6 moveToLocalFile
public void moveToLocalFile(Path src, Path dst) throws IOException {
        this.copyToLocalFile(true, src, dst);
    }
複製代碼

將目標文件移動到指定路徑,函數內部調用的是 copyToLocalFile

  • 7.6 exists
public boolean exists(Path f) throws IOException {
        try {
            return this.getFileStatus(f) != null;
        } catch (FileNotFoundException var3) {
            return false;
        }
    }
複製代碼

輸入一個路徑,檢查 HDFS 上是否存在這個路徑,存在返回 true,不存在返回 false

  • 7.7 delete
public abstract boolean delete(Path var1, boolean var2) throws IOException;
複製代碼

第一個參數是要刪除的路徑,第二個參數爲 true 時,若是目標文件夾內有文件,會強制刪除。

歡迎關注本文做者:

掃碼關注並回復「乾貨」,獲取我整理的千G Android、iOS、JavaWeb、大數據、人工智能等學習資源。

相關文章
相關標籤/搜索