一、環境準備
1. 配置 windows 的環境變量
在此處下載下傳 windows 的依賴項 https://gitee.com/parasol_ry/hadoop/tree/master/hadoop-3.1.0/bin
配置環境變量:

<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
</dependencies>
在項目的 src/main/resources 目錄下,建立一個檔案,命名為“log4j.properties”,在檔案 中填入
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
HDFS 的 API 案例實操
package com.learn.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
public class HdfsClient {
//更新為全局變量是 ctrl + alt + F
// ctrl + p 檢視參數有哪些
private FileSystem fs;
@Before
public void init() throws URISyntaxException, IOException, InterruptedException {
//連接配接的nn位址
URI uri = new URI("hdfs://node01:8020");
//配置一個檔案
Configuration configuration = new Configuration();
configuration.set("dfs.replication", "2");
//使用者
String user = "root";
//擷取用戶端對象
fs = FileSystem.get(uri, configuration, user);
}
@After
public void close() throws IOException {
//關閉資源
fs.close();
}
//建立目錄
@Test
public void testmkdir() throws URISyntaxException, IOException, InterruptedException {
//建立一個檔案夾
fs.mkdirs(new Path("/xiyou/huaguoshan3"));
}
/*
*參數優先級
* hdfs-default.xml => hdfs-site.xml => 在項目資源目錄下的配置檔案優先級 => 代碼中的優先級配置
* 優先級由 低 到 高
* */
//上傳
@Test
public void testPut() throws IOException {
//參數解讀:
// 1. 删除原始資料 2.是否允許覆寫 3.原始資料路徑 4.目的地路徑
fs.copyFromLocalFile(false, true, new Path("D:\\hadoop\\sun.txt"), new Path("/xiyou/huaguoshan"));
}
//檔案下載下傳
@Test
public void testGet() throws IOException {
//參數解讀
// 1. 源檔案是否删除 2.原檔案的路徑 3.目标位址路徑 4.是否開啟校驗
fs.copyToLocalFile(false, new Path("/xiyou/huaguoshan"), new Path("D:\\hadoop"), true);
}
//删除
@Test
public void testRm() throws IOException {
//參數解讀
// 1. 要删除的路徑 2. 是否遞歸删除
// 删除檔案
fs.delete(new Path("/jdk-8u291-linux-x64.tar.gz"), false);
//删除目錄
fs.delete(new Path("/xiyou/huaguoshan"), true);
}
//檔案的更名和移動
@Test
public void testMv() throws IOException {
//參數解讀
// 1.原檔案的路徑 2.目标檔案的路徑
//對檔案的修改
fs.rename(new Path("/input/word.txt"), new Path("/input/ss.txt"));
}
//擷取檔案詳細資訊
@Test
public void fileDetail() throws IOException {
//參數解讀
// 1.檔案的路徑 2.是否遞歸周遊
//擷取所有檔案資訊 疊代器
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
//周遊檔案
while (listFiles.hasNext()) {
LocatedFileStatus fileStatus = listFiles.next();
System.out.println("=======" + fileStatus.getPath() + "=======");
System.out.println(fileStatus.getPermission());
System.out.println(fileStatus.getOwner());
System.out.println(fileStatus.getGroup());
System.out.println(fileStatus.getLen());
System.out.println(fileStatus.getModificationTime());
System.out.println(fileStatus.getReplication());
System.out.println(fileStatus.getBlockSize());
System.out.println(fileStatus.getPath().getName());
//擷取塊資訊
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
System.out.println(Arrays.toString(blockLocations));
}
}
//判斷是檔案還是檔案夾
@Test
public void testFile() throws IOException {
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus status : listStatus) {
if (status.isFile()) {
System.out.println("檔案: " + status.getPath().getName());
} else {
System.out.println("路徑: " + status.getPath().getName());
}
}
}
}
東北日出西邊雨 道是無情卻有情