Hadoop 的版本是 3.1.1
1. 啟動 Hadoop 服務
$ start-all.sh
2. 新建 IDEA 的 Maven 項目
2.1 選中 Maven,Project SDK 選擇為 1.8,再點擊 Next
2.2 填寫好 GroupId囚霸,ArtifactId 后光稼,點擊 Next
2.3 點擊 Finish
3. 修改 Target bytecode version
打開 Setting,選中 Build, Execution, Deployment -> Compiler -> java,將 Target bytecode version 改為 1.8 或 8竹揍。
確認這幾個配置下的 jdk 版本都為 1.8
4. 導入需要的 jar 包
4.1 選中 Dependencies 后點擊下方的 + 號材失,選擇「JARs or directories」
4.2 進入 Hadoop 目錄下的 share/hadoop/ 中痕鳍,把這幾個包都導進去
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>
<!--<!– https://mvnrepository.com/artifact/commons-logging/commons-logging –>-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!--<!– https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common –>-->
<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」
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 程序
6. 運行結(jié)果
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());
}
參數(shù)是新的文件夾的路徑,可以在文件夾里嵌套文件夾進行創(chuàng)建。
- 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
有多個重載函數(shù)笼呆,它的參數(shù)可以指定是否覆蓋已有的文件熊响、文件備份數(shù)量、寫入文件緩沖區(qū)大小诗赌、文件塊大小以及文件權限汗茄。它的返回值是一個 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);
}
將本地文件拷貝到文件系統(tǒng)洪碳,參數(shù)可以指定上傳本地文件的路徑,上傳的多個路徑組成的 Path 數(shù)組叼屠,存放目標對路徑瞳腌,可以指定是否刪除本地本地的文件或者覆蓋 hdfs 上已經(jīng)創(chuàng)建的文件。
- 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
參數(shù)指定移動文件后是否要刪除源文件嫂侍。
- 7.6
moveToLocalFile
public void moveToLocalFile(Path src, Path dst) throws IOException {
this.copyToLocalFile(true, src, dst);
}
將目標文件移動到指定路徑,函數(shù)內(nèi)部調(diào)用的是 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;
第一個參數(shù)是要刪除的路徑,第二個參數(shù)為 true
時瞭空,如果目標文件夾內(nèi)有文件揪阿,會強制刪除。