客戶端環(huán)境準(zhǔn)備
hadoop的 Windows依賴文件夾,拷貝hadoop-3.1.0到非中文路徑(比如d:\)。
- 配置HADOOP_HOME環(huán)境變量
- 配置Path環(huán)境變量。
不能放在包含有空格的目錄下,cmd 輸入hadoop顯示此時(shí)不應(yīng)有 \hadoop-3.0.0\bin\。我放在E:\Program Files (x86) \hadoop-3.0.0\bin\中,就出現(xiàn)錯(cuò)誤
驗(yàn)證Hadoop環(huán)境變量是否正常。雙擊winutils.exe,如果報(bào)如下錯(cuò)誤。說(shuō)明缺少微軟運(yùn)行庫(kù)(正版系統(tǒng)往往有這個(gè)問(wèn)題)。里面有對(duì)應(yīng)的微軟運(yùn)行庫(kù)安裝包雙擊安裝即可。 - 配置Path環(huán)境變量。然后重啟電腦
- 如果上述操作后在后面代碼執(zhí)行的過(guò)程中,還有問(wèn)題可以將bin目錄下hadoop.dll和winutils.exe放到C:/windows/system32目錄下
- 在IDEA中創(chuàng)建一個(gè)Maven工程HdfsClientDemo,并導(dǎo)入相應(yīng)的依賴坐標(biāo)+日志添加
<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>
- 在項(xiàng)目的src/main/resources目錄下,新建一個(gè)文件,命名為“l(fā)og4j.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
- 創(chuàng)建HdfsClient類
方式一
public class HdfsClient{
@Test
public void testMkdirs() throws IOException, InterruptedException, URISyntaxException{
// 1 獲取文件系統(tǒng)
Configuration configuration = new Configuration();
// 配置在集群上運(yùn)行
FileSystem fs = FileSystem.get(
new URI("hdfs://hadoop102:8020"),
configuration,
"xiaoming"
);
}
}
vim core-site.xml
方式二 給main方法傳參數(shù)
1.代碼部分
public class HdfsClient{
@Test
public void testMkdirs() throws IOException, InterruptedException, URISyntaxException{
// 1 獲取文件系統(tǒng)
Configuration configuration = new Configuration();
// 配置在集群上運(yùn)行
configuration.set("fs.defaultFS", "hdfs://hadoop102:8020");
FileSystem fs = FileSystem.get(configuration);
}
}
- 配置部分
運(yùn)行時(shí)需要配置用戶名稱(默認(rèn)是使用windows用戶名操作HDFS)
給main方法傳參 :
1.在IDEA中傳參 – 在運(yùn)行按鈕上右鍵—>點(diǎn)擊Edit 類名.main() —>
Program Arguments后面寫傳的參數(shù) —>默認(rèn)就是字符串 多個(gè)參數(shù)之間用空格隔開,注意:如果右鍵沒(méi)有就先運(yùn)行一遍。
2. java 字節(jié)碼文件名 參數(shù)1 參數(shù)2 …
客戶端去操作HDFS時(shí),是有一個(gè)用戶身份的。默認(rèn)情況下,HDFS客戶端API會(huì)從JVM中獲取一個(gè)參數(shù)來(lái)作為自己的用戶身份:-DHADOOP_USER_NAME=*****,
為用戶名稱。
錯(cuò)誤:Permission denied: user=XXXXXX, access=WRITE, inode=“/demo”::supergroup:drwxr-xr-x
解決方案 :①修改權(quán)限–不建議 ②修改操作HDFS的用戶名(默認(rèn)是windows系統(tǒng)登錄的用戶名)
修改用戶名:在IDEA中傳參 -- 在運(yùn)行按鈕上右鍵--->點(diǎn)擊Edit 類名.main() --->
VM OPtions : -DHADOOP_USER_NAME=******
public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {
URI uri = new URI("hdfs://hadoop102:8020");
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://hadoop102:8020");
conf.set("dfs.replication","2");
FileSystem fs = FileSystem.get( conf);
// fs.copyToLocalFile(false, new Path("/input/word.txt"), new Path("E:\\io"), false);
fs.copyFromLocalFile(false,true,new Path("E:\\io\\upload.txt"),new Path("/input"));
fs.close();
}
}
VM options可以調(diào)節(jié)JVM的堆棧等大小。
HDFS文件上傳
@Test
public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
// 1 獲取文件系統(tǒng)
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "xiaoming");
// 2 上傳文件
fs.copyFromLocalFile(new Path("e:/banzhang.txt"), new Path("/banzhang.txt"));
// 3 關(guān)閉資源
fs.close();
IDEA創(chuàng)建Maven工程,由于版本不一樣,這里我使用2022.3版本,有一個(gè)將junit加入classpath的設(shè)置,還有將各種目錄設(shè)置:Mark Directory as :src/main/java 關(guān)聯(lián)為 Sources Root;(右擊java》選擇mark Dirctory as》選擇Sources Root);src/main/resources 關(guān)聯(lián)為Resources Root;(右擊resources》選擇mark Dirctory as》選擇Resources Root);src/test/java 關(guān)聯(lián)為Test Sources Root;(右擊test文件夾下test》選擇mark Dirctory as》選擇Test Sources Root;src/test/resources 關(guān)聯(lián)為 Test Resources Root;(右擊test文件夾下resources》選擇mark Dirctory as》選擇Test Resources Root)
還有最后一步就是:選中pom.xml文件,右鍵add as Maven project。Jar包沒(méi)有生效就重新加載pom.xml文件。
HDFS文件下載
@Test
public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException{
// 1 獲取文件系統(tǒng)
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "xiaoming");
// 2 執(zhí)行下載操作
// boolean delSrc 指是否將原文件刪除
// Path src 指要下載的文件路徑
// Path dst 指將文件下載到的路徑
// boolean useRawLocalFileSystem 是否開啟文件校驗(yàn)
fs.copyToLocalFile(false, new Path("/banzhang.txt"), new Path("e:/banhua.txt"), true);
// 3 關(guān)閉資源
fs.close();
例如:本地上傳a.txt到HDFS上面,首先會(huì)在本地對(duì)a.txt進(jìn)行一個(gè)校驗(yàn)得到一個(gè)校驗(yàn)值(123),然后將a.txt和校驗(yàn)值123一起上傳到HDFS。在HDFS上面進(jìn)行性文件a.txt的再一次校驗(yàn)值***,將123 與 ***進(jìn)行對(duì)比。下載也是一樣的流程。比如上傳文件到百度網(wǎng)盤,會(huì)發(fā)現(xiàn)有時(shí)候特別快,上傳的文件會(huì)有一個(gè)校驗(yàn)值,其他人上傳相同文件的時(shí)候,會(huì)將另外待上傳相同文件計(jì)算一個(gè)校驗(yàn)值去服務(wù)端查找,有此校驗(yàn)值,就保存一個(gè)在百度網(wǎng)網(wǎng)盤。上傳違法文件時(shí),也會(huì)在黑名單保留此文件的校驗(yàn)值。
測(cè)試參數(shù)優(yōu)先級(jí)
- 編寫源代碼
@Test
public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
// 1 獲取文件系統(tǒng)
Configuration configuration = new Configuration();
configuration.set("dfs.replication", "2");
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "xiaoming");
// 2 上傳文件
fs.copyFromLocalFile(new Path("e:/banzhang.txt"), new Path("/banzhang.txt"));
// 3 關(guān)閉資源
fs.close();
System.out.println("over");
- 將hdfs-site.xml拷貝到項(xiàng)目的根目錄(resource)下
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>
- 參數(shù)優(yōu)先級(jí)
參數(shù)優(yōu)先級(jí)排序:(1)客戶端代碼中設(shè)置的值 >(2)ClassPath下的用戶自定義配置文件 >(3)然后是服務(wù)器的默認(rèn)配置
參數(shù)的配置 : 客戶端代碼,客戶端的配置文件,服務(wù)器端xxx-default.xml,服務(wù)器端的xxx-site.xml
在客戶端的執(zhí)行的命令 : 客戶端代碼 > 客戶端的配置文件 > 服務(wù)器端的xxx-default.xml(默認(rèn)備份數(shù)3)
如果注釋掉客戶端代碼和配置文件,這個(gè)時(shí)候如果服務(wù)端也配置了hdfs-site.xml (例如配置8),是不生效的,上傳文件默認(rèn)備份數(shù)3。
在服務(wù)器端執(zhí)行命令 : hadoop fs -put a.txt /demo
服務(wù)器端xxx-site.xml > 服務(wù)器端xxx-default.xml
驗(yàn)證1:客戶端和配置文件都設(shè)置了備份數(shù),看誰(shuí)優(yōu)先級(jí)高!
客戶端設(shè)置為文件備份數(shù)2,配置文件設(shè)置為文件備份數(shù)5
驗(yàn)證2: 只在配置文件里設(shè)置文件備份數(shù)
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-449408.html
驗(yàn)證3: 服務(wù)端 /opt/module/hadoop-3.1.3/etc/hadoop/hdfs-site.xml。設(shè)置之后必須分發(fā)xsync hdfs-site.xml并重啟HDFS。注意:此操作僅涉及服務(wù)端,與客戶端沒(méi)有任何關(guān)系?。。。?!
分發(fā)重啟不能立馬操作!
以下結(jié)果證明了服務(wù)器端xxx-site.xml > 服務(wù)器端xxx-default.xml
注意:剛啟動(dòng)集群不能立馬操作,否則會(huì)報(bào)下面的錯(cuò)。
分享到此,感謝下伙伴們支持!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-449408.html
到了這里,關(guān)于Hadoop HDFS的API操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!